Passed
Push — master ( bb09b0...e78dfc )
by Aimeos
05:26
created

Standard::fromArray()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
rs 6.9666
c 0
b 0
f 0
cc 12
nc 11
nop 2

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), 2018-2022
6
 * @package MShop
7
 * @subpackage Subscription
8
 */
9
10
11
namespace Aimeos\MShop\Subscription\Item;
12
13
14
/**
15
 * Default implementation of subscription item
16
 *
17
 * @package MShop
18
 * @subpackage Subscription
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Subscription\Item\Iface
23
{
24
	private $baseItem;
25
26
27
	/**
28
	 * Initializes the object with the given values.
29
	 *
30
	 * @param array $values Associative list of values from database
31
	 * @param \Aimeos\MShop\Order\Item\Base\Iface|null $baseItem Order basket if available
32
	 */
33
	public function __construct( array $values = [], ?\Aimeos\MShop\Order\Item\Base\Iface $baseItem = null )
34
	{
35
		parent::__construct( 'subscription.', $values );
36
		$this->baseItem = $baseItem;
37
	}
38
39
40
	/**
41
	 * Returns the associated order base item
42
	 *
43
	 * @return \Aimeos\MShop\Order\Item\Base\Iface|null Order base item
44
	 */
45
	public function getBaseItem() : ?\Aimeos\MShop\Order\Item\Base\Iface
46
	{
47
		return $this->baseItem;
48
	}
49
50
51
	/**
52
	 * Returns the ID of the base order
53
	 *
54
	 * @return string|null ID of the base order
55
	 */
56
	public function getOrderBaseId() : ?string
57
	{
58
		return $this->get( 'subscription.ordbaseid' );
59
	}
60
61
62
	/**
63
	 * Sets the ID of the base order item which the customer bought
64
	 *
65
	 * @param string $id ID of the base order
66
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
67
	 */
68
	public function setOrderBaseId( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
69
	{
70
		return $this->set( 'subscription.ordbaseid', $id );
71
	}
72
73
74
	/**
75
	 * Returns the ID of the ordered product
76
	 *
77
	 * @return string|null ID of the ordered product
78
	 */
79
	public function getOrderProductId() : ?string
80
	{
81
		return $this->get( 'subscription.ordprodid' );
82
	}
83
84
85
	/**
86
	 * Sets the ID of the ordered product item which the customer subscribed for
87
	 *
88
	 * @param string $id ID of the ordered product
89
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
90
	 */
91
	public function setOrderProductId( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
92
	{
93
		return $this->set( 'subscription.ordprodid', $id );
94
	}
95
96
97
	/**
98
	 * Returns the date of the next subscription renewal
99
	 *
100
	 * @return string|null ISO date in "YYYY-MM-DD HH:mm:ss" format
101
	 */
102
	public function getDateNext() : ?string
103
	{
104
		$value = $this->get( 'subscription.datenext' );
105
		return $value ? substr( $value, 0, 19 ) : null;
106
	}
107
108
109
	/**
110
	 * Sets the date of the next subscription renewal
111
	 *
112
	 * @param string $date ISO date in "YYYY-MM-DD HH:mm:ss" format
113
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
114
	 */
115
	public function setDateNext( string $date ) : \Aimeos\MShop\Subscription\Item\Iface
116
	{
117
		return $this->set( 'subscription.datenext', $this->checkDateFormat( $date ) );
118
	}
119
120
121
	/**
122
	 * Returns the date when the subscription renewal ends
123
	 *
124
	 * @return string|null ISO date in "YYYY-MM-DD HH:mm:ss" format
125
	 */
126
	public function getDateEnd() : ?string
127
	{
128
		$value = $this->get( 'subscription.dateend' );
129
		return $value ? substr( $value, 0, 19 ) : null;
130
	}
131
132
133
	/**
134
	 * Sets the delivery date of the invoice.
135
	 *
136
	 * @param string|null $date ISO date in "YYYY-MM-DD HH:mm:ss" format
137
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
138
	 */
139
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Subscription\Item\Iface
140
	{
141
		return $this->set( 'subscription.dateend', $this->checkDateFormat( $date ) );
142
	}
143
144
145
	/**
146
	 * Returns the time interval to pass between the subscription renewals
147
	 *
148
	 * @return string PHP time interval, e.g. "P1M2W"
149
	 */
150
	public function getInterval() : string
151
	{
152
		return $this->get( 'subscription.interval', '' );
153
	}
154
155
156
	/**
157
	 * Sets the time interval to pass between the subscription renewals
158
	 *
159
	 * @param string $value PHP time interval, e.g. "P1M2W"
160
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
161
	 */
162
	public function setInterval( string $value ) : \Aimeos\MShop\Subscription\Item\Iface
163
	{
164
		if( strlen( $value ) > 1 && preg_match( '/^P([0-9]+Y)?([0-9]+M)?([0-9]+W)?([0-9]+D)?(T?[0-9]+H)?$/', $value ) !== 1 ) {
165
			throw new \Aimeos\MShop\Subscription\Exception( sprintf( 'Invalid time interval format "%1$s"', $value ) );
166
		}
167
168
		return $this->set( 'subscription.interval', $value );
169
	}
170
171
172
	/**
173
	 * Returns the current renewal period of the subscription product
174
	 *
175
	 * @return int Current renewal period
176
	 */
177
	public function getPeriod() : int
178
	{
179
		return $this->get( 'subscription.period', 1 );
180
	}
181
182
183
	/**
184
	 * Sets the current renewal period of the subscription product
185
	 *
186
	 * @param int $value Current renewal period
187
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
188
	 */
189
	public function setPeriod( int $value ) : \Aimeos\MShop\Subscription\Item\Iface
190
	{
191
		return $this->set( 'subscription.period', $value );
192
	}
193
194
195
	/**
196
	 * Returns the product ID of the subscription product
197
	 *
198
	 * @return string Product ID
199
	 */
200
	public function getProductId() : string
201
	{
202
		return $this->get( 'subscription.productid', '' );
203
	}
204
205
206
	/**
207
	 * Sets the product ID of the subscription product
208
	 *
209
	 * @param string $value Product ID
210
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
211
	 */
212
	public function setProductId( string $value ) : \Aimeos\MShop\Subscription\Item\Iface
213
	{
214
		return $this->set( 'subscription.productid', $value );
215
	}
216
217
218
	/**
219
	 * Returns the reason for the end of the subscriptions
220
	 *
221
	 * @return int|null Reason code or NULL for no reason
222
	 */
223
	public function getReason() : ?int
224
	{
225
		return $this->get( 'subscription.reason' );
226
	}
227
228
229
	/**
230
	 * Sets the reason for the end of the subscriptions
231
	 *
232
	 * @param int|null $value Reason code or NULL for no reason
233
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
234
	 */
235
	public function setReason( ?int $value ) : \Aimeos\MShop\Subscription\Item\Iface
236
	{
237
		return $this->set( 'subscription.reason', $value );
238
	}
239
240
241
	/**
242
	 * Returns the status of the subscriptions
243
	 *
244
	 * @return int Subscription status, i.e. "1" for enabled, "0" for disabled
245
	 */
246
	public function getStatus() : int
247
	{
248
		return $this->get( 'subscription.status', 1 );
249
	}
250
251
252
	/**
253
	 * Sets the status of the subscriptions
254
	 *
255
	 * @return int Subscription status, i.e. "1" for enabled, "0" for disabled
256
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
257
	 */
258
	public function setStatus( int $status ) : \Aimeos\MShop\Subscription\Item\Iface
259
	{
260
		return $this->set( 'subscription.status', $status );
261
	}
262
263
264
	/**
265
	 * Returns the item type
266
	 *
267
	 * @return string Item type, subtypes are separated by slashes
268
	 */
269
	public function getResourceType() : string
270
	{
271
		return 'subscription';
272
	}
273
274
275
	/*
276
	 * Sets the item values from the given array and removes that entries from the list
277
	 *
278
	 * @param array &$list Associative list of item keys and their values
279
	 * @param bool True to set private properties too, false for public only
280
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
281
	 */
282
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
283
	{
284
		$item = parent::fromArray( $list, $private );
285
286
		foreach( $list as $key => $value )
287
		{
288
			switch( $key )
289
			{
290
				case 'subscription.ordbaseid': $item = $item->setOrderBaseId( $value ); break;
291
				case 'subscription.ordprodid': $item = $item->setOrderProductId( $value ); break;
292
				case 'subscription.productid': $item = $item->setProductId( $value ); break;
293
				case 'subscription.datenext': $item = $item->setDateNext( $value ); break;
294
				case 'subscription.dateend': $item = $item->setDateEnd( $value ); break;
295
				case 'subscription.interval': $item = $item->setInterval( $value ); break;
296
				case 'subscription.period': $item = $item->setPeriod( (int) $value ); break;
297
				case 'subscription.status': $item = $item->setStatus( (int) $value ); break;
298
				case 'subscription.reason': $item = $item->setReason( $value !== null ? (int) $value : null ); break;
299
				default: continue 2;
300
			}
301
302
			unset( $list[$key] );
303
		}
304
305
		return $item;
306
	}
307
308
309
	/**
310
	 * Returns the item values as associative list.
311
	 *
312
	 * @param bool True to return private properties, false for public only
313
	 * @return array Associative list of item properties and their values
314
	 */
315
	public function toArray( bool $private = false ) : array
316
	{
317
		$list = parent::toArray( $private );
318
319
		$list['subscription.ordbaseid'] = $this->getOrderBaseId();
320
		$list['subscription.ordprodid'] = $this->getOrderProductId();
321
		$list['subscription.productid'] = $this->getProductId();
322
		$list['subscription.datenext'] = $this->getDateNext();
323
		$list['subscription.dateend'] = $this->getDateEnd();
324
		$list['subscription.interval'] = $this->getInterval();
325
		$list['subscription.period'] = $this->getPeriod();
326
		$list['subscription.status'] = $this->getStatus();
327
		$list['subscription.reason'] = $this->getReason();
328
329
		return $list;
330
	}
331
}
332