Passed
Push — master ( be7e20...ee21b5 )
by Aimeos
05:56
created

Standard   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
c 0
b 0
f 0
dl 0
loc 317
rs 9.2
wmc 40

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 3 1
A setConfig() 0 3 1
A setLabel() 0 3 1
A __construct() 0 6 2
A setCode() 0 3 1
A toArray() 0 15 1
A getType() 0 3 1
A setType() 0 3 1
A getConfig() 0 3 1
A setDateStart() 0 3 1
A getLabel() 0 3 1
A getDateEnd() 0 3 1
A getResourceType() 0 3 1
A setProvider() 0 7 2
A isAvailable() 0 5 6
A setStatus() 0 3 1
A getPosition() 0 3 1
A setPosition() 0 3 1
A getDateStart() 0 3 1
A getStatus() 0 3 1
B fromArray() 0 24 11
A setDateEnd() 0 3 1
A getProvider() 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, 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 Service
9
 */
10
11
12
namespace Aimeos\MShop\Service\Item;
13
14
15
/**
16
 * Service item with common methods.
17
 *
18
 * @package MShop
19
 * @subpackage Service
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
23
	implements \Aimeos\MShop\Service\Item\Iface
24
{
25
	use \Aimeos\MShop\Common\Item\Config\Traits;
26
	use \Aimeos\MShop\Common\Item\ListRef\Traits;
27
28
29
	private $date;
30
31
32
	/**
33
	 * Initializes the item object.
34
	 *
35
	 * @param array $values Parameter for initializing the basic properties
36
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
37
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
38
	 */
39
	public function __construct( array $values = [], array $listItems = [], array $refItems = [] )
40
	{
41
		parent::__construct( 'service.', $values );
42
43
		$this->date = ( isset( $values['.date'] ) ? $values['.date'] : null );
44
		$this->initListItems( $listItems, $refItems );
45
	}
46
47
48
	/**
49
	 * Returns the code of the service item if available
50
	 *
51
	 * @return string Service item code
52
	 */
53
	public function getCode()
54
	{
55
		return (string) $this->get( 'service.code', '' );
56
	}
57
58
59
	/**
60
	 * Sets the code of the service item
61
	 *
62
	 * @param string $code Code of the service item
63
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
64
	 */
65
	public function setCode( $code )
66
	{
67
		return $this->set( 'service.code', $this->checkCode( $code ) );
68
	}
69
70
71
	/**
72
	 * Returns the type of the service item if available.
73
	 *
74
	 * @return string|null Service item type
75
	 */
76
	public function getType()
77
	{
78
		return $this->get( 'service.type' );
79
	}
80
81
82
	/**
83
	 * Sets the type of the service item.
84
	 *
85
	 * @param string $type Type of the service item
86
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
87
	 */
88
	public function setType( $type )
89
	{
90
		return $this->set( 'service.type', $this->checkCode( $type ) );
91
	}
92
93
94
	/**
95
	 * Returns the name of the service provider the item belongs to.
96
	 *
97
	 * @return string Name of the service provider
98
	 */
99
	public function getProvider()
100
	{
101
		return (string) $this->get( 'service.provider', '' );
102
	}
103
104
105
	/**
106
	 * Sets the new name of the service provider the item belongs to.
107
	 *
108
	 * @param string $provider Name of the service provider
109
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
110
	 */
111
	public function setProvider( $provider )
112
	{
113
		if( preg_match( '/^[A-Za-z0-9]+(,[A-Za-z0-9]+)*$/', $provider ) !== 1 ) {
114
			throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid provider name "%1$s"', $provider ) );
115
		}
116
117
		return $this->set( 'service.provider', (string) $provider );
118
	}
119
120
121
	/**
122
	 * Returns the label of the service item if available.
123
	 *
124
	 * @return string Service item label
125
	 */
126
	public function getLabel()
127
	{
128
		return (string) $this->get( 'service.label', '' );
129
	}
130
131
132
	/**
133
	 * Sets the label of the service item
134
	 *
135
	 * @param string $label Label of the service item
136
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
137
	 */
138
	public function setLabel( $label )
139
	{
140
		return $this->set( 'service.label', (string) $label );
141
	}
142
143
144
	/**
145
	 * Returns the starting point of time, in which the service is available.
146
	 *
147
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
148
	 */
149
	public function getDateStart()
150
	{
151
		return $this->get( 'service.datestart' );
152
	}
153
154
155
	/**
156
	 * Sets a new starting point of time, in which the service is available.
157
	 *
158
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
159
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
160
	 */
161
	public function setDateStart( $date )
162
	{
163
		return $this->set( 'service.datestart', $this->checkDateFormat( $date ) );
164
	}
165
166
167
	/**
168
	 * Returns the ending point of time, in which the service is available.
169
	 *
170
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
171
	 */
172
	public function getDateEnd()
173
	{
174
		return $this->get( 'service.dateend' );
175
	}
176
177
178
	/**
179
	 * Sets a new ending point of time, in which the service is available.
180
	 *
181
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
182
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
183
	 */
184
	public function setDateEnd( $date )
185
	{
186
		return $this->set( 'service.dateend', $this->checkDateFormat( $date ) );
187
	}
188
189
190
	/**
191
	 * Returns the configuration values of the item
192
	 *
193
	 * @return array Configuration values
194
	 */
195
	public function getConfig()
196
	{
197
		return (array) $this->get( 'service.config', [] );
198
	}
199
200
201
	/**
202
	 * Sets the configuration values of the item.
203
	 *
204
	 * @param array $config Configuration values
205
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
206
	 */
207
	public function setConfig( array $config )
208
	{
209
		return $this->set( 'service.config', $config );
210
	}
211
212
213
	/**
214
	 * Returns the position of the service item in the list of deliveries.
215
	 *
216
	 * @return integer Position in item list
217
	 */
218
	public function getPosition()
219
	{
220
		return (int) $this->get( 'service.position', 0 );
221
	}
222
223
224
	/**
225
	 * Sets the new position of the service item in the list of deliveries.
226
	 *
227
	 * @param integer $pos Position in item list
228
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
229
	 */
230
	public function setPosition( $pos )
231
	{
232
		return $this->set( 'service.position', (int) $pos );
233
	}
234
235
236
	/**
237
	 * Returns the status of the item.
238
	 *
239
	 * @return integer Status of the item
240
	 */
241
	public function getStatus()
242
	{
243
		return (int) $this->get( 'service.status', 1 );
244
	}
245
246
247
	/**
248
	 * Sets the status of the item.
249
	 *
250
	 * @param integer $status Status of the item
251
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
252
	 */
253
	public function setStatus( $status )
254
	{
255
		return $this->set( 'service.status', (int) $status );
256
	}
257
258
259
	/**
260
	 * Returns the item type
261
	 *
262
	 * @return string Item type, subtypes are separated by slashes
263
	 */
264
	public function getResourceType()
265
	{
266
		return 'service';
267
	}
268
269
270
	/**
271
	 * Tests if the item is available based on status, time, language and currency
272
	 *
273
	 * @return boolean True if available, false if not
274
	 */
275
	public function isAvailable()
276
	{
277
		return parent::isAvailable() && $this->getStatus() > 0
278
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->date )
279
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->date );
280
	}
281
282
283
	/*
284
	 * Sets the item values from the given array and removes that entries from the list
285
	 *
286
	 * @param array &$list Associative list of item keys and their values
287
	 * @param boolean True to set private properties too, false for public only
288
	 * @return \Aimeos\MShop\Service\Item\Iface Service item for chaining method calls
289
	 */
290
	public function fromArray( array &$list, $private = false )
291
	{
292
		$item = parent::fromArray( $list, $private );
293
294
		foreach( $list as $key => $value )
295
		{
296
			switch( $key )
297
			{
298
				case 'service.type': $item = $item->setType( $value ); break;
299
				case 'service.code': $item = $item->setCode( $value ); break;
300
				case 'service.label': $item = $item->setLabel( $value ); break;
301
				case 'service.provider': $item = $item->setProvider( $value ); break;
302
				case 'service.position': $item = $item->setPosition( $value ); break;
303
				case 'service.datestart': $item = $item->setDateStart( $value ); break;
304
				case 'service.dateend': $item = $item->setDateEnd( $value ); break;
305
				case 'service.config': $item = $item->setConfig( $value ); break;
306
				case 'service.status': $item = $item->setStatus( $value ); break;
307
				default: continue 2;
308
			}
309
310
			unset( $list[$key] );
311
		}
312
313
		return $item;
314
	}
315
316
317
	/**
318
	 * Returns the item values as array.
319
	 *
320
	 * @param boolean True to return private properties, false for public only
321
	 * @return array Associative list of item properties and their values
322
	 */
323
	public function toArray( $private = false )
324
	{
325
		$list = parent::toArray( $private );
326
327
		$list['service.type'] = $this->getType();
328
		$list['service.code'] = $this->getCode();
329
		$list['service.label'] = $this->getLabel();
330
		$list['service.provider'] = $this->getProvider();
331
		$list['service.position'] = $this->getPosition();
332
		$list['service.datestart'] = $this->getDateStart();
333
		$list['service.dateend'] = $this->getDateEnd();
334
		$list['service.config'] = $this->getConfig();
335
		$list['service.status'] = $this->getStatus();
336
337
		return $list;
338
	}
339
340
}
341