Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Quote_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_quote | |
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 | * Quote 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\Quote; |
| 12 | |
| 13 | /** |
| 14 | * Quote Factory class. |
| 15 | * |
| 16 | * @since 6.2.0 |
| 17 | */ |
| 18 | class Quote_Factory extends Entity_Factory { |
| 19 | |
| 20 | /** |
| 21 | * Quote 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 | 'zbsq_id_override' => 'id_override', |
| 30 | 'zbsq_title' => 'title', |
| 31 | 'zbsq_currency' => 'currency', |
| 32 | 'zbsq_value' => 'value', |
| 33 | 'zbsq_date' => 'date', |
| 34 | 'zbsq_template' => 'template', |
| 35 | 'zbsq_content' => 'content', |
| 36 | 'zbsq_notes' => 'notes', |
| 37 | 'zbsq_hash' => 'hash', |
| 38 | 'zbsq_send_attachments' => 'send_attachments', |
| 39 | 'zbsq_lastviewed' => 'lastviewed', |
| 40 | 'zbsq_viewed_count' => 'viewed_count', |
| 41 | 'zbsq_accepted' => 'accepted', |
| 42 | 'zbsq_acceptedsigned' => 'acceptedsigned', |
| 43 | 'zbsq_acceptedip' => 'acceptedip', |
| 44 | 'zbsq_created' => 'created', |
| 45 | 'zbsq_lastupdated' => 'lastupdated', |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * Associative field map. |
| 50 | * |
| 51 | * For tags, invoices, transactions, quotes, tasks... |
| 52 | * |
| 53 | * @since 6.2.0 |
| 54 | * @var array |
| 55 | */ |
| 56 | protected static $associative_field_map = array( |
| 57 | 'tags', |
| 58 | ); |
| 59 | |
| 60 | /** |
| 61 | * Get the quote instance based on the $data array. |
| 62 | * |
| 63 | * @since 6.2.0 |
| 64 | * |
| 65 | * @param array $data The quote data from the DAL. |
| 66 | * @return mixed The quote instance. |
| 67 | * |
| 68 | * @throws Factory_Exception If the data passed is invalid. |
| 69 | */ |
| 70 | public static function create( array $data ) { |
| 71 | // Detect if this is a db quote or a generic quote |
| 72 | if ( array_key_exists( 'zbsq_created', $data ) ) { |
| 73 | return self::create_from_db( $data ); |
| 74 | } elseif ( self::validate_tidy_quote( $data ) ) { |
| 75 | return self::create_from_tidy_data( $data ); |
| 76 | } |
| 77 | |
| 78 | throw new Factory_Exception( 'Invalid quote data', Factory_Exception::INVALID_DATA ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Validate the data array (Tidy from DAL) |
| 83 | * |
| 84 | * @since 6.2.0 |
| 85 | * |
| 86 | * @param array $tidy_quote The tidy data array. |
| 87 | * @return bool If it's valid or not. |
| 88 | */ |
| 89 | public static function validate_tidy_quote( array $tidy_quote ): bool { |
| 90 | |
| 91 | if ( empty( $tidy_quote ) ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | $valid_fields = array( 'title', 'currency', 'value' ); |
| 96 | |
| 97 | foreach ( $valid_fields as $field ) { |
| 98 | if ( ! array_key_exists( $field, $tidy_quote ) ) { |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * {@inheritDoc} |
| 108 | */ |
| 109 | public static function get_entity_class(): ?string { |
| 110 | return Quote::class; |
| 111 | } |
| 112 | } |