Standard   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 65
dl 0
loc 341
rs 8.8798
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
C fromArray() 0 24 12
A getPosition() 0 3 1
A setDateEnd() 0 3 1
A toArray() 0 20 2
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 isAvailable() 0 5 6
A getDateStart() 0 4 2
A getType() 0 3 1
A __construct() 0 6 2
A getStatus() 0 3 1
A getDateEnd() 0 4 2
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

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