Standard   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 45
eloc 65
dl 0
loc 350
rs 8.8
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getPosition() 0 3 1
A setDateEnd() 0 3 1
A setRefItem() 0 5 1
A setStatus() 0 3 1
A setRefId() 0 3 1
A getRefId() 0 3 1
A setConfig() 0 3 1
A getKey() 0 3 1
A setDomain() 0 3 1
A getDateStart() 0 4 2
A getStatus() 0 3 1
A getType() 0 3 1
A __construct() 0 6 2
A getDateEnd() 0 4 2
A setType() 0 3 1
A setParentId() 0 3 1
A setPosition() 0 3 1
A getParentId() 0 3 1
A getConfig() 0 3 1
A getRefItem() 0 3 1
A getDomain() 0 3 1
A setDateStart() 0 3 1
C fromArray() 0 24 12
A toArray() 0 20 2
A isAvailable() 0 5 6

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-2024
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 string $date;
29
	private string $prefix;
30
	private ?\Aimeos\MShop\Common\Item\Iface $refItem = null;
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( string $prefix, array $values = [] )
40
	{
41
		parent::__construct( $prefix, $values, str_replace( '.', '/', rtrim( $prefix, '.' ) ) );
42
43
		$this->date = isset( $values['.date'] ) ? $values['.date'] : date( 'Y-m-d H:i:s' );
44
		$this->prefix = (string) $prefix;
45
	}
46
47
48
	/**
49
	 * Returns the parent ID of the common list item,
50
	 * like the unique ID of a product or a tree node.
51
	 *
52
	 * @return string|null Parent ID of the common list item
53
	 */
54
	public function getParentId() : ?string
55
	{
56
		return $this->get( $this->prefix . 'parentid' );
57
	}
58
59
60
	/**
61
	 * Sets the parent ID of the common list item,
62
	 * like the unique ID of a product or a tree node.
63
	 *
64
	 * @param string|null $parentid New parent ID of the common list item
65
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
66
	 */
67
	public function setParentId( ?string $parentid ) : \Aimeos\MShop\Common\Item\Iface
68
	{
69
		return $this->set( $this->prefix . 'parentid', $parentid );
70
	}
71
72
73
	/**
74
	 * Returns the unique key of the list item
75
	 *
76
	 * @return string Unique key consisting of domain/type/refid
77
	 */
78
	public function getKey() : string
79
	{
80
		return $this->getDomain() . '|' . $this->getType() . '|' . $this->getRefId();
81
	}
82
83
84
	/**
85
	 * Returns the domain of the common list item, e.g. text or media.
86
	 *
87
	 * @return string Domain of the common list item
88
	 */
89
	public function getDomain() : string
90
	{
91
		return $this->get( $this->prefix . 'domain', '' );
92
	}
93
94
95
	/**
96
	 * Sets the new domain of the common list item, e.g. text od media.
97
	 *
98
	 * @param string $domain New domain of the common list item
99
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
100
	 */
101
	public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface
102
	{
103
		return $this->set( $this->prefix . 'domain', $domain );
104
	}
105
106
107
	/**
108
	 * Returns the reference id of the common list item, like the unique id
109
	 * of a text item or a media item.
110
	 *
111
	 * @return string Reference id of the common list item
112
	 */
113
	public function getRefId() : string
114
	{
115
		return $this->get( $this->prefix . 'refid', '' );
116
	}
117
118
119
	/**
120
	 * Sets the new reference id of the common list item, like the unique id
121
	 * of a text item or a media item.
122
	 *
123
	 * @param string $refid New reference id of the common list item
124
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
125
	 */
126
	public function setRefId( string $refid ) : \Aimeos\MShop\Common\Item\Lists\Iface
127
	{
128
		return $this->set( $this->prefix . 'refid', $refid );
129
	}
130
131
132
	/**
133
	 * Returns the start date of the common list item (YYYY-MM-DD hh:mm:ss).
134
	 *
135
	 * @return string|null Start date of the common list item (YYYY-MM-DD hh:mm:ss)
136
	 */
137
	public function getDateStart() : ?string
138
	{
139
		$value = $this->get( $this->prefix . 'datestart' );
140
		return $value ? substr( $value, 0, 19 ) : null;
141
	}
142
143
144
	/**
145
	 * Sets the new start date of the common list item (YYYY-MM-DD hh:mm:ss).
146
	 *
147
	 * @param string|null $date New start date of the common list item (YYYY-MM-DD hh:mm:ss)
148
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
149
	 */
150
	public function setDateStart( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
151
	{
152
		return $this->set( $this->prefix . 'datestart', $this->checkDateFormat( $date ) );
153
	}
154
155
156
	/**
157
	 * Returns the end date of the common list item (YYYY-MM-DD hh:mm:ss).
158
	 *
159
	 * @return string|null End date of the common list item (YYYY-MM-DD hh:mm:ss)
160
	 */
161
	public function getDateEnd() : ?string
162
	{
163
		$value = $this->get( $this->prefix . 'dateend' );
164
		return $value ? substr( $value, 0, 19 ) : null;
165
	}
166
167
168
	/**
169
	 * Sets the new end date of the common list item (YYYY-MM-DD hh:mm:ss).
170
	 *
171
	 * @param string|null $date New end date of the common list item (YYYY-MM-DD hh:mm:ss)
172
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
173
	 */
174
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
175
	{
176
		return $this->set( $this->prefix . 'dateend', $this->checkDateFormat( $date ) );
177
	}
178
179
180
	/**
181
	 * Returns the type of the list item.
182
	 *
183
	 * @return string Type of the list item
184
	 */
185
	public function getType() : string
186
	{
187
		return $this->get( $this->prefix . 'type', 'default' );
188
	}
189
190
191
192
	/**
193
	 * Sets the new type of the list item.
194
	 *
195
	 * @param string $type type of the list item
196
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
197
	 */
198
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
199
	{
200
		return $this->set( $this->prefix . 'type', $this->checkCode( $type ) );
201
	}
202
203
204
	/**
205
	 * Returns the position of the list item.
206
	 *
207
	 * @return int Position of the list item
208
	 */
209
	public function getPosition() : int
210
	{
211
		return $this->get( $this->prefix . 'position', 0 );
212
	}
213
214
215
	/**
216
	 * Sets the new position of the list item.
217
	 *
218
	 * @param int $pos position of the list item
219
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
220
	 */
221
	public function setPosition( int $pos ) : \Aimeos\MShop\Common\Item\Iface
222
	{
223
		return $this->set( $this->prefix . 'position', $pos );
224
	}
225
226
227
	/**
228
	 * Returns the status of the list item.
229
	 *
230
	 * @return int Status of the item
231
	 */
232
	public function getStatus() : int
233
	{
234
		return $this->get( $this->prefix . 'status', 1 );
235
	}
236
237
238
	/**
239
	 * Sets the new status of the list item.
240
	 *
241
	 * @param int $status Status of the item
242
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
243
	 */
244
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
245
	{
246
		return $this->set( $this->prefix . 'status', $status );
247
	}
248
249
250
	/**
251
	 * Returns the configuration of the list item.
252
	 *
253
	 * @return array Custom configuration values
254
	 */
255
	public function getConfig() : array
256
	{
257
		return $this->get( $this->prefix . 'config', [] );
258
	}
259
260
261
	/**
262
	 * Sets the new configuration for the list item.
263
	 *
264
	 * @param array $config Custom configuration values
265
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
266
	 */
267
	public function setConfig( array $config ) : \Aimeos\MShop\Common\Item\Iface
268
	{
269
		return $this->set( $this->prefix . 'config', $config );
270
	}
271
272
273
	/**
274
	 * Returns the referenced item if it's available.
275
	 *
276
	 * @return \Aimeos\MShop\Common\Item\Iface|null Referenced list item
277
	 */
278
	public function getRefItem() : ?\Aimeos\MShop\Common\Item\Iface
279
	{
280
		return $this->refItem;
281
	}
282
283
284
	/**
285
	 * Stores the item referenced by the list item.
286
	 *
287
	 * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Item referenced by the list item or null for no reference
288
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
289
	 */
290
	public function setRefItem( ?\Aimeos\MShop\Common\Item\Iface $refItem ) : \Aimeos\MShop\Common\Item\Lists\Iface
291
	{
292
		$this->refItem = $refItem;
293
294
		return $this;
295
	}
296
297
298
	/**
299
	 * Tests if the item is available based on status, time, language and currency
300
	 *
301
	 * @return boolean True if available, false if not
302
	 */
303
	public function isAvailable() : bool
304
	{
305
		return parent::isAvailable() && $this->getStatus() > 0
306
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->date )
307
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->date );
308
	}
309
310
311
	/*
312
	 * Sets the item values from the given array and removes that entries from the list
313
	 *
314
	 * @param array &$list Associative list of item keys and their values
315
	 * @param bool True to set private properties too, false for public only
316
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface List item for chaining method calls
317
	 */
318
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
319
	{
320
		$item = parent::fromArray( $list, $private );
321
322
		foreach( $list as $key => $value )
323
		{
324
			switch( $key )
325
			{
326
				case $this->prefix . 'parentid': !$private ?: $item = $item->setParentId( $value ); break;
327
				case $this->prefix . 'domain': $item = $item->setDomain( $value ); break;
328
				case $this->prefix . 'type': $item = $item->setType( $value ); break;
329
				case $this->prefix . 'refid': $item = $item->setRefId( $value ); break;
330
				case $this->prefix . 'datestart': $item = $item->setDateStart( $value ); break;
331
				case $this->prefix . 'dateend': $item = $item->setDateEnd( $value ); break;
332
				case $this->prefix . 'status': $item = $item->setStatus( (int) $value ); break;
333
				case $this->prefix . 'config': $item = $item->setConfig( (array) $value ); break;
334
				case $this->prefix . 'position': $item = $item->setPosition( (int) $value ); break;
335
				default: continue 2;
336
			}
337
338
			unset( $list[$key] );
339
		}
340
341
		return $item;
342
	}
343
344
345
	/**
346
	 * Returns the item values as array.
347
	 *
348
	 * @param bool True to return private properties, false for public only
349
	 * @return array Associative list of item properties and their values
350
	 */
351
	public function toArray( bool $private = false ) : array
352
	{
353
		$list = parent::toArray( $private );
354
355
		$list[$this->prefix . 'domain'] = $this->getDomain();
356
		$list[$this->prefix . 'refid'] = $this->getRefId();
357
		$list[$this->prefix . 'datestart'] = $this->getDateStart();
358
		$list[$this->prefix . 'dateend'] = $this->getDateEnd();
359
		$list[$this->prefix . 'config'] = $this->getConfig();
360
		$list[$this->prefix . 'position'] = $this->getPosition();
361
		$list[$this->prefix . 'status'] = $this->getStatus();
362
		$list[$this->prefix . 'type'] = $this->getType();
363
364
		if( $private === true )
365
		{
366
			$list[$this->prefix . 'key'] = $this->getKey();
367
			$list[$this->prefix . 'parentid'] = $this->getParentId();
368
		}
369
370
		return $list;
371
	}
372
373
}
374