Passed
Push — master ( b7c60c...771ab2 )
by Aimeos
05:10
created

Standard   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 446
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 107
dl 0
loc 446
c 0
b 0
f 0
rs 3.6
wmc 60

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setParentId() 0 9 2
A getParentId() 0 7 2
C fromArray() 0 24 12
A getPosition() 0 7 2
A setDateEnd() 0 9 2
A toArray() 0 20 2
A setRefItem() 0 5 1
A setStatus() 0 9 2
A setRefId() 0 9 2
A getRefId() 0 7 2
A setConfig() 0 6 1
A getKey() 0 3 1
A setDomain() 0 9 2
A isAvailable() 0 5 6
A getDateStart() 0 7 2
A getStatus() 0 7 2
A getType() 0 4 2
A getDateEnd() 0 7 2
A setType() 0 9 2
A setPosition() 0 9 2
A getConfig() 0 7 2
A getRefItem() 0 3 1
A getResourceType() 0 3 1
A getDomain() 0 7 2
A setDateStart() 0 9 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, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MShop
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\MShop\Common\Item\Lists;
13
14
15
/**
16
 * Default implementation of the list item.
17
 *
18
 * @package MShop
19
 * @subpackage Common
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
23
	implements \Aimeos\MShop\Common\Item\Lists\Iface
24
{
25
	use \Aimeos\MShop\Common\Item\Config\Traits;
26
27
28
	private $prefix;
29
	private $values;
30
	private $refItem;
31
32
33
	/**
34
	 * Initializes the list item object.
35
	 *
36
	 * @param string $prefix Property prefix when converting to array
37
	 * @param array $values Associative list of key/value pairs of the list item
38
	 */
39
	public function __construct( $prefix, array $values = [] )
40
	{
41
		parent::__construct( $prefix, $values );
42
43
		$this->prefix = (string) $prefix;
44
		$this->values = $values;
45
	}
46
47
48
49
50
	/**
51
	 * Returns the parent ID of the common list item,
52
	 * like the unique ID of a product or a tree node.
53
	 *
54
	 * @return string|null Parent ID of the common list item
55
	 */
56
	public function getParentId()
57
	{
58
		if( isset( $this->values[$this->prefix . 'parentid'] ) ) {
59
			return (string) $this->values[$this->prefix . 'parentid'];
60
		}
61
62
		return null;
63
	}
64
65
66
	/**
67
	 * Sets the parent ID of the common list item,
68
	 * like the unique ID of a product or a tree node.
69
	 *
70
	 * @param string $parentid New parent ID of the common list item
71
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
72
	 */
73
	public function setParentId( $parentid )
74
	{
75
		if( (string) $parentid !== $this->getParentId() )
76
		{
77
			$this->values[$this->prefix . 'parentid'] = (int) $parentid;
78
			$this->setModified();
79
		}
80
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the unique key of the list item
87
	 *
88
	 * @return string Unique key consisting of domain/type/refid
89
	 */
90
	public function getKey()
91
	{
92
		return $this->getDomain() . '|' . $this->getType() . '|' . $this->getRefId();
93
	}
94
95
96
	/**
97
	 * Returns the domain of the common list item, e.g. text or media.
98
	 *
99
	 * @return string Domain of the common list item
100
	 */
101
	public function getDomain()
102
	{
103
		if( isset( $this->values[$this->prefix . 'domain'] ) ) {
104
			return (string) $this->values[$this->prefix . 'domain'];
105
		}
106
107
		return '';
108
	}
109
110
111
	/**
112
	 * Sets the new domain of the common list item, e.g. text od media.
113
	 *
114
	 * @param string $domain New domain of the common list item
115
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
116
	 */
117
	public function setDomain( $domain )
118
	{
119
		if( (string) $domain !== $this->getDomain() )
120
		{
121
			$this->values[$this->prefix . 'domain'] = (string) $domain;
122
			$this->setModified();
123
		}
124
125
		return $this;
126
	}
127
128
129
	/**
130
	 * Returns the reference id of the common list item, like the unique id
131
	 * of a text item or a media item.
132
	 *
133
	 * @return string Reference id of the common list item
134
	 */
135
	public function getRefId()
136
	{
137
		if( isset( $this->values[$this->prefix . 'refid'] ) ) {
138
			return (string) $this->values[$this->prefix . 'refid'];
139
		}
140
141
		return '';
142
	}
143
144
145
	/**
146
	 * Sets the new reference id of the common list item, like the unique id
147
	 * of a text item or a media item.
148
	 *
149
	 * @param string $refid New reference id of the common list item
150
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
151
	 */
152
	public function setRefId( $refid )
153
	{
154
		if( (string) $refid !== $this->getRefId() )
155
		{
156
			$this->values[$this->prefix . 'refid'] = (string) $refid;
157
			$this->setModified();
158
		}
159
160
		return $this;
161
	}
162
163
164
	/**
165
	 * Returns the start date of the common list item (YYYY-MM-DD hh:mm:ss).
166
	 *
167
	 * @return string|null Start date of the common list item (YYYY-MM-DD hh:mm:ss)
168
	 */
169
	public function getDateStart()
170
	{
171
		if( isset( $this->values[$this->prefix . 'datestart'] ) ) {
172
			return (string) $this->values[$this->prefix . 'datestart'];
173
		}
174
175
		return null;
176
	}
177
178
179
	/**
180
	 * Sets the new start date of the common list item (YYYY-MM-DD hh:mm:ss).
181
	 *
182
	 * @param string $date New start date of the common list item (YYYY-MM-DD hh:mm:ss)
183
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
184
	 */
185
	public function setDateStart( $date )
186
	{
187
		if( (string) $date !== $this->getDateStart() )
188
		{
189
			$this->values[$this->prefix . 'datestart'] = $this->checkDateFormat( $date );
190
			$this->setModified();
191
		}
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the end date of the common list item (YYYY-MM-DD hh:mm:ss).
199
	 *
200
	 * @return string|null End date of the common list item (YYYY-MM-DD hh:mm:ss)
201
	 */
202
	public function getDateEnd()
203
	{
204
		if( isset( $this->values[$this->prefix . 'dateend'] ) ) {
205
			return (string) $this->values[$this->prefix . 'dateend'];
206
		}
207
208
		return null;
209
	}
210
211
212
	/**
213
	 * Sets the new end date of the common list item (YYYY-MM-DD hh:mm:ss).
214
	 *
215
	 * @param string $date New end date of the common list item (YYYY-MM-DD hh:mm:ss)
216
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
217
	 */
218
	public function setDateEnd( $date )
219
	{
220
		if( (string) $date !== $this->getDateEnd() )
221
		{
222
			$this->values[$this->prefix . 'dateend'] = $this->checkDateFormat( $date );
223
			$this->setModified();
224
		}
225
226
		return $this;
227
	}
228
229
230
	/**
231
	 * Returns the type of the list item.
232
	 *
233
	 * @return string|null Type of the list item
234
	 */
235
	public function getType()
236
	{
237
		if( isset( $this->values[$this->prefix . 'type'] ) ) {
238
			return (string) $this->values[$this->prefix . 'type'];
239
		}
240
	}
241
242
243
244
	/**
245
	 * Sets the new type of the list item.
246
	 *
247
	 * @param string $type type of the list item
248
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
249
	 */
250
	public function setType( $type )
251
	{
252
		if( (string) $type != $this->getType() )
253
		{
254
			$this->values[$this->prefix . 'type'] = (string) $type;
255
			$this->setModified();
256
		}
257
258
		return $this;
259
	}
260
261
262
	/**
263
	 * Returns the position of the list item.
264
	 *
265
	 * @return integer Position of the list item
266
	 */
267
	public function getPosition()
268
	{
269
		if( isset( $this->values[$this->prefix . 'position'] ) ) {
270
			return (int) $this->values[$this->prefix . 'position'];
271
		}
272
273
		return 0;
274
	}
275
276
277
	/**
278
	 * Sets the new position of the list item.
279
	 *
280
	 * @param integer $pos position of the list item
281
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
282
	 */
283
	public function setPosition( $pos )
284
	{
285
		if( (int) $pos !== $this->getPosition() )
286
		{
287
			$this->values[$this->prefix . 'position'] = (int) $pos;
288
			$this->setModified();
289
		}
290
291
		return $this;
292
	}
293
294
295
	/**
296
	 * Returns the status of the list item.
297
	 *
298
	 * @return integer Status of the item
299
	 */
300
	public function getStatus()
301
	{
302
		if( isset( $this->values[$this->prefix . 'status'] ) ) {
303
			return (int) $this->values[$this->prefix . 'status'];
304
		}
305
306
		return 1;
307
	}
308
309
310
	/**
311
	 * Sets the new status of the list item.
312
	 *
313
	 * @param integer $status Status of the item
314
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
315
	 */
316
	public function setStatus( $status )
317
	{
318
		if( (int) $status !== $this->getStatus() )
319
		{
320
			$this->values[$this->prefix . 'status'] = (int) $status;
321
			$this->setModified();
322
		}
323
324
		return $this;
325
	}
326
327
328
	/**
329
	 * Returns the configuration of the list item.
330
	 *
331
	 * @return array Custom configuration values
332
	 */
333
	public function getConfig()
334
	{
335
		if( isset( $this->values[$this->prefix . 'config'] ) ) {
336
			return (array) $this->values[$this->prefix . 'config'];
337
		}
338
339
		return [];
340
	}
341
342
343
	/**
344
	 * Sets the new configuration for the list item.
345
	 *
346
	 * @param array $config Custom configuration values
347
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
348
	 */
349
	public function setConfig( array $config )
350
	{
351
		$this->values[$this->prefix . 'config'] = $config;
352
		$this->setModified();
353
354
		return $this;
355
	}
356
357
358
	/**
359
	 * Returns the referenced item if it's available.
360
	 *
361
	 * @return \Aimeos\MShop\Common\Item\Iface Referenced list item
362
	 */
363
	public function getRefItem()
364
	{
365
		return $this->refItem;
366
	}
367
368
369
	/**
370
	 * Stores the item referenced by the list item.
371
	 *
372
	 * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Item referenced by the list item or null for no reference
373
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
374
	 */
375
	public function setRefItem( \Aimeos\MShop\Common\Item\Iface $refItem = null )
376
	{
377
		$this->refItem = $refItem;
378
379
		return $this;
380
	}
381
382
383
	/**
384
	 * Returns the item type
385
	 *
386
	 * @return string Item type, subtypes are separated by slashes
387
	 */
388
	public function getResourceType()
389
	{
390
		return str_replace( '.', '/', rtrim( $this->prefix, '.' ) );
391
	}
392
393
394
	/**
395
	 * Tests if the item is available based on status, time, language and currency
396
	 *
397
	 * @return boolean True if available, false if not
398
	 */
399
	public function isAvailable()
400
	{
401
		return parent::isAvailable() && (bool) $this->getStatus()
402
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->values['date'] )
403
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->values['date'] );
404
	}
405
406
407
	/*
408
	 * Sets the item values from the given array and removes that entries from the list
409
	 *
410
	 * @param array &$list Associative list of item keys and their values
411
	 * @param boolean True to set private properties too, false for public only
412
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface List item for chaining method calls
413
	 */
414
	public function fromArray( array &$list, $private = false )
415
	{
416
		$item = parent::fromArray( $list, $private );
417
418
		foreach( $list as $key => $value )
419
		{
420
			switch( $key )
421
			{
422
				case $this->prefix . 'parentid': !$private ?: $item = $item->setParentId( $value ); break;
423
				case $this->prefix . 'type': $item = $item->setType( $value ); break;
424
				case $this->prefix . 'domain': $item = $item->setDomain( $value ); break;
425
				case $this->prefix . 'refid': $item = $item->setRefId( $value ); break;
0 ignored issues
show
Bug introduced by
The method setRefId() does not exist on Aimeos\MShop\Common\Item\Base. Did you maybe mean setId()? ( Ignorable by Annotation )

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

425
				case $this->prefix . 'refid': /** @scrutinizer ignore-call */ $item = $item->setRefId( $value ); break;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
426
				case $this->prefix . 'datestart': $item = $item->setDateStart( $value ); break;
427
				case $this->prefix . 'dateend': $item = $item->setDateEnd( $value ); break;
428
				case $this->prefix . 'config': $item = $item->setConfig( $value ); break;
429
				case $this->prefix . 'position': $item = $item->setPosition( $value ); break;
430
				case $this->prefix . 'status': $item = $item->setStatus( $value ); break;
431
				default: continue 2;
432
			}
433
434
			unset( $list[$key] );
435
		}
436
437
		return $item;
438
	}
439
440
441
	/**
442
	 * Returns the item values as array.
443
	 *
444
	 * @param boolean True to return private properties, false for public only
445
	 * @return array Associative list of item properties and their values
446
	 */
447
	public function toArray( $private = false )
448
	{
449
		$list = parent::toArray( $private );
450
451
		$list[$this->prefix . 'domain'] = $this->getDomain();
452
		$list[$this->prefix . 'refid'] = $this->getRefId();
453
		$list[$this->prefix . 'datestart'] = $this->getDateStart();
454
		$list[$this->prefix . 'dateend'] = $this->getDateEnd();
455
		$list[$this->prefix . 'config'] = $this->getConfig();
456
		$list[$this->prefix . 'position'] = $this->getPosition();
457
		$list[$this->prefix . 'status'] = $this->getStatus();
458
		$list[$this->prefix . 'type'] = $this->getType();
459
460
		if( $private === true )
461
		{
462
			$list[$this->prefix . 'key'] = $this->getKey();
463
			$list[$this->prefix . 'parentid'] = $this->getParentId();
464
		}
465
466
		return $list;
467
	}
468
469
}
470