Passed
Push — master ( 5f90b3...823da0 )
by Aimeos
04:37
created

Standard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 8
dl 0
loc 9
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2021
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
	// protected is a workaround for serialize problem
24
	protected $price;
25
	protected $locale;
26
	protected $customer;
27
	protected $recalc = false;
28
	protected $available = true;
29
30
31
	/**
32
	 * Initializes the shopping cart.
33
	 *
34
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
35
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
36
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
37
	 * @param \Aimeos\MShop\Order\Item\Base\Product\Iface[] $products List of ordered product items
38
	 * @param \Aimeos\MShop\Order\Item\Base\Address\Iface[] $addresses List of order address items
39
	 * @param \Aimeos\MShop\Order\Item\Base\Service\Iface[] $services List of order service items
40
	 * @param \Aimeos\MShop\Order\Item\Base\Product\Iface[] $coupons Associative list of coupon codes as keys and order product items as values
41
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object
42
	 */
43
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
44
		array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
45
		?\Aimeos\MShop\Customer\Item\Iface $custItem = null )
46
	{
47
		parent::__construct( $price, $locale, $values, $products, $addresses, $services, $coupons );
48
49
		$this->price = $price;
50
		$this->locale = $locale;
51
		$this->customer = $custItem;
52
	}
53
54
55
	/**
56
	 * Clones internal objects of the order base item.
57
	 */
58
	public function __clone()
59
	{
60
		parent::__clone();
61
62
		$this->price = clone $this->price;
63
		$this->locale = clone $this->locale;
64
	}
65
66
67
	/**
68
	 * Tests if all necessary items are available to create the order.
69
	 *
70
	 * @param int $what Test for the specific type of completeness
71
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for method chaining
72
	 * @throws \Aimeos\MShop\Order\Exception if there are no products in the basket
73
	 */
74
	public function check( int $what = self::PARTS_ALL ) : \Aimeos\MShop\Order\Item\Base\Iface
75
	{
76
		$this->checkParts( $what );
77
78
		$this->notify( 'check.before', $what );
79
80
		if( ( $what & self::PARTS_PRODUCT ) && ( count( $this->getProducts() ) < 1 ) ) {
81
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Basket empty' ) );
82
		}
83
84
		$this->notify( 'check.after', $what );
85
86
		return $this;
87
	}
88
89
90
	/**
91
	 * Returns the associated customer item
92
	 *
93
	 * @return \Aimeos\MShop\Customer\Item\Iface|null Customer item
94
	 */
95
	public function getCustomerItem() : ?\Aimeos\MShop\Customer\Item\Iface
96
	{
97
		return $this->customer;
98
	}
99
100
101
	/**
102
	 * Returns the ID of the order if already available.
103
	 *
104
	 * @return string|null ID of the order item
105
	 */
106
	public function getId() : ?string
107
	{
108
		return $this->get( 'order.base.id' );
109
	}
110
111
112
	/**
113
	 * Sets the id of the order base object.
114
	 *
115
	 * @param string|null $id Unique ID of the order base object
116
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
117
	 */
118
	public function setId( ?string $id ) : \Aimeos\MShop\Common\Item\Iface
119
	{
120
		$id = \Aimeos\MShop\Common\Item\Base::checkId( $this->getId(), $id );
121
		$this->set( 'order.base.id', $id );
122
123
		if( $id !== null ) {
124
			$this->modified = false;
125
		}
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Returns the ID of the site the item is stored.
133
	 *
134
	 * @return string Site ID (or null if not available)
135
	 */
136
	public function getSiteId() : string
137
	{
138
		return $this->get( 'order.base.siteid', '' );
139
	}
140
141
142
	/**
143
	 * Returns the code of the site the item is stored.
144
	 *
145
	 * @return string Site code (or empty string if not available)
146
	 */
147
	public function getSiteCode() : string
148
	{
149
		return $this->get( 'order.base.sitecode', '' );
150
	}
151
152
153
	/**
154
	 * Returns the comment field of the order item.
155
	 *
156
	 * @return string Comment for the order
157
	 */
158
	public function getComment() : string
159
	{
160
		return $this->get( 'order.base.comment', '' );
161
	}
162
163
164
	/**
165
	 * Sets the comment field of the order item
166
	 *
167
	 * @param string $comment Comment for the order
168
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
169
	 */
170
	public function setComment( ?string $comment ) : \Aimeos\MShop\Order\Item\Base\Iface
171
	{
172
		return $this->set( 'order.base.comment', (string) $comment );
173
	}
174
175
176
	/**
177
	 * Returns modify date/time of the order item base product.
178
	 *
179
	 * @return string|null Returns modify date/time of the order base item
180
	 */
181
	public function getTimeModified() : ?string
182
	{
183
		return $this->get( 'order.base.mtime' );
184
	}
185
186
187
	/**
188
	 * Returns the create date of the item.
189
	 *
190
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
191
	 */
192
	public function getTimeCreated() : ?string
193
	{
194
		return $this->get( 'order.base.ctime' );
195
	}
196
197
198
	/**
199
	 * Returns the editor code of editor who created/modified the item at last.
200
	 *
201
	 * @return string Editorcode of editor who created/modified the item at last
202
	 */
203
	public function editor() : string
204
	{
205
		return $this->get( 'order.base.editor', '' );
206
	}
207
208
209
	/**
210
	 * Returns the customer ID of the customer who has ordered.
211
	 *
212
	 * @return string Unique ID of the customer
213
	 */
214
	public function getCustomerId() : string
215
	{
216
		return $this->get( 'order.base.customerid', '' );
217
	}
218
219
220
	/**
221
	 * Sets the customer ID of the customer who has ordered.
222
	 *
223
	 * @param string $customerid Unique ID of the customer
224
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
225
	 */
226
	public function setCustomerId( ?string $customerid ) : \Aimeos\MShop\Order\Item\Base\Iface
227
	{
228
		if( (string) $customerid !== $this->getCustomerId() )
229
		{
230
			$this->notify( 'setCustomerId.before', (string) $customerid );
231
			$this->set( 'order.base.customerid', (string) $customerid );
232
			$this->notify( 'setCustomerId.after', (string) $customerid );
233
		}
234
235
		return $this;
236
	}
237
238
239
	/**
240
	 * Returns the customer reference field of the order item
241
	 *
242
	 * @return string Customer reference for the order
243
	 */
244
	public function getCustomerReference() : string
245
	{
246
		return $this->get( 'order.base.customerref', '' );
247
	}
248
249
250
	/**
251
	 * Sets the customer reference field of the order item
252
	 *
253
	 * @param string $value Customer reference for the order
254
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
255
	 */
256
	public function setCustomerReference( ?string $value ) : \Aimeos\MShop\Order\Item\Base\Iface
257
	{
258
		return $this->set( 'order.base.customerref', (string) $value );
259
	}
260
261
262
	/**
263
	 * Returns the locales for the basic order item.
264
	 *
265
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
266
	 *  about site, language, country and currency
267
	 */
268
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
269
	{
270
		return $this->locale;
271
	}
272
273
274
	/**
275
	 * Sets the locales for the basic order item.
276
	 *
277
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
278
	 *  about site, language, country and currency
279
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
280
	 */
281
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Order\Item\Base\Iface
282
	{
283
		$this->notify( 'setLocale.before', $locale );
284
285
		$this->locale = clone $locale;
286
		$this->modified = true;
287
288
		$this->notify( 'setLocale.after', $locale );
289
290
		return $this;
291
	}
292
293
294
	/**
295
	 * Returns a price item with amounts calculated for the products, costs, etc.
296
	 *
297
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
298
	 */
299
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
300
	{
301
		if( $this->recalc )
302
		{
303
			$price = $this->price->clear();
304
305
			foreach( $this->getServices() as $list )
306
			{
307
				foreach( $list as $service ) {
308
					$price = $price->addItem( $service->getPrice() );
309
				}
310
			}
311
312
			foreach( $this->getProducts() as $product ) {
313
				$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
314
			}
315
316
			$this->price = $price;
317
			$this->recalc = false;
318
		}
319
320
		return $this->price;
321
	}
322
323
324
	/**
325
	 * Tests if the item is available based on status, time, language and currency
326
	 *
327
	 * @return bool True if available, false if not
328
	 */
329
	public function isAvailable() : bool
330
	{
331
		return $this->available;
332
	}
333
334
335
	/**
336
	 * Sets the general availability of the item
337
	 *
338
	 * @param bool $value True if available, false if not
339
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for method chaining
340
	 */
341
	public function setAvailable( bool $value ) : \Aimeos\MShop\Common\Item\Iface
342
	{
343
		$this->available = $value;
344
		return $this;
345
	}
346
347
348
	/**
349
	 * Sets the modified flag of the object.
350
	 *
351
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for method chaining
352
	 */
353
	public function setModified() : \Aimeos\MShop\Order\Item\Base\Iface
354
	{
355
		$this->recalc = true;
356
		return parent::setModified();
357
	}
358
359
360
	/*
361
	 * Sets the item values from the given array and removes that entries from the list
362
	 *
363
	 * @param array &$list Associative list of item keys and their values
364
	 * @param bool True to set private properties too, false for public only
365
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
366
	 */
367
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
368
	{
369
		$item = $this;
370
		$locale = $item->locale();
371
372
		unset( $list['order.base.siteid'] );
373
		unset( $list['order.base.ctime'] );
374
		unset( $list['order.base.mtime'] );
375
		unset( $list['order.base.editor'] );
376
377
		foreach( $list as $key => $value )
378
		{
379
			switch( $key )
380
			{
381
				case 'order.base.id': $item = $item->setId( $value ); break;
382
				case 'order.base.customerid': $item = $item->setCustomerId( $value ); break;
383
				case 'order.base.languageid': $locale = $locale->setLanguageId( $value ); break;
384
				case 'order.base.customerref': $item = $item->setCustomerReference( $value ); break;
385
				case 'order.base.comment': $item = $item->setComment( $value ); break;
386
				default: continue 2;
387
			}
388
389
			unset( $list[$key] );
390
		}
391
392
		return $item->setLocale( $locale );
393
	}
394
395
396
	/**
397
	 * Returns the item values as array.
398
	 *
399
	 * @param bool True to return private properties, false for public only
400
	 * @return array Associative list of item properties and their values
401
	 */
402
	public function toArray( bool $private = false ) : array
403
	{
404
		$price = $this->getPrice();
405
		$locale = $this->locale();
406
407
		$list = array(
408
			'order.base.id' => $this->getId(),
409
			'order.base.sitecode' => $this->getSiteCode(),
410
			'order.base.customerid' => $this->getCustomerId(),
411
			'order.base.languageid' => $locale->getLanguageId(),
412
			'order.base.currencyid' => $price->getCurrencyId(),
413
			'order.base.price' => $price->getValue(),
414
			'order.base.costs' => $price->getCosts(),
415
			'order.base.rebate' => $price->getRebate(),
416
			'order.base.taxvalue' => $price->getTaxValue(),
417
			'order.base.taxflag' => $price->getTaxFlag(),
418
			'order.base.customerref' => $this->getCustomerReference(),
419
			'order.base.comment' => $this->getComment(),
420
		);
421
422
		if( $private === true )
423
		{
424
			$list['order.base.siteid'] = $this->getSiteId();
425
			$list['order.base.mtime'] = $this->getTimeModified();
426
			$list['order.base.ctime'] = $this->getTimeCreated();
427
			$list['order.base.editor'] = $this->editor();
428
		}
429
430
		return $list;
431
	}
432
433
434
	/**
435
	 * Notifies listeners before the basket becomes an order.
436
	 *
437
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
438
	 */
439
	public function finish() : \Aimeos\MShop\Order\Item\Base\Iface
440
	{
441
		$this->notify( 'setOrder.before' );
442
		return $this;
443
	}
444
445
446
	/**
447
	 * Checks the constants for the different parts of the basket.
448
	 *
449
	 * @param int $value Part constant
450
	 * @throws \Aimeos\MShop\Order\Exception If parts constant is invalid
451
	 */
452
	protected function checkParts( int $value )
453
	{
454
		if( $value < self::PARTS_NONE || $value > self::PARTS_ALL ) {
455
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Flags not within allowed range' ) );
456
		}
457
	}
458
459
460
	/**
461
	 * Checks if the price uses the same currency as the price in the basket.
462
	 *
463
	 * @param \Aimeos\MShop\Price\Item\Iface $item Price item
464
	 */
465
	protected function checkPrice( \Aimeos\MShop\Price\Item\Iface $item )
466
	{
467
		$price = clone $this->price;
468
		$price->addItem( $item );
469
	}
470
}
471