Completed
Push — master ( ab8a43...3303a4 )
by Aimeos
11:36
created

Standard   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 252
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getParentId() 0 6 2
A setParentId() 0 10 2
A getCode() 0 6 2
A setCode() 0 10 2
A getCount() 0 8 2
A setCount() 0 10 2
A getDateStart() 0 6 2
A setDateStart() 0 10 2
A getDateEnd() 0 6 2
A setDateEnd() 0 10 2
A getResourceType() 0 4 1
B isAvailable() 0 7 5
B fromArray() 0 20 7
A toArray() 0 15 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Coupon
9
 */
10
11
12
namespace Aimeos\MShop\Coupon\Item\Code;
13
14
15
/**
16
 * Default coupon code implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Coupon
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Coupon\Item\Code\Iface
24
{
25
	private $values;
26
27
	/**
28
	 * Initializes the coupon code instance
29
	 *
30
	 * @param array $values Associative array with ID, coupon item ID, code and counter
31
	 */
32
	public function __construct( array $values = [] )
33
	{
34
		parent::__construct( 'coupon.code.', $values );
35
36
		$this->values = $values;
37
	}
38
39
40
	/**
41
	 * Returns the unique ID of the coupon item the code belongs to.
42
	 *
43
	 * @return string|null Unique ID of the coupon item
44
	 */
45
	public function getParentId()
46
	{
47
		if( isset( $this->values['coupon.code.parentid'] ) ) {
48
			return (string) $this->values['coupon.code.parentid'];
49
		}
50
	}
51
52
53
	/**
54
	 * Sets the new unique ID of the coupon item the code belongs to.
55
	 *
56
	 * @param string $id Unique ID of the coupon item
57
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
58
	 */
59
	public function setParentId( $id )
60
	{
61
		if( (string) $id !== $this->getParentId() )
62
		{
63
			$this->values['coupon.code.parentid'] = (string) $id;
64
			$this->setModified();
65
		}
66
67
		return $this;
68
	}
69
70
71
	/**
72
	 * Returns the code of the coupon item.
73
	 *
74
	 * @return string|null Coupon code
75
	 */
76
	public function getCode()
77
	{
78
		if( isset( $this->values['coupon.code.code'] ) ) {
79
			return (string) $this->values['coupon.code.code'];
80
		}
81
	}
82
83
84
	/**
85
	 * Sets the new code for the coupon item.
86
	 *
87
	 * @param string $code Coupon code
88
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
89
	 */
90
	public function setCode( $code )
91
	{
92
		if( (string) $code !== $this->getCode() )
93
		{
94
			$this->values['coupon.code.code'] = $this->checkCode( $code );
95
			$this->setModified();
96
		}
97
98
		return $this;
99
	}
100
101
102
	/**
103
	 * Returns the number of tries the code is valid.
104
	 *
105
	 * @return integer Number of available tries
106
	 */
107
	public function getCount()
108
	{
109
		if( isset( $this->values['coupon.code.count'] ) ) {
110
			return (int) $this->values['coupon.code.count'];
111
		}
112
113
		return 0;
114
	}
115
116
117
	/**
118
	 * Sets the new number of tries the code is valid.
119
	 *
120
	 * @param integer $count Number of tries
121
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
122
	 */
123
	public function setCount( $count )
124
	{
125
		if( (int) $count !== $this->getCount() )
126
		{
127
			$this->values['coupon.code.count'] = (int) $count;
128
			$this->setModified();
129
		}
130
131
		return $this;
132
	}
133
134
135
	/**
136
	 * Returns the starting point of time, in which the code is available.
137
	 *
138
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
139
	 */
140
	public function getDateStart()
141
	{
142
		if( isset( $this->values['coupon.code.datestart'] ) ) {
143
			return (string) $this->values['coupon.code.datestart'];
144
		}
145
	}
146
147
148
	/**
149
	 * Sets a new starting point of time, in which the code is available.
150
	 *
151
	 * @param string New ISO date in YYYY-MM-DD hh:mm:ss format
152
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
153
	 */
154
	public function setDateStart( $date )
155
	{
156
		if( $date !== $this->getDateStart() )
157
		{
158
			$this->values['coupon.code.datestart'] = $this->checkDateFormat( $date );
159
			$this->setModified();
160
		}
161
162
		return $this;
163
	}
164
165
166
	/**
167
	 * Returns the ending point of time, in which the code is available.
168
	 *
169
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
170
	 */
171
	public function getDateEnd()
172
	{
173
		if( isset( $this->values['coupon.code.dateend'] ) ) {
174
			return (string) $this->values['coupon.code.dateend'];
175
		}
176
	}
177
178
179
	/**
180
	 * Sets a new ending point of time, in which the code is available.
181
	 *
182
	 * @param string New ISO date in YYYY-MM-DD hh:mm:ss format
183
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
184
	 */
185
	public function setDateEnd( $date )
186
	{
187
		if( (string) $date !== $this->getDateEnd() )
188
		{
189
			$this->values['coupon.code.dateend'] = $this->checkDateFormat( $date );
190
			$this->setModified();
191
		}
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the item type
199
	 *
200
	 * @return string Item type, subtypes are separated by slashes
201
	 */
202
	public function getResourceType()
203
	{
204
		return 'coupon/code';
205
	}
206
207
208
	/**
209
	 * Tests if the item is available based on status, time, language and currency
210
	 *
211
	 * @return boolean True if available, false if not
212
	 */
213
	public function isAvailable()
214
	{
215
		return parent::isAvailable()
216
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->values['date'] )
217
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->values['date'] );
218
219
	}
220
221
222
	/**
223
	 * Sets the item values from the given array.
224
	 *
225
	 * @param array $list Associative list of item keys and their values
226
	 * @return array Associative list of keys and their values that are unknown
227
	 */
228
	public function fromArray( array $list )
229
	{
230
		$unknown = [];
231
		$list = parent::fromArray( $list );
232
233
		foreach( $list as $key => $value )
234
		{
235
			switch( $key )
236
			{
237
				case 'coupon.code.count': $this->setCount( $value ); break;
238
				case 'coupon.code.code': $this->setCode( $value ); break;
239
				case 'coupon.code.parentid': $this->setParentId( $value ); break;
240
				case 'coupon.code.datestart': $this->setDateStart( $value ); break;
241
				case 'coupon.code.dateend': $this->setDateEnd( $value ); break;
242
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
243
			}
244
		}
245
246
		return $unknown;
247
	}
248
249
250
	/**
251
	 * Returns the item values as array.
252
	 *
253
	 * @param boolean True to return private properties, false for public only
254
	 * @return array Associative list of item properties and their values
255
	 */
256
	public function toArray( $private = false )
257
	{
258
		$list = parent::toArray( $private );
259
260
		$list['coupon.code.code'] = $this->getCode();
261
		$list['coupon.code.count'] = $this->getCount();
262
		$list['coupon.code.datestart'] = $this->getDateStart();
263
		$list['coupon.code.dateend'] = $this->getDateEnd();
264
265
		if( $private === true ) {
266
			$list['coupon.code.parentid'] = $this->getParentId();
267
		}
268
269
		return $list;
270
	}
271
272
}
273