Passed
Push — master ( 144a01...69c572 )
by Aimeos
05:22
created

Standard   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 322
rs 8.8
c 0
b 0
f 0
wmc 45

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceItem() 0 3 1
A setSiteId() 0 3 1
A getServiceId() 0 3 1
A getName() 0 3 1
A getCode() 0 3 1
A getMediaUrl() 0 3 1
A __construct() 0 6 1
A setMediaUrl() 0 3 1
A setName() 0 3 1
A getPosition() 0 7 2
A setBaseId() 0 3 1
A setServiceId() 0 3 1
A getType() 0 3 1
A setPosition() 0 7 3
A setType() 0 3 1
A getBaseId() 0 3 1
A setCode() 0 3 1
A getSiteId() 0 3 1
A toArray() 0 24 2
D fromArray() 0 31 20
A copyFrom() 0 16 2

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

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

335
		$this->setType( /** @scrutinizer ignore-type */ $service->getType() );
Loading history...
336
		$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

336
		$this->setServiceId( /** @scrutinizer ignore-type */ $service->getId() );
Loading history...
337
338
		if( ( $item = $service->getRefItems( 'media', 'default', 'default' )->first() ) !== null ) {
339
			$this->setMediaUrl( $item->getUrl() );
340
		}
341
342
		return $this->setModified();
343
	}
344
}
345