Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Invoice_Factory
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 validate_tidy_invoice
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 get_entity_class
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Invoice Factory.
4 *
5 * @package automattic/jetpack-crm
6 * @since 6.2.0
7 */
8
9namespace Automattic\Jetpack\CRM\Entities\Factories;
10
11use Automattic\Jetpack\CRM\Entities\Invoice;
12
13/**
14 * Invoice Factory class.
15 *
16 * @since 6.2.0
17 */
18class Invoice_Factory extends Entity_Factory {
19
20    /**
21     * Invoice 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        'zbsi_status'              => 'status',
30        'zbsi_id_override'         => 'id_override',
31        'zbsi_parent'              => 'parent',
32        'zbsi_hash'                => 'hash',
33        'zbsi_send_attachments'    => 'send_attachments',
34        'zbsi_pdf_template'        => 'pdf_template',
35        'zbsi_portal_template'     => 'portal_template',
36        'zbsi_email_template'      => 'email_template',
37        'zbsi_invoice_frequency'   => 'invoice_frequency',
38        'zbsi_currency'            => 'currency',
39        'zbsi_pay_via'             => 'pay_via',
40        'zbsi_logo_url'            => 'logo_url',
41        'zbsi_address_to_objtype'  => 'address_to_objtype',
42        'zbsi_addressed_from'      => 'addressed_from',
43        'zbsi_addressed_to'        => 'addressed_to',
44        'zbsi_allow_partial'       => 'allow_partial',
45        'zbsi_allow_tip'           => 'allow_tip',
46        'zbsi_hours_or_quantity'   => 'hours_or_quantity',
47        'zbsi_date'                => 'date',
48        'zbsi_due_date'            => 'due_date',
49        'zbsi_paid_date'           => 'paid_date',
50        'zbsi_hash_viewed'         => 'hash_viewed',
51        'zbsi_hash_viewed_count'   => 'hash_viewed_count',
52        'zbsi_portal_viewed'       => 'portal_viewed',
53        'zbsi_portal_viewed_count' => 'portal_viewed_count',
54        'zbsi_net'                 => 'net',
55        'zbsi_discount'            => 'discount',
56        'zbsi_discount_type'       => 'discount_type',
57        'zbsi_shipping'            => 'shipping',
58        'zbsi_shipping_taxes'      => 'shipping_taxes',
59        'zbsi_shipping_tax'        => 'shipping_tax',
60        'zbsi_taxes'               => 'taxes',
61        'zbsi_tax'                 => 'tax',
62        'zbsi_total'               => 'total',
63        'zbsi_created'             => 'created',
64        'zbsi_lastupdated'         => 'lastupdated',
65    );
66
67    /**
68     * Associative field map.
69     *
70     * For tags, invoices, transactions, quotes, tasks...
71     *
72     * @since 6.2.0
73     * @var array
74     */
75    protected static $associative_field_map = array(
76        'tags',
77    );
78
79    /**
80     * Get the invoice instance based on the $data array.
81     *
82     * @since 6.2.0
83     *
84     * @param array $data The invoice data from the DAL.
85     * @return mixed The invoice instance.
86     *
87     * @throws Factory_Exception If the data passed is invalid.
88     */
89    public static function create( array $data ) {
90        // Detect if this is a db invoice or a generic invoice
91        if ( array_key_exists( 'zbsi_status', $data ) ) {
92            return self::create_from_db( $data );
93        } elseif ( self::validate_tidy_invoice( $data ) ) {
94            return self::create_from_tidy_data( $data );
95        }
96
97        throw new Factory_Exception( 'Invalid invoice data', Factory_Exception::INVALID_DATA );
98    }
99
100    /**
101     * Validate the data array (Tidy from DAL)
102     *
103     * @since 6.2.0
104     *
105     * @param array $tidy_invoice The tidy data array.
106     * @return bool If it's valid or not.
107     */
108    public static function validate_tidy_invoice( array $tidy_invoice ): bool {
109
110        if ( empty( $tidy_invoice ) ) {
111            return false;
112        }
113
114        $valid_fields = array( 'parent', 'hash', 'id_override' );
115
116        foreach ( $valid_fields as $field ) {
117            if ( ! array_key_exists( $field, $tidy_invoice ) ) {
118                return false;
119            }
120        }
121
122        return true;
123    }
124
125    /**
126     * {@inheritDoc}
127     */
128    public static function get_entity_class(): ?string {
129        return Invoice::class;
130    }
131}