Passed
Push — master ( 9f1c00...0115e8 )
by Aimeos
05:45
created

Standard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Coupon
8
 */
9
10
11
namespace Aimeos\MShop\Coupon\Item\Code;
12
13
14
/**
15
 * Default coupon code implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Coupon
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Coupon\Item\Code\Iface
23
{
24
	/**
25
	 * Returns the unique ID of the coupon item the code belongs to.
26
	 *
27
	 * @return string|null Unique ID of the coupon item
28
	 */
29
	public function getParentId() : ?string
30
	{
31
		return $this->get( 'coupon.code.parentid' );
32
	}
33
34
35
	/**
36
	 * Sets the new unique ID of the coupon item the code belongs to.
37
	 *
38
	 * @param string|null $id Unique ID of the coupon item
39
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
40
	 */
41
	public function setParentId( ?string $id ) : \Aimeos\MShop\Common\Item\Iface
42
	{
43
		return $this->set( 'coupon.code.parentid', $id );
44
	}
45
46
47
	/**
48
	 * Returns the code of the coupon item.
49
	 *
50
	 * @return string|null Coupon code
51
	 */
52
	public function getCode() : ?string
53
	{
54
		return $this->get( 'coupon.code.code' );
55
	}
56
57
58
	/**
59
	 * Sets the new code for the coupon item.
60
	 *
61
	 * @param string $code Coupon code
62
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
63
	 */
64
	public function setCode( string $code ) : \Aimeos\MShop\Coupon\Item\Code\Iface
65
	{
66
		return $this->set( 'coupon.code.code', $this->checkCode( $code ) );
67
	}
68
69
70
	/**
71
	 * Returns the number of tries the code is valid.
72
	 *
73
	 * @return int|null Number of available tries or null for unlimited
74
	 */
75
	public function getCount() : ?int
76
	{
77
		if( ( $result = $this->get( 'coupon.code.count', 0 ) ) !== null ) {
78
			return $result;
79
		}
80
81
		return null;
82
	}
83
84
85
	/**
86
	 * Sets the new number of tries the code is valid.
87
	 *
88
	 * @param int|null $count Number of tries or null for unlimited
89
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
90
	 */
91
	public function setCount( $count = null ) : \Aimeos\MShop\Coupon\Item\Code\Iface
92
	{
93
		return $this->set( 'coupon.code.count', is_numeric( $count ) ? (int) $count : null );
94
	}
95
96
97
	/**
98
	 * Returns the starting point of time, in which the code is available.
99
	 *
100
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
101
	 */
102
	public function getDateStart() : ?string
103
	{
104
		$value = $this->get( 'coupon.code.datestart' );
105
		return $value ? substr( $value, 0, 19 ) : null;
106
	}
107
108
109
	/**
110
	 * Sets a new starting point of time, in which the code is available.
111
	 *
112
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
113
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
114
	 */
115
	public function setDateStart( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
116
	{
117
		return $this->set( 'coupon.code.datestart', $this->checkDateFormat( $date ) );
118
	}
119
120
121
	/**
122
	 * Returns the ending point of time, in which the code is available.
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( 'coupon.code.dateend' );
129
		return $value ? substr( $value, 0, 19 ) : null;
130
	}
131
132
133
	/**
134
	 * Sets a new ending point of time, in which the code is available.
135
	 *
136
	 * @param string|null New ISO date in YYYY-MM-DD hh:mm:ss format
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Coupon\Item\Code\New was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
137
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
138
	 */
139
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
140
	{
141
		return $this->set( 'coupon.code.dateend', $this->checkDateFormat( $date ) );
142
	}
143
144
145
	/**
146
	 * Returns reference for the coupon code
147
	 * This can be an arbitrary value used by the coupon provider
148
	 *
149
	 * @return string Arbitrary value depending on the coupon provider
150
	 */
151
	public function getRef() : string
152
	{
153
		return $this->get( 'coupon.code.ref', '' );
154
	}
155
156
157
	/**
158
	 * Sets the new reference for the coupon code
159
	 * This can be an arbitrary value used by the coupon provider
160
	 *
161
	 * @param string|null $ref Arbitrary value depending on the coupon provider
162
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
163
	 */
164
	public function setRef( ?string $ref ) : \Aimeos\MShop\Coupon\Item\Code\Iface
165
	{
166
		return $this->set( 'coupon.code.ref', (string) $ref );
167
	}
168
169
170
	/**
171
	 * Tests if the item is available based on status, time, language and currency
172
	 *
173
	 * @return bool True if available, false if not
174
	 */
175
	public function isAvailable() : bool
176
	{
177
		$date = $this->get( '.date' );
178
179
		return parent::isAvailable()
180
			&& ( $this->getDateStart() === null || $this->getDateStart() < $date )
181
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $date );
182
	}
183
184
185
	/*
186
	 * Sets the item values from the given array and removes that entries from the list
187
	 *
188
	 * @param array &$list Associative list of item keys and their values
189
	 * @param bool True to set private properties too, false for public only
190
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface Coupon code item for chaining method calls
191
	 */
192
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
193
	{
194
		$item = parent::fromArray( $list, $private );
195
196
		foreach( $list as $key => $value )
197
		{
198
			switch( $key )
199
			{
200
				case 'coupon.code.parentid': !$private ?: $item = $item->setParentId( $value ); break;
201
				case 'coupon.code.datestart': $item = $item->setDateStart( $value ); break;
202
				case 'coupon.code.dateend': $item = $item->setDateEnd( $value ); break;
203
				case 'coupon.code.count': $item = $item->setCount( $value ); break;
204
				case 'coupon.code.code': $item = $item->setCode( $value ); break;
205
				case 'coupon.code.ref': $item = $item->setRef( $value ); break;
206
				default: continue 2;
207
			}
208
209
			unset( $list[$key] );
210
		}
211
212
		return $item;
213
	}
214
215
216
	/**
217
	 * Returns the item values as array.
218
	 *
219
	 * @param bool True to return private properties, false for public only
220
	 * @return array Associative list of item properties and their values
221
	 */
222
	public function toArray( bool $private = false ) : array
223
	{
224
		$list = parent::toArray( $private );
225
226
		$list['coupon.code.datestart'] = $this->getDateStart();
227
		$list['coupon.code.dateend'] = $this->getDateEnd();
228
		$list['coupon.code.count'] = $this->getCount();
229
		$list['coupon.code.code'] = $this->getCode();
230
		$list['coupon.code.ref'] = $this->getRef();
231
232
		if( $private === true ) {
233
			$list['coupon.code.parentid'] = $this->getParentId();
234
		}
235
236
		return $list;
237
	}
238
239
}
240