Passed
Push — master ( d7cab9...07cded )
by Aimeos
04:47
created

Standard::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item\Service;
12
13
14
/**
15
 * Default order service item implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
class Standard extends Base implements Iface
21
{
22
	/**
23
	 * Clones internal objects of the order product item.
24
	 */
25
	public function __clone()
26
	{
27
		$this->set( '.transactions', map( $this->get( '.transactions', [] ) )->clone() );
28
		$this->set( '.attributes', map( $this->get( '.attributes', [] ) )->clone() );
29
		$this->set( '.price', clone $this->get( '.price' ) );
30
31
		parent::__clone();
32
	}
33
34
35
	/**
36
	 * Returns the price item for the service.
37
	 *
38
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate
39
	 */
40
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
41
	{
42
		return $this->get( '.price' );
43
	}
44
45
46
	/**
47
	 * Sets the price item for the service.
48
	 *
49
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item containing price and additional costs
50
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
51
	 */
52
	public function setPrice( \Aimeos\MShop\Price\Item\Iface $price ) : \Aimeos\MShop\Order\Item\Service\Iface
53
	{
54
		return $this->set( '.price', $price );
55
	}
56
57
58
	/**
59
	 * Returns the associated service item
60
	 *
61
	 * @return \Aimeos\MShop\Service\Item\Iface|null Service item
62
	 */
63
	public function getServiceItem() : ?\Aimeos\MShop\Service\Item\Iface
64
	{
65
		return $this->get( '.service' );
66
	}
67
68
69
	/**
70
	 * Returns the ID of the site the item is stored
71
	 *
72
	 * @return string Site ID (or null if not available)
73
	 */
74
	public function getSiteId() : string
75
	{
76
		return $this->get( 'order.service.siteid', '' );
77
	}
78
79
80
	/**
81
	 * Sets the site ID of the item.
82
	 *
83
	 * @param string $value Unique site ID of the item
84
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
85
	 */
86
	public function setSiteId( string $value ) : \Aimeos\MShop\Order\Item\Service\Iface
87
	{
88
		return $this->set( 'order.service.siteid', $value );
89
	}
90
91
92
	/**
93
	 * Returns the order base ID of the order service if available.
94
	 *
95
	 * @return string|null Base ID of the item.
96
	 */
97
	public function getParentId() : ?string
98
	{
99
		return $this->get( 'order.service.parentid' );
100
	}
101
102
103
	/**
104
	 * Sets the order service base ID of the order service item.
105
	 *
106
	 * @param string|null $value Order service base ID
107
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
108
	 */
109
	public function setParentId( ?string $value ) : \Aimeos\MShop\Order\Item\Service\Iface
110
	{
111
		return $this->set( 'order.service.parentid', $value );
112
	}
113
114
115
	/**
116
	 * Returns the original ID of the service item used for the order.
117
	 *
118
	 * @return string Original service ID
119
	 */
120
	public function getServiceId() : string
121
	{
122
		return $this->get( 'order.service.serviceid', '' );
123
	}
124
125
126
	/**
127
	 * Sets a new ID of the service item used for the order.
128
	 *
129
	 * @param string $servid ID of the service item used for the order
130
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
131
	 */
132
	public function setServiceId( string $servid ) : \Aimeos\MShop\Order\Item\Service\Iface
133
	{
134
		return $this->set( 'order.service.serviceid', $servid );
135
	}
136
137
138
	/**
139
	 * Returns the code of the service item.
140
	 *
141
	 * @return string Service item code
142
	 */
143
	public function getCode() : string
144
	{
145
		return $this->get( 'order.service.code', '' );
146
	}
147
148
149
	/**
150
	 * Sets a new code for the service item.
151
	 *
152
	 * @param string $code Code as defined by the service provider
153
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
154
	 */
155
	public function setCode( string $code ) : \Aimeos\MShop\Order\Item\Service\Iface
156
	{
157
		return $this->set( 'order.service.code', $this->checkCode( $code ) );
158
	}
159
160
161
	/**
162
	 * Returns the name of the service item.
163
	 *
164
	 * @return string Service item name
165
	 */
166
	public function getName() : string
167
	{
168
		return $this->get( 'order.service.name', '' );
169
	}
170
171
172
	/**
173
	 * Sets a new name for the service item.
174
	 *
175
	 * @param string $name service item name
176
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
177
	 */
178
	public function setName( string $name ) : \Aimeos\MShop\Order\Item\Service\Iface
179
	{
180
		return $this->set( 'order.service.name', $name );
181
	}
182
183
184
	/**
185
	 * Returns the type of the service item.
186
	 *
187
	 * @return string service item type
188
	 */
189
	public function getType() : string
190
	{
191
		return $this->get( 'order.service.type', '' );
192
	}
193
194
195
	/**
196
	 * Sets a new type for the service item.
197
	 *
198
	 * @param string $type Type of the service item
199
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
200
	 */
201
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
202
	{
203
		return $this->set( 'order.service.type', $this->checkCode( $type ) );
204
	}
205
206
207
	/**
208
	 * Returns the location of the media.
209
	 *
210
	 * @return string Location of the media
211
	 */
212
	public function getMediaUrl() : string
213
	{
214
		return $this->get( 'order.service.mediaurl', '' );
215
	}
216
217
218
	/**
219
	 * Sets the media url of the service item.
220
	 *
221
	 * @param string $value Location of the media/picture
222
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
223
	 */
224
	public function setMediaUrl( string $value ) : \Aimeos\MShop\Order\Item\Service\Iface
225
	{
226
		return $this->set( 'order.service.mediaurl', $value );
227
	}
228
229
230
	/**
231
	 * Returns the position of the service in the order.
232
	 *
233
	 * @return int|null Service position in the order from 0-n
234
	 */
235
	public function getPosition() : ?int
236
	{
237
		return $this->get( 'order.service.position' );
238
	}
239
240
241
	/**
242
	 * Sets the position of the service within the list of ordered servicees
243
	 *
244
	 * @param int|null $value Service position in the order from 0-n or null for resetting the position
245
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
246
	 * @throws \Aimeos\MShop\Order\Exception If the position is invalid
247
	 */
248
	public function setPosition( ?int $value ) : \Aimeos\MShop\Order\Item\Service\Iface
249
	{
250
		if( $value < 0 ) {
251
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order service position "%1$s" must be greater than 0', $value ) );
252
		}
253
254
		return $this->set( 'order.service.position', $value );
255
	}
256
257
258
	/**
259
	 * Sets the item values from the given array and removes that entries from the list
260
	 *
261
	 * @param array &$list Associative list of item keys and their values
262
	 * @param bool True to set private properties too, false for public only
263
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order service item for chaining method calls
264
	 */
265
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
266
	{
267
		$price = $this->getPrice();
268
		$item = parent::fromArray( $list, $private );
269
270
		foreach( $list as $key => $value )
271
		{
272
			switch( $key )
273
			{
274
				case 'order.service.siteid': !$private ?: $item = $item->setSiteId( $value ); break;
275
				case 'order.service.parentid': !$private ?: $item = $item->setParentId( $value ); break;
276
				case 'order.service.serviceid': !$private ?: $item = $item->setServiceId( $value ); break;
277
				case 'order.service.type': $item = $item->setType( $value ); break;
278
				case 'order.service.code': $item = $item->setCode( $value ); break;
279
				case 'order.service.name': $item = $item->setName( $value ); break;
280
				case 'order.service.currencyid': $price = $price->setCurrencyId( $value ); break;
281
				case 'order.service.price': $price = $price->setValue( $value ); break;
282
				case 'order.service.costs': $price = $price->setCosts( $value ); break;
283
				case 'order.service.rebate': $price = $price->setRebate( $value ); break;
284
				case 'order.service.taxrates': $price = $price->setTaxRates( $value ); break;
285
				case 'order.service.taxvalue': $price = $price->setTaxValue( $value ); break;
286
				case 'order.service.taxflag': $price = $price->setTaxFlag( $value ); break;
287
				case 'order.service.position': $item = $item->setPosition( $value ); break;
288
				case 'order.service.mediaurl': $item = $item->setMediaUrl( $value ); break;
289
				default: continue 2;
290
			}
291
292
			unset( $list[$key] );
293
		}
294
295
		return $item;
296
	}
297
298
299
	/**
300
	 * Returns the item values as array.
301
	 *
302
	 * @param bool True to return private properties, false for public only
303
	 * @return array Associative list of item properties and their values.
304
	 */
305
	public function toArray( bool $private = false ) : array
306
	{
307
		$price = $this->getPrice();
308
		$list = parent::toArray( $private );
309
310
		$list['order.service.type'] = $this->getType();
311
		$list['order.service.code'] = $this->getCode();
312
		$list['order.service.name'] = $this->getName();
313
		$list['order.service.currencyid'] = $price->getCurrencyId();
314
		$list['order.service.price'] = $price->getValue();
315
		$list['order.service.costs'] = $price->getCosts();
316
		$list['order.service.rebate'] = $price->getRebate();
317
		$list['order.service.taxrates'] = $price->getTaxRates();
318
		$list['order.service.taxvalue'] = $price->getTaxValue();
319
		$list['order.service.taxflag'] = $price->getTaxFlag();
320
		$list['order.service.position'] = $this->getPosition();
321
		$list['order.service.mediaurl'] = $this->getMediaUrl();
322
		$list['order.service.serviceid'] = $this->getServiceId();
323
324
		if( $private === true ) {
325
			$list['order.service.parentid'] = $this->getParentId();
326
		}
327
328
		return $list;
329
	}
330
331
332
	/**
333
	 * Copys all data from a given service item.
334
	 *
335
	 * @param \Aimeos\MShop\Service\Item\Iface $service New service item
336
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
337
	 */
338
	public function copyFrom( \Aimeos\MShop\Service\Item\Iface $service ) : \Aimeos\MShop\Order\Item\Service\Iface
339
	{
340
		if( self::macro( 'copyFrom' ) ) {
341
			return $this->call( 'copyFrom', $service );
342
		}
343
344
		$values = $service->toArray();
345
		$this->fromArray( $values );
346
347
		$this->setSiteId( $service->getSiteId() );
348
		$this->setCode( $service->getCode() );
349
		$this->setName( $service->getName() );
350
		$this->setType( $service->getType() );
351
		$this->setServiceId( $service->getId() );
0 ignored issues
show
Bug introduced by
It seems like $service->getId() can also be of type null; however, parameter $servid of Aimeos\MShop\Order\Item\...tandard::setServiceId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

351
		$this->setServiceId( /** @scrutinizer ignore-type */ $service->getId() );
Loading history...
352
353
		if( ( $item = $service->getRefItems( 'media', 'default', 'default' )->first() ) !== null ) {
354
			$this->setMediaUrl( $item->getUrl() );
355
		}
356
357
		return $this->setModified();
358
	}
359
}
360