Passed
Push — master ( 8a9822...bee67c )
by Aimeos
04:54
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-2022
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
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 \Aimeos\MShop\Order\Item\Base\Product\Iface[] $products List of ordered product items
37
	 * @param \Aimeos\MShop\Order\Item\Base\Address\Iface[] $addresses List of order address items
38
	 * @param \Aimeos\MShop\Order\Item\Base\Service\Iface[] $services List of order service items
39
	 * @param \Aimeos\MShop\Order\Item\Base\Product\Iface[] $coupons Associative list of coupon codes as keys and order product items as values
40
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object
41
	 */
42
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
43
		array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
44
		?\Aimeos\MShop\Customer\Item\Iface $custItem = null )
45
	{
46
		parent::__construct( $price, $locale, $values, $products, $addresses, $services, $coupons );
47
48
		$this->price = $price;
49
		$this->locale = $locale;
50
		$this->customer = $custItem;
51
	}
52
53
54
	/**
55
	 * Clones internal objects of the order base item.
56
	 */
57
	public function __clone()
58
	{
59
		parent::__clone();
60
61
		$this->price = clone $this->price;
62
		$this->locale = clone $this->locale;
63
	}
64
65
66
	/**
67
	 * Specifies the data which should be serialized to JSON by json_encode().
68
	 *
69
	 * @return array<string,mixed> Data to serialize to JSON
70
	 */
71
	#[\ReturnTypeWillChange]
72
	public function jsonSerialize()
73
	{
74
		return parent::jsonSerialize() + [
75
			'price' => $this->price,
76
			'locale' => $this->locale,
77
			'customer' => $this->customer,
78
		];
79
	}
80
81
82
	/**
83
	 * Tests if all necessary items are available to create the order.
84
	 *
85
	 * @param array $what Type of data
86
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for method chaining
87
	 * @throws \Aimeos\MShop\Order\Exception if there are no products in the basket
88
	 */
89
	public function check( array $what = ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'] ) : \Aimeos\MShop\Order\Item\Base\Iface
90
	{
91
		$this->notify( 'check.before', $what );
92
93
		if( in_array( 'order/base/product', $what ) && ( count( $this->getProducts() ) < 1 ) ) {
94
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Basket empty' ) );
95
		}
96
97
		$this->notify( 'check.after', $what );
98
99
		return $this;
100
	}
101
102
103
	/**
104
	 * Returns the associated customer item
105
	 *
106
	 * @return \Aimeos\MShop\Customer\Item\Iface|null Customer item
107
	 */
108
	public function getCustomerItem() : ?\Aimeos\MShop\Customer\Item\Iface
109
	{
110
		return $this->customer;
111
	}
112
113
114
	/**
115
	 * Returns the code of the site the item is stored.
116
	 *
117
	 * @return string Site code (or empty string if not available)
118
	 */
119
	public function getSiteCode() : string
120
	{
121
		return $this->get( 'order.base.sitecode', '' );
122
	}
123
124
125
	/**
126
	 * Returns the comment field of the order item.
127
	 *
128
	 * @return string Comment for the order
129
	 */
130
	public function getComment() : string
131
	{
132
		return $this->get( 'order.base.comment', '' );
133
	}
134
135
136
	/**
137
	 * Sets the comment field of the order item
138
	 *
139
	 * @param string $comment Comment for the order
140
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
141
	 */
142
	public function setComment( ?string $comment ) : \Aimeos\MShop\Order\Item\Base\Iface
143
	{
144
		return $this->set( 'order.base.comment', (string) $comment );
145
	}
146
147
148
	/**
149
	 * Returns modify date/time of the order item base product.
150
	 *
151
	 * @return string|null Returns modify date/time of the order base item
152
	 */
153
	public function getTimeModified() : ?string
154
	{
155
		return $this->get( 'order.base.mtime' );
156
	}
157
158
159
	/**
160
	 * Returns the create date of the item.
161
	 *
162
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
163
	 */
164
	public function getTimeCreated() : ?string
165
	{
166
		return $this->get( 'order.base.ctime' );
167
	}
168
169
170
	/**
171
	 * Returns the editor code of editor who created/modified the item at last.
172
	 *
173
	 * @return string Editorcode of editor who created/modified the item at last
174
	 */
175
	public function editor() : string
176
	{
177
		return $this->get( 'order.base.editor', '' );
178
	}
179
180
181
	/**
182
	 * Returns the customer ID of the customer who has ordered.
183
	 *
184
	 * @return string Unique ID of the customer
185
	 */
186
	public function getCustomerId() : string
187
	{
188
		return $this->get( 'order.base.customerid', '' );
189
	}
190
191
192
	/**
193
	 * Sets the customer ID of the customer who has ordered.
194
	 *
195
	 * @param string $customerid Unique ID of the customer
196
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
197
	 */
198
	public function setCustomerId( ?string $customerid ) : \Aimeos\MShop\Order\Item\Base\Iface
199
	{
200
		if( (string) $customerid !== $this->getCustomerId() )
201
		{
202
			$this->notify( 'setCustomerId.before', (string) $customerid );
203
			$this->set( 'order.base.customerid', (string) $customerid );
204
			$this->notify( 'setCustomerId.after', (string) $customerid );
205
		}
206
207
		return $this;
208
	}
209
210
211
	/**
212
	 * Returns the customer reference field of the order item
213
	 *
214
	 * @return string Customer reference for the order
215
	 */
216
	public function getCustomerReference() : string
217
	{
218
		return $this->get( 'order.base.customerref', '' );
219
	}
220
221
222
	/**
223
	 * Sets the customer reference field of the order item
224
	 *
225
	 * @param string $value Customer reference for the order
226
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
227
	 */
228
	public function setCustomerReference( ?string $value ) : \Aimeos\MShop\Order\Item\Base\Iface
229
	{
230
		return $this->set( 'order.base.customerref', (string) $value );
231
	}
232
233
234
	/**
235
	 * Returns the locales for the basic order item.
236
	 *
237
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
238
	 *  about site, language, country and currency
239
	 */
240
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
241
	{
242
		return $this->locale;
243
	}
244
245
246
	/**
247
	 * Sets the locales for the basic order item.
248
	 *
249
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
250
	 *  about site, language, country and currency
251
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
252
	 */
253
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Order\Item\Base\Iface
254
	{
255
		$this->notify( 'setLocale.before', $locale );
256
257
		$this->locale = clone $locale;
258
		$this->setModified();
259
260
		$this->notify( 'setLocale.after', $locale );
261
262
		return $this;
263
	}
264
265
266
	/**
267
	 * Returns a price item with amounts calculated for the products, costs, etc.
268
	 *
269
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
270
	 */
271
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
272
	{
273
		if( $this->recalc )
274
		{
275
			$price = $this->price->clear();
276
277
			foreach( $this->getServices() as $list )
278
			{
279
				foreach( $list as $service ) {
280
					$price = $price->addItem( $service->getPrice() );
281
				}
282
			}
283
284
			foreach( $this->getProducts() as $product ) {
285
				$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
286
			}
287
288
			$this->price = $price;
289
			$this->recalc = false;
290
		}
291
292
		return $this->price;
293
	}
294
295
296
	/**
297
	 * Sets the modified flag of the object.
298
	 *
299
	 * @return \Aimeos\MShop\Common\Item\Iface Order base item for method chaining
300
	 */
301
	public function setModified() : \Aimeos\MShop\Common\Item\Iface
302
	{
303
		$this->recalc = true;
304
		return parent::setModified();
305
	}
306
307
308
	/*
309
	 * Sets the item values from the given array and removes that entries from the list
310
	 *
311
	 * @param array &$list Associative list of item keys and their values
312
	 * @param bool True to set private properties too, false for public only
313
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
314
	 */
315
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
316
	{
317
		$item = $this;
318
		$locale = $item->locale();
319
320
		parent::fromArray( $list, $private );
321
322
		foreach( $list as $key => $value )
323
		{
324
			switch( $key )
325
			{
326
				case 'order.base.id': $item = $item->setId( $value ); break;
327
				case 'order.base.customerid': $item = $item->setCustomerId( $value ); break;
328
				case 'order.base.languageid': $locale = $locale->setLanguageId( $value ); break;
329
				case 'order.base.customerref': $item = $item->setCustomerReference( $value ); break;
330
				case 'order.base.comment': $item = $item->setComment( $value ); break;
331
				default: continue 2;
332
			}
333
334
			unset( $list[$key] );
335
		}
336
337
		return $item->setLocale( $locale );
338
	}
339
340
341
	/**
342
	 * Returns the item values as array.
343
	 *
344
	 * @param bool True to return private properties, false for public only
345
	 * @return array Associative list of item properties and their values
346
	 */
347
	public function toArray( bool $private = false ) : array
348
	{
349
		$price = $this->getPrice();
350
		$locale = $this->locale();
351
352
		$list = parent::toArray( $private );
353
354
		$list['order.base.id'] = $this->getId();
355
		$list['order.base.sitecode'] = $this->getSiteCode();
356
		$list['order.base.customerid'] = $this->getCustomerId();
357
		$list['order.base.languageid'] = $locale->getLanguageId();
358
		$list['order.base.currencyid'] = $price->getCurrencyId();
359
		$list['order.base.price'] = $price->getValue();
360
		$list['order.base.costs'] = $price->getCosts();
361
		$list['order.base.rebate'] = $price->getRebate();
362
		$list['order.base.taxflag'] = $price->getTaxFlag();
363
		$list['order.base.taxvalue'] = $price->getTaxValue();
364
		$list['order.base.customerref'] = $this->getCustomerReference();
365
		$list['order.base.comment'] = $this->getComment();
366
367
		if( $private === true )
368
		{
369
			$list['order.base.siteid'] = $this->getSiteId();
370
			$list['order.base.mtime'] = $this->getTimeModified();
371
			$list['order.base.ctime'] = $this->getTimeCreated();
372
			$list['order.base.editor'] = $this->editor();
373
		}
374
375
		return $list;
376
	}
377
378
379
	/**
380
	 * Notifies listeners before the basket becomes an order.
381
	 *
382
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for chaining method calls
383
	 */
384
	public function finish() : \Aimeos\MShop\Order\Item\Base\Iface
385
	{
386
		$this->notify( 'setOrder.before' );
387
		return $this;
388
	}
389
390
391
	/**
392
	 * Checks if the price uses the same currency as the price in the basket.
393
	 *
394
	 * @param \Aimeos\MShop\Price\Item\Iface $item Price item
395
	 */
396
	protected function checkPrice( \Aimeos\MShop\Price\Item\Iface $item )
397
	{
398
		$price = clone $this->price;
399
		$price->addItem( $item );
400
	}
401
}
402