Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Contact_Factory | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| validate_tidy_contact | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| get_entity_class | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Contact Factory. |
| 4 | * |
| 5 | * @package automattic/jetpack-crm |
| 6 | * @since 6.2.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\CRM\Entities\Factories; |
| 10 | |
| 11 | use Automattic\Jetpack\CRM\Entities\Contact; |
| 12 | |
| 13 | /** |
| 14 | * Contact Factory class. |
| 15 | * |
| 16 | * @since 6.2.0 |
| 17 | */ |
| 18 | class Contact_Factory extends Entity_Factory { |
| 19 | |
| 20 | /** |
| 21 | * Contact DB field name mapping. db_field => model_field. |
| 22 | * |
| 23 | * @since 6.2.0 |
| 24 | * @var array |
| 25 | */ |
| 26 | protected static $field_map = array( |
| 27 | 'ID' => 'id', |
| 28 | 'zbs_owner' => 'owner', |
| 29 | 'zbsc_status' => 'status', |
| 30 | 'zbsc_email' => 'email', |
| 31 | 'zbsc_prefix' => 'prefix', |
| 32 | 'zbsc_fname' => 'fname', |
| 33 | 'zbsc_lname' => 'lname', |
| 34 | 'zbsc_addr1' => 'addr1', |
| 35 | 'zbsc_addr2' => 'addr2', |
| 36 | 'zbsc_city' => 'city', |
| 37 | 'zbsc_county' => 'county', |
| 38 | 'zbsc_postcode' => 'postcode', |
| 39 | 'zbsc_country' => 'country', |
| 40 | 'zbsc_secaddr1' => 'secaddr_addr1', |
| 41 | 'zbsc_secaddr2' => 'secaddr_addr2', |
| 42 | 'zbsc_seccity' => 'secaddr_city', |
| 43 | 'zbsc_seccounty' => 'secaddr_county', |
| 44 | 'zbsc_secpostcode' => 'secaddr_postcode', |
| 45 | 'zbsc_seccountry' => 'secaddr_country', |
| 46 | 'zbsc_hometel' => 'hometel', |
| 47 | 'zbsc_worktel' => 'worktel', |
| 48 | 'zbsc_mobtel' => 'mobtel', |
| 49 | 'zbsc_wpid' => 'wpid', |
| 50 | 'zbsc_avatar' => 'avatar', |
| 51 | 'zbsc_tw' => 'tw', |
| 52 | 'zbsc_li' => 'li', |
| 53 | 'zbsc_fb' => 'fb', |
| 54 | 'zbsc_created' => 'created', |
| 55 | 'zbsc_lastupdated' => 'lastupdated', |
| 56 | 'zbsc_lastcontacted' => 'lastcontacted', |
| 57 | ); |
| 58 | |
| 59 | /** |
| 60 | * Associative field map. |
| 61 | * |
| 62 | * For tags, invoices, transactions, quotes, tasks... |
| 63 | * |
| 64 | * @since 6.2.0 |
| 65 | * @var array |
| 66 | */ |
| 67 | protected static $associative_field_map = array( |
| 68 | 'tags', |
| 69 | ); |
| 70 | |
| 71 | /** |
| 72 | * Get the contact instance based on the $data array. |
| 73 | * |
| 74 | * @since 6.2.0 |
| 75 | * |
| 76 | * @param array $data The contact data from the DAL. |
| 77 | * @return mixed The contact instance. |
| 78 | * |
| 79 | * @throws Factory_Exception If the data passed is invalid. |
| 80 | */ |
| 81 | public static function create( array $data ) { |
| 82 | // Detect if this is a db contact or a generic contact |
| 83 | if ( array_key_exists( 'zbsc_status', $data ) ) { |
| 84 | return self::create_from_db( $data ); |
| 85 | } elseif ( self::validate_tidy_contact( $data ) ) { |
| 86 | return self::create_from_tidy_data( $data ); |
| 87 | } |
| 88 | |
| 89 | throw new Factory_Exception( 'Invalid contact data', Factory_Exception::INVALID_DATA ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Validate the data array (Tidy from DAL) |
| 94 | * |
| 95 | * @since 6.2.0 |
| 96 | * |
| 97 | * @param array $tidy_contact The tidy data array. |
| 98 | * @return bool If it's valid or not. |
| 99 | */ |
| 100 | public static function validate_tidy_contact( array $tidy_contact ): bool { |
| 101 | |
| 102 | if ( empty( $tidy_contact ) ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $valid_fields = array( 'id' ); |
| 107 | |
| 108 | foreach ( $valid_fields as $field ) { |
| 109 | if ( ! array_key_exists( $field, $tidy_contact ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | public static function get_entity_class(): ?string { |
| 121 | return Contact::class; |
| 122 | } |
| 123 | } |