Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Company_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_company | |
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 | * Company 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\Company; |
| 12 | |
| 13 | /** |
| 14 | * Company Factory class. |
| 15 | * |
| 16 | * @since 6.2.0 |
| 17 | */ |
| 18 | class Company_Factory extends Entity_Factory { |
| 19 | |
| 20 | /** |
| 21 | * Company 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 | 'zbsco_status' => 'status', |
| 30 | 'zbsco_email' => 'email', |
| 31 | 'zbsco_name' => 'name', |
| 32 | 'zbsco_addr1' => 'addr1', |
| 33 | 'zbsco_addr2' => 'addr2', |
| 34 | 'zbsco_city' => 'city', |
| 35 | 'zbsco_county' => 'county', |
| 36 | 'zbsco_postcode' => 'postcode', |
| 37 | 'zbsco_country' => 'country', |
| 38 | 'zbsco_secaddr1' => 'secaddr_addr1', |
| 39 | 'zbsco_secaaddr2' => 'secaddr_addr2', |
| 40 | 'zbsco_seccity' => 'secaddr_city', |
| 41 | 'zbsco_seccounty' => 'secaddr_county', |
| 42 | 'zbsco_secpostcode' => 'secaddr_postcode', |
| 43 | 'zbsco_seccountry' => 'secaddr_country', |
| 44 | 'zbsco_maintel' => 'maintel', |
| 45 | 'zbsco_sectel' => 'sectel', |
| 46 | 'zbsco_wpid' => 'wpid', |
| 47 | 'zbsco_avatar' => 'avatar', |
| 48 | 'zbsco_tw' => 'tw', |
| 49 | 'zbsco_li' => 'li', |
| 50 | 'zbsco_fb' => 'fb', |
| 51 | 'zbsco_created' => 'created', |
| 52 | 'zbsco_lastupdated' => 'lastupdated', |
| 53 | 'zbsco_lastcontacted' => 'lastcontacted', |
| 54 | ); |
| 55 | |
| 56 | /** |
| 57 | * Associative field map. |
| 58 | * |
| 59 | * For tags, invoices, transactions, quotes, tasks... |
| 60 | * |
| 61 | * @since 6.2.0 |
| 62 | * @var array |
| 63 | */ |
| 64 | protected static $associative_field_map = array( |
| 65 | 'tags', |
| 66 | ); |
| 67 | |
| 68 | /** |
| 69 | * Get the company instance based on the $data array. |
| 70 | * |
| 71 | * @since 6.2.0 |
| 72 | * |
| 73 | * @param array $data The company data from the DAL. |
| 74 | * @return mixed The company instance. |
| 75 | * |
| 76 | * @throws Factory_Exception If the data passed is invalid. |
| 77 | */ |
| 78 | public static function create( array $data ) { |
| 79 | // Detect if this is a db company or a generic company |
| 80 | if ( array_key_exists( 'zbsco_status', $data ) ) { |
| 81 | return self::create_from_db( $data ); |
| 82 | } elseif ( self::validate_tidy_company( $data ) ) { |
| 83 | return self::create_from_tidy_data( $data ); |
| 84 | } |
| 85 | |
| 86 | throw new Factory_Exception( 'Invalid company data', Factory_Exception::INVALID_DATA ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Validate the data array (Tidy from DAL) |
| 91 | * |
| 92 | * @since 6.2.0 |
| 93 | * |
| 94 | * @param array $tidy_company The tidy data array. |
| 95 | * @return bool If it's valid or not. |
| 96 | */ |
| 97 | public static function validate_tidy_company( array $tidy_company ): bool { |
| 98 | |
| 99 | if ( empty( $tidy_company ) ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | $valid_fields = array( 'name', 'email', 'addr1' ); |
| 104 | |
| 105 | foreach ( $valid_fields as $field ) { |
| 106 | if ( ! array_key_exists( $field, $tidy_company ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * {@inheritDoc} |
| 116 | */ |
| 117 | public static function get_entity_class(): ?string { |
| 118 | return Company::class; |
| 119 | } |
| 120 | } |