Completed
Branch BUG-10177-stop-bot-txns (b6b1ca)
by
unknown
74:10 queued 43:57
created

EE_Cart::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php if (!defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
2
do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' );/**
3
 *
4
 * Event Espresso
5
 *
6
 * Event Registration and Management Plugin for WordPress
7
 *
8
 * @ package		Event Espresso
9
 * @ author		Seth Shoultes
10
 * @ copyright	(c) 2008-2011 Event Espresso  All Rights Reserved.
11
 * @ license		http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
12
 * @ link			http://www.eventespresso.com
13
 *
14
 * ------------------------------------------------------------------------
15
 *
16
 * EE_Cart class
17
 * Used to keep track of which tickets the user has specified they want to purchase.
18
 * This data is used for generating the Transaction and Registrations, and the
19
 * Line Items on cart are themselves saved for creating a persistent snapshot of
20
 * what was purchased and for how much.
21
 *
22
 *
23
 * @ version		2.0
24
 * @subpackage	includes/core/EE_Cart.core.php
25
 * @ author		Mike Nelson, Brent Christensen
26
 *
27
 * ------------------------------------------------------------------------
28
 */
29
 class EE_Cart {
30
31
	/**
32
	 * 	instance of the EE_Cart object
33
	 * 	@access 	private
34
	 *	@var EE_Cart $_instance
35
	 */
36
	private static $_instance;
37
38
	/**
39
	 * 	instance of the EE_Session object
40
	 * 	@access    protected
41
	 *	@var EE_Session $_session
42
	 */
43
	protected $_session;
44
45
	/**
46
	 * The total Line item which comprises all the children line-item subtotals,
47
	 * which in turn each have their line items.
48
	 * Typically, the line item structure will look like:
49
	 * grand total
50
	 * -tickets-sub-total
51
	 * --ticket1
52
	 * --ticket2
53
	 * --...
54
	 * -taxes-sub-total
55
	 * --tax1
56
	 * --tax2
57
	 * @var EE_Line_Item
58
	 */
59
	private $_grand_total;
60
61
62
63
	 /**
64
	  * @singleton method used to instantiate class object
65
	  * @access    public
66
	  * @param EE_Line_Item $grand_total
67
	  * @param EE_Session $session
68
	  * @return \EE_Cart
69
	  */
70
	 public static function instance( EE_Line_Item $grand_total = null, EE_Session $session = null ) {
71
		if ( ! empty( $grand_total ) ){
72
			self::$_instance = new self( $grand_total, $session );
73
		}
74
		// or maybe retrieve an existing one ?
75
		if ( ! self::$_instance instanceof EE_Cart ) {
76
			// try getting the cart out of the session
77
			$saved_cart = $session instanceof EE_Session ? $session->cart() : null;
78
			self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self( $grand_total, $session );
79
			unset( $saved_cart );
80
		}
81
		// verify that cart is ok and grand total line item exists
82
		if ( ! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item ) {
83
			self::$_instance = new self( $grand_total, $session );
84
		}
85
		self::$_instance->get_grand_total();
86
		 // once everything is all said and done, save the cart to the EE_Session
87
		add_action( 'shutdown', array( self::$_instance, 'save_cart' ), 90 );
88
		return self::$_instance;
89
	}
90
91
92
93
	 /**
94
	  *    private constructor to prevent direct creation
95
	  * @Constructor
96
	  * @access private
97
	  * @param EE_Line_Item $grand_total
98
	  * @param EE_Session $session
99
	  */
100
	 private function __construct( EE_Line_Item $grand_total = null, EE_Session $session = null ) {
101
		 do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' );
102
		 $this->set_session( $session );
103
		 if ( $grand_total instanceof EE_Line_Item ) {
104
			 $this->set_grand_total_line_item( $grand_total );
105
		 }
106
	 }
107
108
109
110
	 /**
111
	  * Resets the cart completely (whereas empty_cart
112
	  * @param EE_Line_Item $grand_total
113
	  * @param EE_Session $session
114
	  * @return EE_Cart
115
	  */
116
	 public static function reset( EE_Line_Item $grand_total = null, EE_Session $session = null ) {
117
		 remove_action( 'shutdown', array( self::$_instance, 'save_cart' ), 90 );
118
		 if ( $session instanceof EE_Session ) {
119
			 $session->reset_cart();
120
		 }
121
		 self::$_instance = null;
122
		 return self::instance( $grand_total, $session );
123
	 }
124
125
126
127
	 /**
128
	  * @param EE_Session $session
129
	  */
130
	 public function set_session( EE_Session $session = null ) {
131
		 $this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core( 'Session' );
0 ignored issues
show
Documentation Bug introduced by
It seems like $session instanceof \EE_...)->load_core('Session') can also be of type boolean. However, the property $_session is declared as type object<EE_Session>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
132
	 }
133
134
135
136
	 /**
137
	  * Sets the cart to match the line item. Especially handy for loading an old cart where you
138
	  *  know the grand total line item on it
139
	  * @param EE_Line_Item $line_item
140
	  */
141
	 public function set_grand_total_line_item( EE_Line_Item $line_item ) {
142
		 $this->_grand_total = $line_item;
143
	 }
144
145
146
147
	 /**
148
	  *    get_cart_from_reg_url_link
149
	  * @access public
150
	  * @param EE_Transaction $transaction
151
	  * @param EE_Session $session
152
	  * @return \EE_Cart
153
	  */
154
	public static function get_cart_from_txn( EE_Transaction $transaction, EE_Session $session = null ) {
155
		$grand_total = $transaction->total_line_item();
156
		$grand_total->get_items();
157
		$grand_total->tax_descendants();
158
		return EE_Cart::instance( $grand_total, $session );
159
	}
160
161
162
163
	/**
164
	 * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items
165
	 * @return EE_Line_Item
166
	 */
167
	private function _create_grand_total(){
168
		$this->_grand_total = EEH_Line_Item::create_total_line_item();
169
		return $this->_grand_total;
170
	}
171
172
173
174
	/**
175
	 *	Gets all the line items of object type Ticket
176
	 *	@access public
177
	 *	@return \EE_Line_Item[]
178
	 */
179
	public function get_tickets() {
180
		return EEH_Line_Item::get_ticket_line_items( $this->_grand_total );
181
	}
182
183
184
185
	/**
186
	 * returns the total quantity of tickets in the cart
187
	 *
188
	 * @access public
189
	 * @return int
190
	 */
191
	public function all_ticket_quantity_count() {
192
		$tickets = $this->get_tickets();
193
		if ( empty( $tickets )) {
194
			return 0;
195
		}
196
		$count = 0;
197
		foreach ( $tickets as $ticket ) {
198
			$count = $count + $ticket->get('LIN_quantity');
199
		}
200
		return $count;
201
	}
202
203
204
205
	/**
206
	 *  Gets all the tax line items
207
	 * @return \EE_Line_Item[]
208
	 */
209
	public function get_taxes(){
210
		return EEH_Line_Item::get_taxes_subtotal( $this->_grand_total )->children();
211
	}
212
213
214
215
	/**
216
	 * Gets the total line item (which is a parent of all other line items) on this cart
217
	 * @return EE_Line_Item
218
	 */
219
	public function get_grand_total(){
220
		return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total();
221
	}
222
223
224
225
	/**
226
	 *	@process items for adding to cart
227
	 *	@access public
228
	 *	@param EE_Ticket $ticket
229
	 *	@param int $qty
230
	 *	@return TRUE on success, FALSE on fail
231
	 */
232
	public function add_ticket_to_cart( EE_Ticket $ticket, $qty = 1 ) {
233
		EEH_Line_Item::add_ticket_purchase( $this->get_grand_total(), $ticket, $qty );
234
		return $this->save_cart() ? TRUE : FALSE;
235
	}
236
237
238
239
	/**
240
	 *	get_cart_total_before_tax
241
	 *	@access public
242
	 *	@return float
243
	 */
244
	public function get_cart_total_before_tax() {
245
		return $this->get_grand_total()->recalculate_pre_tax_total();
246
	}
247
248
249
250
	/**
251
	 *	gets the total amount of tax paid for items in this cart
252
	 *	@access public
253
	 *	@return float
254
	 */
255
	public function get_applied_taxes() {
256
		return EEH_Line_Item::ensure_taxes_applied( $this->_grand_total );
257
	}
258
259
260
261
	/**
262
	 *	Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
263
	 *	@access public
264
	 *	@return float
265
	 */
266
	public function get_cart_grand_total() {
267
		EEH_Line_Item::ensure_taxes_applied( $this->_grand_total );
268
		return $this->get_grand_total()->total();
269
	}
270
271
272
273
	/**
274
	 *	Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
275
	 *	@access public
276
	 *	@return float
277
	 */
278
	public function recalculate_all_cart_totals() {
279
		$pre_tax_total = $this->get_cart_total_before_tax();
280
		$taxes_total = EEH_Line_Item::ensure_taxes_applied( $this->_grand_total );
281
		$this->_grand_total->set_total( $pre_tax_total + $taxes_total );
282
		$this->_grand_total->save_this_and_descendants_to_txn();
283
		return $this->get_grand_total()->total();
284
	}
285
286
287
288
	/**
289
	 *	deletes an item from the cart
290
	 *	@access public
291
	 *	@param array|bool|string $line_item_codes
292
	 *	@return int on success, FALSE on fail
293
	 */
294
	public function delete_items( $line_item_codes = FALSE ) {
295
		do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' );
296
		return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes );
297
	}
298
299
300
301
	/**
302
	 *	@remove ALL items from cart and zero ALL totals
303
	 *	@access public
304
	 *	@return bool
305
	 */
306
	public function empty_cart() {
307
		do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' );
308
		$this->_grand_total = $this->_create_grand_total();
309
		return $this->save_cart( TRUE );
310
	}
311
312
313
314
	/**
315
	 *	@remove ALL items from cart and delete total as well
316
	 *	@access public
317
	 *	@return bool
318
	 */
319
	public function delete_cart() {
320
		$deleted = EEH_Line_Item::delete_all_child_items( $this->_grand_total );
321
		if ( $deleted ) {
322
			$deleted += $this->_grand_total->delete();
323
			$this->_grand_total = null;
324
		}
325
		return $deleted;
326
	}
327
328
329
330
	 /**
331
	  * @save cart to session
332
	  * @access public
333
	  * @param bool $apply_taxes
334
	  * @return TRUE on success, FALSE on fail
335
	  */
336
	public function save_cart( $apply_taxes = TRUE ) {
337
		if ( $apply_taxes && $this->_grand_total instanceof EE_Line_Item ) {
338
			EEH_Line_Item::ensure_taxes_applied( $this->_grand_total );
339
			//make sure we don't cache the transaction because it can get stale
340
			if( $this->_grand_total->get_one_from_cache( 'Transaction' ) instanceof EE_Transaction &&
341
				$this->_grand_total->get_one_from_cache( 'Transaction' )->ID()) {
342
				$this->_grand_total->clear_cache( 'Transaction', null, true );
343
			}
344
		}
345
		if ( $this->_session instanceof EE_Session ) {
346
			return $this->_session->set_cart( $this );
347
		} else {
348
			return false;
349
		}
350
	}
351
352
353
354
     /**
355
      * @return array
356
      */
357
     public function __sleep() {
358
         return array( '_grand_total' );
359
     }
360
361
362
363
     /**
364
      * @return array
365
      */
366
     public function __wakeup() {
367
         $this->set_session();
368
     }
369
370
371
}
372
/* End of file EE_Cart.core.php */
373
/* Location: /includes/core/EE_Cart.core.php */
374