Completed
Push — master ( ab8a43...3303a4 )
by Aimeos
11:36
created

Standard::setAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Item\Base;
13
14
15
/**
16
 * Default implementation of the shopping basket.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard extends \Aimeos\MShop\Order\Item\Base\Base
22
{
23
	private $price;
24
	private $locale;
25
	private $values;
26
	private $available = true;
27
	private $modified = false;
28
29
30
	/**
31
	 * Initializes the shopping cart.
32
	 *
33
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
34
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
35
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
36
	 * @param array $products List of ordered products implementing \Aimeos\MShop\Order\Item\Base\Product\Iface
37
	 * @param array $addresses List of order addresses implementing \Aimeos\MShop\Order\Item\Base\Address\Iface
38
	 * @param array $services List of order services implementing \Aimeos\MShop\Order\Item\Base\Service\Iface
39
	 * @param array $coupons Associative list of coupon codes as keys and ordered products implementing \Aimeos\MShop\Order\Item\Base\Product\Iface as values
40
	 */
41
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
42
		array $values = [], array $products = [], array $addresses = [],
43
		array $services = [], array $coupons = [] )
44
	{
45
		parent::__construct( $price, $locale, $values, $products, $addresses, $services, $coupons );
46
47
		$this->price = $price;
48
		$this->locale = $locale;
49
		$this->values = $values;
50
	}
51
52
53
	/**
54
	 * Clones internal objects of the order base item.
55
	 */
56
	public function __clone()
57
	{
58
		parent::__clone();
59
60
		$this->price = clone $this->price;
61
		$this->locale = clone $this->locale;
62
	}
63
64
65
	/**
66
	 * Prepares the object for serialization.
67
	 *
68
	 * @return array List of properties that should be serialized
69
	 */
70
	public function __sleep()
71
	{
72
		/*
73
		 * Workaround because database connections can't be serialized
74
		 * Listeners will be reattached on wakeup by the order base manager
75
		 */
76
		$this->clearListeners();
77
78
		return array_keys( get_object_vars( $this ) );
79
	}
80
81
82
	/**
83
	 * Returns the ID of the order if already available.
84
	 *
85
	 * @return string|null ID of the order item
86
	 */
87
	public function getId()
88
	{
89
		if( isset( $this->values['order.base.id'] ) ) {
90
			return (string) $this->values['order.base.id'];
91
		}
92
93
		return null;
94
	}
95
96
97
	/**
98
	 * Sets the id of the order base object.
99
	 *
100
	 * @param string $id Unique ID of the order base object
101
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
102
	 */
103
	public function setId( $id )
104
	{
105
		if( ( $this->values['order.base.id'] = \Aimeos\MShop\Common\Item\Base::checkId( $this->getId(), $id ) ) === null ) {
106
			$this->modified = true;
107
		} else {
108
			$this->modified = false;
109
		}
110
111
		return $this;
112
	}
113
114
115
	/**
116
	 * Returns the ID of the site the item is stored.
117
	 *
118
	 * @return string|null Site ID (or null if not available)
119
	 */
120
	public function getSiteId()
121
	{
122
		if( isset( $this->values['order.base.siteid'] ) ) {
123
			return (string) $this->values['order.base.siteid'];
124
		}
125
	}
126
127
128
	/**
129
	 * Returns the code of the site the item is stored.
130
	 *
131
	 * @return string Site code (or empty string if not available)
132
	 */
133
	public function getSiteCode()
134
	{
135
		if( isset( $this->values['order.base.sitecode'] ) ) {
136
			return (string) $this->values['order.base.sitecode'];
137
		}
138
139
		return '';
140
	}
141
142
143
	/**
144
	 * Returns the comment field of the order item.
145
	 *
146
	 * @return string Comment for the order
147
	 */
148
	public function getComment()
149
	{
150
		if( isset( $this->values['order.base.comment'] ) ) {
151
			return (string) $this->values['order.base.comment'];
152
		}
153
154
		return '';
155
	}
156
157
158
	/**
159
	 * Sets the comment field of the order item
160
	 *
161
	 * @param string $comment Comment for the order
162
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
163
	 */
164
	public function setComment( $comment )
165
	{
166
		if( (string) $comment !== $this->getComment() )
167
		{
168
			$this->values['order.base.comment'] = (string) $comment;
169
			$this->modified = true;
170
		}
171
172
		return $this;
173
	}
174
175
176
	/**
177
	 * Returns the current status of the order base item.
178
	 *
179
	 * @return integer Status of the item
180
	 */
181
	public function getStatus()
182
	{
183
		if( isset( $this->values['order.base.status'] ) ) {
184
			return (int) $this->values['order.base.status'];
185
		}
186
187
		return 0;
188
	}
189
190
191
	/**
192
	 * Sets the new status of the order base item.
193
	 *
194
	 * @param integer $value Status of the item
195
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
196
	 */
197
	public function setStatus( $value )
198
	{
199
		if( (int) $value !== $this->getStatus() )
200
		{
201
			$this->values['order.base.status'] = (int) $value;
202
			$this->modified = true;
203
		}
204
205
		return $this;
206
	}
207
208
209
	/**
210
	 * Returns modify date/time of the order item base product.
211
	 *
212
	 * @return string|null Returns modify date/time of the order base item
213
	 */
214
	public function getTimeModified()
215
	{
216
		if( isset( $this->values['order.base.mtime'] ) ) {
217
			return (string) $this->values['order.base.mtime'];
218
		}
219
	}
220
221
222
	/**
223
	 * Returns the create date of the item.
224
	 *
225
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
226
	 */
227
	public function getTimeCreated()
228
	{
229
		if( isset( $this->values['order.base.ctime'] ) ) {
230
			return (string) $this->values['order.base.ctime'];
231
		}
232
	}
233
234
235
	/**
236
	 * Returns the editor code of editor who created/modified the item at last.
237
	 *
238
	 * @return string Editorcode of editor who created/modified the item at last
239
	 */
240
	public function getEditor()
241
	{
242
		if( isset( $this->values['order.base.editor'] ) ) {
243
			return (string) $this->values['order.base.editor'];
244
		}
245
246
		return '';
247
	}
248
249
250
	/**
251
	 * Returns the customer ID of the customer who has ordered.
252
	 *
253
	 * @return string Unique ID of the customer
254
	 */
255
	public function getCustomerId()
256
	{
257
		if( isset( $this->values['order.base.customerid'] ) ) {
258
			return (string) $this->values['order.base.customerid'];
259
		}
260
261
		return '';
262
	}
263
264
265
	/**
266
	 * Sets the customer ID of the customer who has ordered.
267
	 *
268
	 * @param string $customerid Unique ID of the customer
269
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
270
	 */
271
	public function setCustomerId( $customerid )
272
	{
273
		if( (string) $customerid !== $this->getCustomerId() )
274
		{
275
			$this->notifyListeners( 'setCustomerId.before', $customerid );
276
277
			$this->values['order.base.customerid'] = (string) $customerid;
278
			$this->modified = true;
279
280
			$this->notifyListeners( 'setCustomerId.after', $customerid );
281
		}
282
283
		return $this;
284
	}
285
286
287
	/**
288
	 * Returns the locales for the basic order item.
289
	 *
290
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
291
	 *  about site, language, country and currency
292
	 */
293
	public function getLocale()
294
	{
295
		return $this->locale;
296
	}
297
298
299
	/**
300
	 * Sets the locales for the basic order item.
301
	 *
302
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
303
	 *  about site, language, country and currency
304
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
305
	 */
306
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale )
307
	{
308
		$this->notifyListeners( 'setLocale.before', $locale );
309
310
		$this->locale = clone $locale;
311
		$this->modified = true;
312
313
		$this->notifyListeners( 'setLocale.after', $locale );
314
315
		return $this;
316
	}
317
318
319
	/**
320
	 * Returns a price item with amounts calculated for the products, costs, etc.
321
	 *
322
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
323
	 */
324
	public function getPrice()
325
	{
326
		if( $this->price->getValue() === '0.00' )
327
		{
328
			$this->price->clear();
329
			$currencyId = $this->price->getCurrencyId();
330
331
			foreach( $this->getServices() as $list )
332
			{
333
				foreach( $list as $service ) {
334
					$this->price->addItem( $service->getPrice()->setCurrencyId( $currencyId ) );
335
				}
336
			}
337
338
			foreach( $this->getProducts() as $product ) {
339
				$this->price->addItem( $product->getPrice()->setCurrencyId( $currencyId ), $product->getQuantity() );
340
			}
341
		}
342
343
		return $this->price;
344
	}
345
346
347
	/**
348
	 * Tests if the item is available based on status, time, language and currency
349
	 *
350
	 * @return boolean True if available, false if not
351
	 */
352
	public function isAvailable()
353
	{
354
		return $this->available;
355
	}
356
357
358
	/**
359
	 * Sets the general availability of the item
360
	 *
361
	 * @return boolean $value True if available, false if not
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
362
	 */
363
	public function setAvailable( $value )
364
	{
365
		$this->available = (bool) $value;
366
	}
367
368
369
	/**
370
	 * Tests if the order object was modified.
371
	 *
372
	 * @return bool True if modified, false if not
373
	 */
374
	public function isModified()
375
	{
376
		return ( parent::isModified() === false ? $this->modified : true );
377
	}
378
379
380
	/**
381
	 * Sets the modified flag of the object.
382
	 */
383
	public function setModified()
384
	{
385
		$this->modified = true;
386
		$this->price->clear();
387
	}
388
389
390
	/**
391
	 * Sets the item values from the given array.
392
	 *
393
	 * @param array $list Associative list of item keys and their values
394
	 * @return array Associative list of keys and their values that are unknown
395
	 */
396
	public function fromArray( array $list )
397
	{
398
		$unknown = [];
399
400
		foreach( $list as $key => $value )
401
		{
402
			switch( $key )
403
			{
404
				case 'order.base.id': $this->setId( $value ); break;
405
				case 'order.base.comment': $this->setComment( $value ); break;
406
				case 'order.base.customerid': $this->setCustomerId( $value ); break;
407
				case 'order.base.status': $this->setStatus( $value ); break;
408
				case 'order.base.languageid': $this->locale->setLanguageId( $value ); break;
409
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
410
			}
411
		}
412
413
		unset( $list['order.base.siteid'] );
414
		unset( $list['order.base.ctime'] );
415
		unset( $list['order.base.mtime'] );
416
		unset( $list['order.base.editor'] );
417
418
		return $unknown;
419
	}
420
421
422
	/**
423
	 * Returns the item values as array.
424
	 *
425
	 * @param boolean True to return private properties, false for public only
426
	 * @return array Associative list of item properties and their values
427
	 */
428
	public function toArray( $private = false )
429
	{
430
		$price = $this->getPrice();
431
		$locale = $this->getLocale();
432
433
		$list = array(
434
			'order.base.customerid' => $this->getCustomerId(),
435
			'order.base.sitecode' => $this->getSiteCode(),
436
			'order.base.languageid' => $locale->getLanguageId(),
437
			'order.base.currencyid' => $price->getCurrencyId(),
438
			'order.base.price' => $price->getValue(),
439
			'order.base.costs' => $price->getCosts(),
440
			'order.base.rebate' => $price->getRebate(),
441
			'order.base.taxvalue' => $price->getTaxValue(),
442
			'order.base.taxflag' => $price->getTaxFlag(),
443
			'order.base.status' => $this->getStatus(),
444
			'order.base.comment' => $this->getComment(),
445
		);
446
447
		if( $private === true )
448
		{
449
			$list['order.base.id'] = $this->getId();
450
			$list['order.base.siteid'] = $this->getSiteId();
451
			$list['order.base.mtime'] = $this->getTimeModified();
452
			$list['order.base.ctime'] = $this->getTimeCreated();
453
			$list['order.base.editor'] = $this->getEditor();
454
		}
455
456
		return $list;
457
	}
458
459
460
	/**
461
	 * Notifies listeners before the basket becomes an order.
462
	 *
463
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
464
	 */
465
	public function finish()
466
	{
467
		$this->notifyListeners( 'setOrder.before' );
468
		return $this;
469
	}
470
471
472
	/**
473
	 * Checks if the price uses the same currency as the price in the basket.
474
	 *
475
	 * @param \Aimeos\MShop\Price\Item\Iface $item Price item
476
	 */
477
	protected function checkPrice( \Aimeos\MShop\Price\Item\Iface $item )
478
	{
479
		$price = clone $this->price;
480
		$price->addItem( $item );
481
	}
482
}
483