Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Entity_Factory | |
0.00% |
0 / 42 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
| create | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| create_from_tidy_data | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| create_from_db | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| tidy_data | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| data_for_dal | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| entity_new_instance | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_fields_map | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_associative_field_map | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_entity_class | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * Base Entity Factory. |
| 4 | * |
| 5 | * @package automattic/jetpack-crm |
| 6 | * @since 6.2.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\CRM\Entities\Factories; |
| 10 | |
| 11 | /** |
| 12 | * Base Entity Factory. |
| 13 | * |
| 14 | * @since 6.2.0 |
| 15 | */ |
| 16 | abstract class Entity_Factory { |
| 17 | |
| 18 | /** |
| 19 | * Database field name mapping. |
| 20 | * |
| 21 | * Each array entry represents a map of the database name and the corresponding model field. |
| 22 | * |
| 23 | * Example: array( 'db_column' => 'entity_property' ). |
| 24 | * |
| 25 | * @since 6.2.0 |
| 26 | * @var string[] |
| 27 | */ |
| 28 | protected static $field_map = array(); |
| 29 | |
| 30 | /** |
| 31 | * Associative field map. |
| 32 | * |
| 33 | * For tags, invoices, transactions, quotes, tasks, etc. |
| 34 | * |
| 35 | * @since 6.2.0 |
| 36 | * @var string[] |
| 37 | */ |
| 38 | protected static $associative_field_map = array(); |
| 39 | |
| 40 | /** |
| 41 | * Create the instance of the class based on the data from DAL. |
| 42 | * |
| 43 | * @since 6.2.0 |
| 44 | * |
| 45 | * @param array $data The data to create the instance with. |
| 46 | * @return mixed The entity instance. |
| 47 | */ |
| 48 | abstract public static function create( array $data ); |
| 49 | |
| 50 | /** |
| 51 | * Create the entity instance from a generic/tidy data array. |
| 52 | * |
| 53 | * @since 6.2.0 |
| 54 | * |
| 55 | * @param array $tidy_data An array with the tidy data from DAL. |
| 56 | * @return mixed The entity instance. |
| 57 | * |
| 58 | * @throws Factory_Exception If the entity class is invalid. |
| 59 | */ |
| 60 | protected static function create_from_tidy_data( array $tidy_data ) { |
| 61 | |
| 62 | $entity = self::entity_new_instance(); |
| 63 | |
| 64 | $fields_map = static::get_fields_map(); |
| 65 | |
| 66 | // Process primary fields |
| 67 | foreach ( $tidy_data as $field => $value ) { |
| 68 | if ( in_array( $field, $fields_map, true ) ) { |
| 69 | $entity->{ $field } = $value; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $associative_field_map = static::get_associative_field_map(); |
| 74 | |
| 75 | // Process associative fields |
| 76 | foreach ( $associative_field_map as $field ) { |
| 77 | if ( array_key_exists( $field, $tidy_data ) ) { |
| 78 | $entity->{ $field } = $tidy_data[ $field ]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $entity; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Create the entity instance from the database data array. |
| 87 | * |
| 88 | * @since 6.2.0 |
| 89 | * |
| 90 | * @param array $db_data The data array from the database. |
| 91 | * @return mixed The entity instance. |
| 92 | * |
| 93 | * @throws Factory_Exception If the entity class is invalid. |
| 94 | */ |
| 95 | protected static function create_from_db( array $db_data ) { |
| 96 | $entity = self::entity_new_instance(); |
| 97 | |
| 98 | $fields_map = static::get_fields_map(); |
| 99 | |
| 100 | foreach ( $db_data as $key => $value ) { |
| 101 | if ( array_key_exists( $key, $fields_map ) ) { |
| 102 | $entity->{ $fields_map[ $key ] } = $value; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return $entity; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the data (tidy) as an array from the entity instance. |
| 111 | * |
| 112 | * @since 6.2.0 |
| 113 | * |
| 114 | * @param mixed $entity The entity instance. |
| 115 | * @return array The tidy data array. |
| 116 | * |
| 117 | * @throws Factory_Exception If the entity class is invalid. |
| 118 | */ |
| 119 | public static function tidy_data( $entity ): array { |
| 120 | |
| 121 | $entity_class = static::get_entity_class(); |
| 122 | |
| 123 | if ( ! $entity instanceof $entity_class ) { |
| 124 | throw new Factory_Exception( 'Invalid entity instance provided.', Factory_Exception::INVALID_ENTITY_CLASS ); |
| 125 | } |
| 126 | |
| 127 | $fields_map = static::get_fields_map(); |
| 128 | |
| 129 | $tidy_data = array(); |
| 130 | foreach ( $fields_map as $value ) { |
| 131 | $tidy_data[ $value ] = $entity->{ $value }; |
| 132 | } |
| 133 | |
| 134 | return $tidy_data; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the data from the entity instance as an array ready for the DAL. |
| 139 | * |
| 140 | * @since 6.2.0 |
| 141 | * |
| 142 | * @param mixed $entity The entity instance. |
| 143 | * @return array The data array for the DAL. |
| 144 | */ |
| 145 | public static function data_for_dal( $entity ): array { |
| 146 | $db_input_data = array( |
| 147 | 'id' => $entity->id, |
| 148 | 'owner' => $entity->owner, |
| 149 | 'data' => array(), |
| 150 | ); |
| 151 | |
| 152 | $skip_fields = array( 'id', 'owner' ); |
| 153 | |
| 154 | $fields_map = static::get_fields_map(); |
| 155 | |
| 156 | foreach ( $fields_map as $entity_field ) { |
| 157 | if ( in_array( $entity_field, $skip_fields, true ) ) { |
| 158 | continue; |
| 159 | } |
| 160 | $db_input_data['data'][ $entity_field ] = $entity->{ $entity_field }; |
| 161 | } |
| 162 | return $db_input_data; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Create an empty entity instance. |
| 167 | * |
| 168 | * @since 6.2.0 |
| 169 | * |
| 170 | * @return mixed The entity instance. |
| 171 | * @throws Factory_Exception If the entity class is invalid. |
| 172 | */ |
| 173 | protected static function entity_new_instance() { |
| 174 | $entity_class = static::get_entity_class(); |
| 175 | |
| 176 | if ( class_exists( $entity_class ) ) { |
| 177 | return new $entity_class(); |
| 178 | } else { |
| 179 | throw new Factory_Exception( 'Invalid entity class provided.', Factory_Exception::INVALID_ENTITY_CLASS ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Return the fields map. |
| 185 | * |
| 186 | * 'db_column' => 'entity_property' |
| 187 | * |
| 188 | * @since 6.2.0 |
| 189 | * |
| 190 | * @return array The fields map. |
| 191 | */ |
| 192 | public static function get_fields_map(): array { |
| 193 | return static::$field_map; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return the associative fields map. |
| 198 | * |
| 199 | * Tags, files, etc. |
| 200 | * |
| 201 | * @since 6.2.0 |
| 202 | * |
| 203 | * @return array The associative fields map. |
| 204 | */ |
| 205 | public static function get_associative_field_map(): array { |
| 206 | return static::$associative_field_map; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Return the entity class handle by the Factory. |
| 211 | * |
| 212 | * @since 6.2.0 |
| 213 | * |
| 214 | * @return string|null The entity class. |
| 215 | */ |
| 216 | abstract public static function get_entity_class(): ?string; |
| 217 | } |