Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Transaction_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_transaction | |
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 | * Transaction 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\Transaction; |
| 12 | |
| 13 | /** |
| 14 | * Transaction Factory class. |
| 15 | * |
| 16 | * @since 6.2.0 |
| 17 | */ |
| 18 | class Transaction_Factory extends Entity_Factory { |
| 19 | |
| 20 | /** |
| 21 | * Transaction 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 | 'zbst_status' => 'status', |
| 30 | 'zbst_type' => 'type', |
| 31 | 'zbst_ref' => 'ref', |
| 32 | 'zbst_origin' => 'origin', |
| 33 | 'zbst_parent' => 'parent', |
| 34 | 'zbst_hash' => 'hash', |
| 35 | 'zbst_title' => 'title', |
| 36 | 'zbst_desc' => 'desc', |
| 37 | 'zbst_date' => 'date', |
| 38 | 'zbst_customer_ip' => 'customer_ip', |
| 39 | 'zbst_currency' => 'currency', |
| 40 | 'zbst_net' => 'net', |
| 41 | 'zbst_fee' => 'fee', |
| 42 | 'zbst_discount' => 'discount', |
| 43 | 'zbst_shipping' => 'shipping', |
| 44 | 'zbst_shipping_taxes' => 'shipping_taxes', |
| 45 | 'zbst_shipping_tax' => 'shipping_tax', |
| 46 | 'zbst_taxes' => 'taxes', |
| 47 | 'zbst_tax' => 'tax', |
| 48 | 'zbst_total' => 'total', |
| 49 | 'zbst_date_paid' => 'date_paid', |
| 50 | 'zbst_date_completed' => 'date_completed', |
| 51 | 'zbst_created' => 'created', |
| 52 | 'zbst_lastupdated' => 'lastupdated', |
| 53 | ); |
| 54 | |
| 55 | /** |
| 56 | * Associative field map. |
| 57 | * |
| 58 | * For tags, invoices, transactions, quotes, tasks... |
| 59 | * |
| 60 | * @since 6.2.0 |
| 61 | * @var array |
| 62 | */ |
| 63 | protected static $associative_field_map = array( |
| 64 | 'tags', |
| 65 | ); |
| 66 | |
| 67 | /** |
| 68 | * Get the transaction instance based on the $data array. |
| 69 | * |
| 70 | * @since 6.2.0 |
| 71 | * |
| 72 | * @param array $data The transaction data from the DAL. |
| 73 | * @return mixed The transaction instance. |
| 74 | * |
| 75 | * @throws Factory_Exception If the data passed is invalid. |
| 76 | */ |
| 77 | public static function create( array $data ) { |
| 78 | // Detect if this is a db transaction or a generic transaction |
| 79 | if ( array_key_exists( 'zbst_status', $data ) ) { |
| 80 | return self::create_from_db( $data ); |
| 81 | } elseif ( self::validate_tidy_transaction( $data ) ) { |
| 82 | return self::create_from_tidy_data( $data ); |
| 83 | } |
| 84 | |
| 85 | throw new Factory_Exception( 'Invalid transaction data', Factory_Exception::INVALID_DATA ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Validate the data array (Tidy from DAL) |
| 90 | * |
| 91 | * @since 6.2.0 |
| 92 | * |
| 93 | * @param array $tidy_transaction The tidy data array. |
| 94 | * @return bool If it's valid or not. |
| 95 | */ |
| 96 | public static function validate_tidy_transaction( array $tidy_transaction ): bool { |
| 97 | |
| 98 | if ( empty( $tidy_transaction ) ) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | $valid_fields = array( 'type', 'ref', 'currency' ); |
| 103 | |
| 104 | foreach ( $valid_fields as $field ) { |
| 105 | if ( ! array_key_exists( $field, $tidy_transaction ) ) { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | public static function get_entity_class(): ?string { |
| 117 | return Transaction::class; |
| 118 | } |
| 119 | } |