Passed
Push — master ( 12d0b0...d66e62 )
by Aimeos
04:50
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
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 8

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-2024
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Item;
13
14
15
/**
16
 * Default implementation of an order invoice item.
17
 *
18
 * @property int oldPaymentStatus Last delivery status before it was changed by setDeliveryStatus()
19
 * @property int oldDeliveryStatus Last payment status before it was changed by setPaymentStatus()
20
 *
21
 * @package MShop
22
 * @subpackage Order
23
 */
24
class Standard
25
	extends \Aimeos\MShop\Order\Item\Base
26
	implements \Aimeos\MShop\Order\Item\Iface
27
{
28
	// protected is a workaround for serialize problem
29
	protected ?\Aimeos\MShop\Customer\Item\Iface $customer;
30
	protected \Aimeos\MShop\Locale\Item\Iface $locale;
31
	protected \Aimeos\MShop\Price\Item\Iface $price;
32
	protected bool $recalc = false;
33
34
35
	/**
36
	 * Initializes the shopping basket.
37
	 *
38
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
39
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
40
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
41
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of ordered product items
42
	 * @param \Aimeos\MShop\Order\Item\Address\Iface[] $addresses List of order address items
43
	 * @param \Aimeos\MShop\Order\Item\Service\Iface[] $services List of order service items
44
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $coupons Associative list of coupon codes as keys and order product items as values
45
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object
46
	 */
47
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
48
		array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
49
		?\Aimeos\MShop\Customer\Item\Iface $custItem = null )
50
	{
51
		parent::__construct( $price, $locale, $values, $products, $addresses, $services, $coupons );
52
53
		$this->price = $price;
54
		$this->locale = $locale;
55
		$this->customer = $custItem;
56
	}
57
58
59
	/**
60
	 * Clones internal objects of the order base item.
61
	 */
62
	public function __clone()
63
	{
64
		parent::__clone();
65
66
		$this->price = clone $this->price;
67
		$this->locale = clone $this->locale;
68
	}
69
70
71
	/**
72
	 * Specifies the data which should be serialized to JSON by json_encode().
73
	 *
74
	 * @return array<string,mixed> Data to serialize to JSON
75
	 */
76
	#[\ReturnTypeWillChange]
77
	public function jsonSerialize()
78
	{
79
		return parent::jsonSerialize() + [
80
			'price' => $this->price,
81
			'locale' => $this->locale,
82
			'customer' => $this->customer,
83
		];
84
	}
85
86
87
	/**
88
	 * Returns the order number
89
	 *
90
	 * @return string Order number
91
	 */
92
	public function getOrderNumber() : string
93
	{
94
		if( self::macro( 'ordernumber' ) ) {
95
			return (string) $this->call( 'ordernumber' );
96
		}
97
98
		return (string) $this->getId();
99
	}
100
101
102
	/**
103
	 * Returns the number of the invoice.
104
	 *
105
	 * @return string Invoice number
106
	 */
107
	public function getInvoiceNumber() : string
108
	{
109
		if( self::macro( 'invoicenumber' ) ) {
110
			return (string) $this->call( 'invoicenumber' );
111
		}
112
113
		return (string) $this->get( 'order.invoiceno', '' );
114
	}
115
116
117
	/**
118
	 * Sets the number of the invoice.
119
	 *
120
	 * @param string|null $value Invoice number
121
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
122
	 */
123
	public function setInvoiceNumber( ?string $value ) : \Aimeos\MShop\Common\Item\Iface
124
	{
125
		return $this->set( 'order.invoiceno', (string) $value );
126
	}
127
128
129
	/**
130
	 * Returns the channel of the invoice (repeating, web, phone, etc).
131
	 *
132
	 * @return string Invoice channel
133
	 */
134
	public function getChannel() : string
135
	{
136
		return (string) $this->get( 'order.channel', '' );
137
	}
138
139
140
	/**
141
	 * Sets the channel of the invoice.
142
	 *
143
	 * @param string|null $channel Invoice channel
144
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
145
	 */
146
	public function setChannel( ?string $channel ) : \Aimeos\MShop\Common\Item\Iface
147
	{
148
		return $this->set( 'order.channel', $this->checkCode( (string) $channel ) );
149
	}
150
151
152
	/**
153
	 * Returns the delivery date of the invoice.
154
	 *
155
	 * @return string|null ISO date in yyyy-mm-dd HH:ii:ss format
156
	 */
157
	public function getDateDelivery() : ?string
158
	{
159
		$value = $this->get( 'order.datedelivery' );
160
		return $value ? substr( $value, 0, 19 ) : null;
161
	}
162
163
164
	/**
165
	 * Sets the delivery date of the invoice.
166
	 *
167
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format
168
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
169
	 */
170
	public function setDateDelivery( ?string $date ) : \Aimeos\MShop\Order\Item\Iface
171
	{
172
		return $this->set( 'order.datedelivery', $this->checkDateFormat( $date ) );
173
	}
174
175
176
	/**
177
	 * Returns the purchase date of the invoice.
178
	 *
179
	 * @return string|null ISO date in yyyy-mm-dd HH:ii:ss format
180
	 */
181
	public function getDatePayment() : ?string
182
	{
183
		$value = $this->get( 'order.datepayment' );
184
		return $value ? substr( $value, 0, 19 ) : null;
185
	}
186
187
188
	/**
189
	 * Sets the purchase date of the invoice.
190
	 *
191
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format
192
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
193
	 */
194
	public function setDatePayment( ?string $date ) : \Aimeos\MShop\Order\Item\Iface
195
	{
196
		return $this->set( 'order.datepayment', $this->checkDateFormat( $date ) );
197
	}
198
199
200
	/**
201
	 * Returns the delivery status of the invoice.
202
	 *
203
	 * @return int Status code constant from \Aimeos\MShop\Order\Item\Base
204
	 */
205
	public function getStatusDelivery() : int
206
	{
207
		return $this->get( 'order.statusdelivery', -1 );
208
	}
209
210
211
	/**
212
	 * Sets the delivery status of the invoice.
213
	 *
214
	 * @param int $status Status code constant from \Aimeos\MShop\Order\Item\Base
215
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
216
	 */
217
	public function setStatusDelivery( int $status ) : \Aimeos\MShop\Order\Item\Iface
218
	{
219
		$this->set( '.statusdelivery', $this->get( 'order.statusdelivery' ) );
220
		return $this->set( 'order.statusdelivery', $status );
221
	}
222
223
224
	/**
225
	 * Returns the payment status of the invoice.
226
	 *
227
	 * @return int Payment constant from \Aimeos\MShop\Order\Item\Base
228
	 */
229
	public function getStatusPayment() : int
230
	{
231
		return $this->get( 'order.statuspayment', -1 );
232
	}
233
234
235
	/**
236
	 * Sets the payment status of the invoice.
237
	 *
238
	 * @param int $status Payment constant from \Aimeos\MShop\Order\Item\Base
239
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
240
	 */
241
	public function setStatusPayment( int $status ) : \Aimeos\MShop\Order\Item\Iface
242
	{
243
		if( $status !== $this->getStatusPayment() ) {
244
			$this->set( 'order.datepayment', date( 'Y-m-d H:i:s' ) );
245
		}
246
247
		$this->set( '.statuspayment', $this->get( 'order.statuspayment' ) );
248
		return $this->set( 'order.statuspayment', $status );
249
	}
250
251
252
	/**
253
	 * Returns the related invoice ID.
254
	 *
255
	 * @return string Related invoice ID
256
	 */
257
	public function getRelatedId() : string
258
	{
259
		return (string) $this->get( 'order.relatedid', '' );
260
	}
261
262
263
	/**
264
	 * Sets the related invoice ID.
265
	 *
266
	 * @param string|null $id Related invoice ID
267
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
268
	 * @throws \Aimeos\MShop\Order\Exception If ID is invalid
269
	 */
270
	public function setRelatedId( ?string $id ) : \Aimeos\MShop\Order\Item\Iface
271
	{
272
		return $this->set( 'order.relatedid', (string) $id );
273
	}
274
275
276
	/**
277
	 * Returns the associated customer item
278
	 *
279
	 * @return \Aimeos\MShop\Customer\Item\Iface|null Customer item
280
	 */
281
	public function getCustomerItem() : ?\Aimeos\MShop\Customer\Item\Iface
282
	{
283
		return $this->customer;
284
	}
285
286
287
	/**
288
	 * Returns the code of the site the item is stored.
289
	 *
290
	 * @return string Site code (or empty string if not available)
291
	 */
292
	public function getSiteCode() : string
293
	{
294
		return $this->get( 'order.sitecode', '' );
295
	}
296
297
298
	/**
299
	 * Returns the comment field of the order item.
300
	 *
301
	 * @return string Comment for the order
302
	 */
303
	public function getComment() : string
304
	{
305
		return $this->get( 'order.comment', '' );
306
	}
307
308
309
	/**
310
	 * Sets the comment field of the order item
311
	 *
312
	 * @param string|null $comment Comment for the order
313
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
314
	 */
315
	public function setComment( ?string $comment ) : \Aimeos\MShop\Order\Item\Iface
316
	{
317
		return $this->set( 'order.comment', (string) $comment );
318
	}
319
320
321
	/**
322
	 * Returns the customer ID of the customer who has ordered.
323
	 *
324
	 * @return string Unique ID of the customer
325
	 */
326
	public function getCustomerId() : string
327
	{
328
		return $this->get( 'order.customerid', '' );
329
	}
330
331
332
	/**
333
	 * Sets the customer ID of the customer who has ordered.
334
	 *
335
	 * @param string|null $customerid Unique ID of the customer
336
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
337
	 */
338
	public function setCustomerId( ?string $customerid ) : \Aimeos\MShop\Order\Item\Iface
339
	{
340
		if( (string) $customerid !== $this->getCustomerId() )
341
		{
342
			$this->notify( 'setCustomerId.before', (string) $customerid );
343
			$this->set( 'order.customerid', (string) $customerid );
344
			$this->notify( 'setCustomerId.after', (string) $customerid );
345
		}
346
347
		return $this;
348
	}
349
350
351
	/**
352
	 * Returns the customer reference field of the order item
353
	 *
354
	 * @return string Customer reference for the order
355
	 */
356
	public function getCustomerReference() : string
357
	{
358
		return $this->get( 'order.customerref', '' );
359
	}
360
361
362
	/**
363
	 * Sets the customer reference field of the order item
364
	 *
365
	 * @param string|null $value Customer reference for the order
366
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
367
	 */
368
	public function setCustomerReference( ?string $value ) : \Aimeos\MShop\Order\Item\Iface
369
	{
370
		return $this->set( 'order.customerref', (string) $value );
371
	}
372
373
374
	/**
375
	 * Returns the locales for the basic order item.
376
	 *
377
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
378
	 *  about site, language, country and currency
379
	 */
380
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
381
	{
382
		return $this->locale;
383
	}
384
385
386
	/**
387
	 * Sets the locales for the basic order item.
388
	 *
389
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
390
	 *  about site, language, country and currency
391
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
392
	 */
393
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Order\Item\Iface
394
	{
395
		$this->notify( 'setLocale.before', $locale );
396
397
		$this->locale = clone $locale;
398
		$this->setModified();
399
400
		$this->notify( 'setLocale.after', $locale );
401
402
		return $this;
403
	}
404
405
406
	/**
407
	 * Returns a price item with amounts calculated for the products, costs, etc.
408
	 *
409
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
410
	 */
411
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
412
	{
413
		if( $this->recalc )
414
		{
415
			$price = $this->price->clear();
416
417
			foreach( $this->getServices() as $list )
418
			{
419
				foreach( $list as $service ) {
420
					$price = $price->addItem( $service->getPrice() );
421
				}
422
			}
423
424
			foreach( $this->getProducts() as $product ) {
425
				$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
426
			}
427
428
			$this->price = $price;
429
			$this->recalc = false;
430
		}
431
432
		return $this->price;
433
	}
434
435
436
	/**
437
	 * Sets the modified flag of the object.
438
	 *
439
	 * @return \Aimeos\MShop\Common\Item\Iface Order base item for method chaining
440
	 */
441
	public function setModified() : \Aimeos\MShop\Common\Item\Iface
442
	{
443
		$this->recalc = true;
444
		return parent::setModified();
445
	}
446
447
448
	/*
449
	 * Sets the item values from the given array and removes that entries from the list
450
	 *
451
	 * @param array &$list Associative list of item keys and their values
452
	 * @param bool True to set private properties too, false for public only
453
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
454
	 */
455
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
456
	{
457
		$item = parent::fromArray( $list, $private );
458
		$locale = $item->locale();
459
460
		foreach( $list as $key => $value )
461
		{
462
			switch( $key )
463
			{
464
				case 'order.channel': !$private ?: $item = $item->setChannel( $value ); break;
465
				case 'order.invoiceno': !$private ?: $item = $item->setInvoiceNumber( $value ); break;
466
				case 'order.statusdelivery': !$private ?: $item = $item->setStatusDelivery( (int) $value ); break;
467
				case 'order.statuspayment': !$private ?: $item = $item->setStatusPayment( (int) $value ); break;
468
				case 'order.datedelivery': !$private ?: $item = $item->setDateDelivery( $value ); break;
469
				case 'order.datepayment': !$private ?: $item = $item->setDatePayment( $value ); break;
470
				case 'order.customerid': !$private ?: $item = $item->setCustomerId( $value ); break;
471
				case 'order.customerref': $item = $item->setCustomerReference( $value ); break;
472
				case 'order.languageid': $locale = $locale->setLanguageId( $value ); break;
473
				case 'order.relatedid': $item = $item->setRelatedId( $value ); break;
474
				case 'order.comment': $item = $item->setComment( $value ); break;
475
				default: continue 2;
476
			}
477
478
			unset( $list[$key] );
479
		}
480
481
		return $item->setLocale( $locale );
482
	}
483
484
485
	/**
486
	 * Returns the item values as array.
487
	 *
488
	 * @param bool True to return private properties, false for public only
489
	 * @return array Associative list of item properties and their values
490
	 */
491
	public function toArray( bool $private = false ) : array
492
	{
493
		$price = $this->getPrice();
494
		$locale = $this->locale();
495
496
		$list = parent::toArray( $private );
497
498
		$list['order.channel'] = $this->getChannel();
499
		$list['order.invoiceno'] = $this->getInvoiceNumber();
500
		$list['order.statusdelivery'] = $this->getStatusDelivery();
501
		$list['order.statuspayment'] = $this->getStatusPayment();
502
		$list['order.datedelivery'] = $this->getDateDelivery();
503
		$list['order.datepayment'] = $this->getDatePayment();
504
		$list['order.relatedid'] = $this->getRelatedId();
505
		$list['order.sitecode'] = $this->getSiteCode();
506
		$list['order.customerid'] = $this->getCustomerId();
507
		$list['order.languageid'] = $locale->getLanguageId();
508
		$list['order.currencyid'] = $price->getCurrencyId();
509
		$list['order.price'] = $price->getValue();
510
		$list['order.costs'] = $price->getCosts();
511
		$list['order.rebate'] = $price->getRebate();
512
		$list['order.taxflag'] = $price->getTaxFlag();
513
		$list['order.taxvalue'] = $price->getTaxValue();
514
		$list['order.customerref'] = $this->getCustomerReference();
515
		$list['order.comment'] = $this->getComment();
516
517
		return $list;
518
	}
519
}
520