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

Standard   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 292
rs 9.68
wmc 34

22 Methods

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