Completed
Branch EDTR/refactor-tkt-reg-count (88344e)
by
unknown
34:40 queued 26:27
created

EEM_Price   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
dl 0
loc 333
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 112 1
A get_new_price() 0 4 1
A get_all_prices() 0 5 1
A get_all_event_prices() 0 10 1
A get_all_default_prices() 0 13 2
A get_all_prices_that_are_taxes() 0 14 3
B get_all_ticket_prices_for_admin() 0 36 8
A _sort_event_prices_by_type() 0 7 3
A _sort_event_prices_by_order() 0 7 3
A get_all_prices_that_are_type() 0 9 1
A _order_by_array_for_get_all_method() 0 8 1
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * Price Model
8
 *
9
 * @package             Event Espresso
10
 * @subpackage          includes/models/EEM_Price.model.php
11
 * @author              Mike Nelson
12
 */
13
class EEM_Price extends EEM_Soft_Delete_Base
14
{
15
16
    // private instance of the EEM_Price object
17
    protected static $_instance;
18
19
20
    /**
21
     * private constructor to prevent direct creation
22
     *
23
     * @Constructor
24
     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings
25
     *                         (and any incoming timezone data that gets saved).
26
     *                         Note this just sends the timezone info to the date time model field objects.
27
     *                         Default is NULL
28
     *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
29
     */
30
    protected function __construct($timezone)
31
    {
32
        require_once(EE_MODELS . 'EEM_Price_Type.model.php');
33
        $this->singular_item = __('Price', 'event_espresso');
34
        $this->plural_item = __('Prices', 'event_espresso');
35
36
        $this->_tables = array(
37
            'Price' => new EE_Primary_Table('esp_price', 'PRC_ID'),
38
        );
39
        $this->_fields = array(
40
            'Price' => array(
41
                'PRC_ID'         => new EE_Primary_Key_Int_Field(
42
                    'PRC_ID',
43
                    'Price ID'
44
                ),
45
                'PRT_ID'         => new EE_Foreign_Key_Int_Field(
46
                    'PRT_ID',
47
                    esc_html__('Price type Id', 'event_espresso'),
48
                    false,
49
                    null,
50
                    'Price_Type'
51
                ),
52
                'PRC_amount'     => new EE_Money_Field(
53
                    'PRC_amount',
54
                    esc_html__('Price Amount', 'event_espresso'),
55
                    false,
56
                    0
57
                ),
58
                'PRC_name'       => new EE_Plain_Text_Field(
59
                    'PRC_name',
60
                    esc_html__('Name of Price', 'event_espresso'),
61
                    false,
62
                    ''
63
                ),
64
                'PRC_desc'       => new EE_Post_Content_Field(
65
                    'PRC_desc',
66
                    esc_html__('Price Description', 'event_espresso'),
67
                    false,
68
                    ''
69
                ),
70
                'PRC_is_default' => new EE_Boolean_Field(
71
                    'PRC_is_default',
72
                    esc_html__('Flag indicating whether price is a default price', 'event_espresso'),
73
                    false,
74
                    false
75
                ),
76
                'PRC_overrides'  => new EE_Integer_Field(
77
                    'PRC_overrides',
78
                    esc_html__(
79
                        'Price ID for a global Price that will be overridden by this Price  ( for replacing default prices )',
80
                        'event_espresso'
81
                    ),
82
                    true,
83
                    0
84
                ),
85
                'PRC_order'      => new EE_Integer_Field(
86
                    'PRC_order',
87
                    esc_html__(
88
                        'Order of Application of Price (lower numbers apply first?)',
89
                        'event_espresso'
90
                    ),
91
                    false,
92
                    1
93
                ),
94
                'PRC_deleted'    => new EE_Trashed_Flag_Field(
95
                    'PRC_deleted',
96
                    esc_html__('Flag Indicating if this has been deleted or not', 'event_espresso'),
97
                    false,
98
                    false
99
                ),
100
                'PRC_parent'     => new EE_Integer_Field(
101
                    'PRC_parent',
102
                    esc_html__('Indicates what PRC_ID is the parent of this PRC_ID', 'event_espresso'),
103
                    true,
104
                    0
105
                ),
106
                'PRC_wp_user'    => new EE_WP_User_Field(
107
                    'PRC_wp_user',
108
                    esc_html__('Price Creator ID', 'event_espresso'),
109
                    false
110
                ),
111
            ),
112
        );
113
        $this->_model_relations = array(
114
            'Ticket'     => new EE_HABTM_Relation('Ticket_Price'),
115
            'Price_Type' => new EE_Belongs_To_Relation(),
116
            'WP_User'    => new EE_Belongs_To_Relation(),
117
        );
118
        // this model is generally available for reading
119
        $this->_cap_restriction_generators[ EEM_Base::caps_read ] =
120
            new EE_Restriction_Generator_Default_Public(
121
                'PRC_is_default',
122
                'Ticket.Datetime.Event'
123
            );
124
        // account for default tickets in the caps
125
        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
126
            new EE_Restriction_Generator_Default_Protected(
127
                'PRC_is_default',
128
                'Ticket.Datetime.Event'
129
            );
130
        $this->_cap_restriction_generators[ EEM_Base::caps_edit ] =
131
            new EE_Restriction_Generator_Default_Protected(
132
                'PRC_is_default',
133
                'Ticket.Datetime.Event'
134
            );
135
        $this->_cap_restriction_generators[ EEM_Base::caps_delete ] =
136
            new EE_Restriction_Generator_Default_Protected(
137
                'PRC_is_default',
138
                'Ticket.Datetime.Event'
139
            );
140
        parent::__construct($timezone);
141
    }
142
143
144
    /**
145
     * instantiate a new price object with blank/empty properties
146
     *
147
     * @return mixed array on success, FALSE on fail
148
     */
149
    public function get_new_price()
150
    {
151
        return $this->create_default_object();
152
    }
153
154
155
    /**
156
     * retrieve  ALL prices from db
157
     *
158
     * @return EE_Base_Class[]|EE_PRice[]
159
     * @throws EE_Error
160
     */
161
    public function get_all_prices()
162
    {
163
        // retrieve all prices
164
        return $this->get_all(array('order_by' => array('PRC_amount' => 'ASC')));
165
    }
166
167
168
    /**
169
     * retrieve all active prices for a particular event
170
     *
171
     * @param int $EVT_ID
172
     * @return array on success
173
     * @throws EE_Error
174
     */
175
    public function get_all_event_prices($EVT_ID = 0)
176
    {
177
        return $this->get_all(array(
178
            array(
179
                'EVT_ID'            => $EVT_ID,
180
                'Price_Type.PBT_ID' => array('!=', EEM_Price_Type::base_type_tax),
181
            ),
182
            'order_by' => $this->_order_by_array_for_get_all_method(),
183
        ));
184
    }
185
186
187
    /**
188
     * retrieve all active global prices (that are not taxes (PBT_ID=4)) for a particular event
189
     *
190
     * @param boolean $count return count
191
     * @return bool|EE_Base_Class[]|EE_PRice[]
192
     * @throws EE_Error
193
     */
194
    public function get_all_default_prices($count = false)
195
    {
196
        $_where = array(
197
            'Price_Type.PBT_ID' => array('!=', 4),
198
            'PRC_deleted'       => 0,
199
            'PRC_is_default'    => 1,
200
        );
201
        $_query_params = array(
202
            $_where,
203
            'order_by' => $this->_order_by_array_for_get_all_method(),
204
        );
205
        return $count ? $this->count(array($_where)) : $this->get_all($_query_params);
206
    }
207
208
209
    /**
210
     * retrieve all prices that are taxes
211
     *
212
     * @return EE_Base_Class[]|EE_PRice[]
213
     * @throws EE_Error
214
     * @throws InvalidArgumentException
215
     * @throws ReflectionException
216
     * @throws InvalidDataTypeException
217
     * @throws InvalidInterfaceException
218
     */
219
    public function get_all_prices_that_are_taxes()
220
    {
221
        $taxes = array();
222
        $all_taxes = $this->get_all(array(
223
            array('Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax),
224
            'order_by' => array('Price_Type.PRT_order' => 'ASC', 'PRC_order' => 'ASC'),
225
        ));
226
        foreach ($all_taxes as $tax) {
227
            if ($tax instanceof EE_Price) {
228
                $taxes[ $tax->order() ][ $tax->ID() ] = $tax;
229
            }
230
        }
231
        return $taxes;
232
    }
233
234
235
    /**
236
     * retrieve all prices for an ticket plus default global prices, but not taxes
237
     *
238
     * @param int $TKT_ID the id of the event.  If not included then we assume that this is a new ticket.
239
     * @return EE_Base_Class[]|EE_PRice[]|boolean
240
     * @throws EE_Error
241
     */
242
    public function get_all_ticket_prices_for_admin($TKT_ID = 0)
243
    {
244
        $array_of_price_objects = array();
245
        if (empty($TKT_ID)) {
246
            // if there is no tkt, get prices with no tkt ID, are global, are not a tax, and are active
247
            // return that list
248
            $default_prices = $this->get_all_default_prices();
249
250
            if ($default_prices) {
251
                foreach ($default_prices as $price) {
0 ignored issues
show
Bug introduced by
The expression $default_prices of type integer|array<integer,object<EE_Base_Class>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
252
                    if ($price instanceof EE_Price) {
253
                        $array_of_price_objects[ $price->type() ][] = $price;
254
                    }
255
                }
256
                return $array_of_price_objects;
257
            }
258
            return array();
259
        }
260
        $ticket_prices = $this->get_all(array(
261
            array(
262
                'TKT_ID'      => $TKT_ID,
263
                'PRC_deleted' => 0,
264
            ),
265
            'order_by' => array('PRC_order' => 'ASC'),
266
        ));
267
268
        if (! empty($ticket_prices)) {
269
            foreach ($ticket_prices as $price) {
270
                if ($price instanceof EE_Price) {
271
                    $array_of_price_objects[ $price->type() ][] = $price;
272
                }
273
            }
274
            return $array_of_price_objects;
275
        }
276
        return false;
277
    }
278
279
280
    /**
281
     * _sort_event_prices_by_type
282
     *
283
     * @param EE_Price $price_a
284
     * @param EE_Price $price_b
285
     * @return bool false on fail
286
     */
287
    public function _sort_event_prices_by_type(EE_Price $price_a, EE_Price $price_b)
288
    {
289
        if ($price_a->type_obj()->order() === $price_b->type_obj()->order()) {
290
            return $this->_sort_event_prices_by_order($price_a, $price_b);
291
        }
292
        return $price_a->type_obj()->order() < $price_b->type_obj()->order() ? -1 : 1;
293
    }
294
295
296
    /**
297
     *        _sort_event_prices_by_order
298
     *
299
     * @param EE_Price $price_a
300
     * @param EE_Price $price_b
301
     * @return bool false on fail
302
     */
303
    public function _sort_event_prices_by_order(EE_Price $price_a, EE_Price $price_b)
304
    {
305
        if ($price_a->order() === $price_b->order()) {
306
            return 0;
307
        }
308
        return $price_a->order() < $price_b->order() ? -1 : 1;
309
    }
310
311
312
    /**
313
     * get all prices of a specific type
314
     *
315
     * @param int $type - PRT_ID
316
     * @return EE_Base_Class[]|EE_PRice[]
317
     * @throws EE_Error
318
     */
319
    public function get_all_prices_that_are_type($type = 0)
320
    {
321
        return $this->get_all(array(
322
            array(
323
                'PRT_ID' => $type,
324
            ),
325
            'order_by' => $this->_order_by_array_for_get_all_method(),
326
        ));
327
    }
328
329
330
    /**
331
     * Returns an array of the normal 'order_by' query parameter provided to the get_all query.
332
     * Of course you don't have to use it, but this is the order we usually want to sort prices by
333
     *
334
     * @return array which can be used like so: $this->get_all(array(array(...where
335
     *               stuff...),'order_by'=>$this->_order_by_array_for_get_all_method()));
336
     */
337
    public function _order_by_array_for_get_all_method()
338
    {
339
        return array(
340
            'PRC_order'            => 'ASC',
341
            'Price_Type.PRT_order' => 'ASC',
342
            'PRC_ID'               => 'ASC',
343
        );
344
    }
345
}
346