Failed Conditions
Push — master ( 7ec057...73baf0 )
by Reüel
16:01 queued 08:21
created

OrderItem::set_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Order item.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use InvalidArgumentException;
14
15
/**
16
 * Order item.
17
 *
18
 * @author  Reüel van der Steege
19
 * @version 2.1.0
20
 * @since   2.0.3
21
 */
22
class OrderItem {
23
	/**
24
	 * Item id.
25
	 *
26
	 * @var string|null
27
	 */
28
	private $id;
29
30
	/**
31
	 * Item name (required).
32
	 *
33
	 * @var string
34
	 */
35
	private $name;
36
37
	/**
38
	 * Description.
39
	 *
40
	 * @var string|null
41
	 */
42
	private $description;
43
44
	/**
45
	 * Quantity (required).
46
	 *
47
	 * @var int
48
	 */
49
	private $quantity;
50
51
	/**
52
	 * Amount (required).
53
	 *
54
	 * @var Money
55
	 */
56
	private $amount;
57
58
	/**
59
	 * Tax.
60
	 *
61
	 * @var Money|null
62
	 */
63
	private $tax;
64
65
	/**
66
	 * Category; physical or digital (required).
67
	 *
68
	 * @var string
69
	 */
70
	private $category;
71
72
	/**
73
	 * VAT category.
74
	 *
75
	 * @var int|null
76
	 */
77
	private $vat_category;
78
79
	/**
80
	 * Construct order result.
81
	 *
82
	 * @param string $name     Name.
83
	 * @param int    $quantity Quantity.
84
	 * @param Money  $amount   Amount.
85
	 * @param string $category Category.
86
	 * @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
87
	 */
88 2
	public function __construct( $name, $quantity, Money $amount, $category ) {
89 2
		$this->set_name( $name );
90 2
		$this->quantity = $quantity;
91 2
		$this->amount   = $amount;
92 2
		$this->set_category( $category );
93 2
	}
94
95
	/**
96
	 * Get item ID.
97
	 *
98
	 * @return string|null
99
	 */
100
	public function get_id() {
101
		return $this->id;
102
	}
103
104
	/**
105
	 * Set item ID.
106
	 *
107
	 * @param string|null $id ID.
108
	 */
109 1
	public function set_id( $id = null ) {
110 1
		$this->id = $id;
111 1
	}
112
113
	/**
114
	 * Get item name.
115
	 *
116
	 * @return string
117
	 */
118 1
	public function get_name() {
119 1
		return $this->name;
120
	}
121
122
	/**
123
	 * Set item name.
124
	 *
125
	 * @param string $name Name.
126
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 50`.
127
	 */
128 2
	public function set_name( $name ) {
129 2
		DataHelper::validate_an( $name, 50 );
130
131 2
		DataHelper::validate_html_special_chars( $name );
132
133 2
		$this->name = $name;
134 2
	}
135
136
	/**
137
	 * Get item description.
138
	 *
139
	 * @return string|null
140
	 */
141
	public function get_description() {
142
		return $this->description;
143
	}
144
145
	/**
146
	 * Set item description.
147
	 *
148
	 * @param string|null $description Description.
149
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`.
150
	 */
151 1
	public function set_description( $description ) {
152 1
		if ( null !== $description ) {
153 1
			DataHelper::validate_an( $description, 100 );
154
		}
155
156 1
		$this->description = $description;
157 1
	}
158
159
	/**
160
	 * Get quantity.
161
	 *
162
	 * @return int
163
	 */
164
	public function get_quantity() {
165
		return $this->quantity;
166
	}
167
168
	/**
169
	 * Get amount.
170
	 *
171
	 * @return Money
172
	 */
173
	public function get_amount() {
174
		return $this->amount;
175
	}
176
177
	/**
178
	 * Get tax.
179
	 *
180
	 * @return Money|null
181
	 */
182
	public function get_tax() {
183
		return $this->tax;
184
	}
185
186
	/**
187
	 * Set tax.
188
	 *
189
	 * @param Money|null $tax Tax.
190
	 */
191 1
	public function set_tax( Money $tax = null ) {
192 1
		$this->tax = $tax;
193 1
	}
194
195
	/**
196
	 * Get category.
197
	 *
198
	 * @return string
199
	 */
200
	public function get_category() {
201
		return $this->category;
202
	}
203
204
	/**
205
	 * Set category.
206
	 *
207
	 * @param string $category Product category: PHYSICAL or DIGITAL.
208
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 8`.
209
	 */
210 2
	public function set_category( $category ) {
211 2
		DataHelper::validate_an( $category, 8 );
212
213 2
		$this->category = $category;
214 2
	}
215
216
	/**
217
	 * Get VAT category.
218
	 *
219
	 * @return int|null
220
	 */
221
	public function get_vat_category() {
222
		return $this->vat_category;
223
	}
224
225
	/**
226
	 * Set VAT category.
227
	 *
228
	 * @param int|null $vat_category VAT category.
229
	 */
230 1
	public function set_vat_category( $vat_category ) {
231 1
		$this->vat_category = $vat_category;
232 1
	}
233
234
	/**
235
	 * Get JSON.
236
	 *
237
	 * @return object
238
	 */
239
	public function get_json() {
240
		$object = (object) array();
241
242
		if ( null !== $this->id ) {
243
			$object->id = $this->id;
244
		}
245
246
		$object->name = $this->name;
247
248
		if ( null !== $this->description ) {
249
			$object->description = $this->description;
250
		}
251
252
		$object->quantity = $this->quantity;
253
		$object->amount   = $this->amount->get_json();
254
255
		if ( null !== $this->tax ) {
256
			$object->tax = $this->tax->get_json();
257
		}
258
259
		$object->category = $this->category;
260
261
		if ( null !== $this->vat_category ) {
262
			$object->vatCategory = $this->vat_category;
263
		}
264
265
		return $object;
266
	}
267
268
	/**
269
	 * Get signature fields.
270
	 *
271
	 * @param array $fields Fields.
272
	 * @return array
273
	 */
274 1
	public function get_signature_fields( $fields = array() ) {
275 1
		if ( null !== $this->id ) {
276 1
			$fields[] = $this->id;
277
		}
278
279 1
		$fields[] = $this->name;
280 1
		$fields[] = $this->description;
281 1
		$fields[] = strval( $this->quantity );
282
283 1
		$fields = $this->amount->get_signature_fields( $fields );
284
285 1
		if ( null === $this->tax ) {
286
			$fields[] = null;
287
		} else {
288 1
			$fields = $this->tax->get_signature_fields( $fields );
289
		}
290
291 1
		$fields[] = $this->category;
292
293 1
		if ( null !== $this->vat_category ) {
294 1
			$fields[] = $this->vat_category;
295
		}
296
297 1
		return $fields;
298
	}
299
}
300