Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Task_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_task | |
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 | * Task 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\Task; |
| 12 | |
| 13 | /** |
| 14 | * Task Factory class. |
| 15 | * |
| 16 | * @since 6.2.0 |
| 17 | */ |
| 18 | class Task_Factory extends Entity_Factory { |
| 19 | |
| 20 | /** |
| 21 | * Task 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 | 'zbse_title' => 'title', |
| 30 | 'zbse_desc' => 'desc', |
| 31 | 'zbse_start' => 'start', |
| 32 | 'zbse_end' => 'end', |
| 33 | 'zbse_complete' => 'complete', |
| 34 | 'zbse_show_on_portal' => 'show_on_portal', |
| 35 | 'zbse_show_on_cal' => 'show_on_calendar', |
| 36 | 'zbse_created' => 'created', |
| 37 | 'zbse_lastupdated' => 'lastupdated', |
| 38 | ); |
| 39 | |
| 40 | /** |
| 41 | * Associative field map. |
| 42 | * |
| 43 | * For tags, invoices, transactions, quotes, tasks... |
| 44 | * |
| 45 | * @since 6.2.0 |
| 46 | * @var array |
| 47 | */ |
| 48 | protected static $associative_field_map = array( |
| 49 | 'tags', |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * Get the task instance based on the $data array. |
| 54 | * |
| 55 | * @since 6.2.0 |
| 56 | * |
| 57 | * @param array $data The task data from the DAL. |
| 58 | * @return mixed The task instance. |
| 59 | * |
| 60 | * @throws Factory_Exception If the data passed is invalid. |
| 61 | */ |
| 62 | public static function create( array $data ) { |
| 63 | // Detect if this is a db task or a generic task |
| 64 | if ( array_key_exists( 'zbse_created', $data ) ) { |
| 65 | return self::create_from_db( $data ); |
| 66 | } elseif ( self::validate_tidy_task( $data ) ) { |
| 67 | return self::create_from_tidy_data( $data ); |
| 68 | } |
| 69 | |
| 70 | throw new Factory_Exception( 'Invalid task data', Factory_Exception::INVALID_DATA ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Validate the data array (Tidy from DAL) |
| 75 | * |
| 76 | * @since 6.2.0 |
| 77 | * |
| 78 | * @param array $tidy_task The tidy data array. |
| 79 | * @return bool If it's valid or not. |
| 80 | */ |
| 81 | public static function validate_tidy_task( array $tidy_task ): bool { |
| 82 | |
| 83 | if ( empty( $tidy_task ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $valid_fields = array( 'title', 'desc', 'start' ); |
| 88 | |
| 89 | foreach ( $valid_fields as $field ) { |
| 90 | if ( ! array_key_exists( $field, $tidy_task ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * {@inheritDoc} |
| 100 | */ |
| 101 | public static function get_entity_class(): ?string { |
| 102 | return Task::class; |
| 103 | } |
| 104 | } |