Passed
Push — master ( 5d05b9...5af5d7 )
by Aimeos
05:15
created

Standard::fromArray()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 14
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 23
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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