Failed Conditions
Push — master ( e6b844...0f20c1 )
by Reüel
09:09 queued 12s
created

OrderItem::get_json()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 16
nop 0
dl 0
loc 27
ccs 0
cts 15
cp 0
crap 30
rs 9.4888
c 0
b 0
f 0
1
<?php
2
/**
3
 * Order item.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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
/**
14
 * Order item.
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 2.1.10
18
 * @since   2.0.3
19
 */
20
class OrderItem implements \JsonSerializable {
21
	/**
22
	 * Item id.
23
	 *
24
	 * @var string|null
25
	 */
26
	private $id;
27
28
	/**
29
	 * Item name (required).
30
	 *
31
	 * @var string
32
	 */
33
	private $name;
34
35
	/**
36
	 * Description.
37
	 *
38
	 * @var string|null
39
	 */
40
	private $description;
41
42
	/**
43
	 * Quantity (required).
44
	 *
45
	 * @var int
46
	 */
47
	private $quantity;
48
49
	/**
50
	 * Amount (required).
51
	 *
52
	 * @var Money
53
	 */
54
	private $amount;
55
56
	/**
57
	 * Tax.
58
	 *
59
	 * @var Money|null
60
	 */
61
	private $tax;
62
63
	/**
64
	 * Category; physical or digital (required).
65
	 *
66
	 * @var string
67
	 */
68
	private $category;
69
70
	/**
71
	 * VAT category.
72
	 *
73
	 * @var string|null
74
	 */
75
	private $vat_category;
76
77
	/**
78
	 * Construct order result.
79
	 *
80
	 * @param string $name     Name.
81
	 * @param int    $quantity Quantity.
82
	 * @param Money  $amount   Amount.
83
	 * @param string $category Category.
84
	 * @throws \InvalidArgumentException Throws invalid argument exception when arguments are invalid.
85
	 */
86 2
	public function __construct( $name, $quantity, Money $amount, $category ) {
87 2
		$this->set_name( $name );
88 2
		$this->quantity = $quantity;
89 2
		$this->amount   = $amount;
90 2
		$this->set_category( $category );
91 2
	}
92
93
	/**
94
	 * Get item ID.
95
	 *
96
	 * @return string|null
97
	 */
98
	public function get_id() {
99
		return $this->id;
100
	}
101
102
	/**
103
	 * Set item ID.
104
	 *
105
	 * @param string|null $id ID.
106
	 * @return void
107
	 */
108 1
	public function set_id( $id = null ) {
109 1
		$this->id = $id;
110 1
	}
111
112
	/**
113
	 * Get item name.
114
	 *
115
	 * @return string
116
	 */
117 1
	public function get_name() {
118 1
		return $this->name;
119
	}
120
121
	/**
122
	 * Set item name.
123
	 *
124
	 * @param string $name Name.
125
	 * @return void
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, 'OrderItems.name' );
130
131 2
		$this->name = $name;
132 2
	}
133
134
	/**
135
	 * Get item description.
136
	 *
137
	 * @return string|null
138
	 */
139
	public function get_description() {
140
		return $this->description;
141
	}
142
143
	/**
144
	 * Set item description.
145
	 *
146
	 * @param string|null $description Description.
147
	 * @return void
148
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`.
149
	 */
150 1
	public function set_description( $description ) {
151 1
		DataHelper::validate_null_or_an( $description, 100, 'OrderItems.description' );
152
153 1
		$this->description = $description;
154 1
	}
155
156
	/**
157
	 * Get quantity.
158
	 *
159
	 * @return int
160
	 */
161
	public function get_quantity() {
162
		return $this->quantity;
163
	}
164
165
	/**
166
	 * Get amount.
167
	 *
168
	 * @return Money
169
	 */
170
	public function get_amount() {
171
		return $this->amount;
172
	}
173
174
	/**
175
	 * Get tax.
176
	 *
177
	 * @return Money|null
178
	 */
179
	public function get_tax() {
180
		return $this->tax;
181
	}
182
183
	/**
184
	 * Set tax.
185
	 *
186
	 * @param Money|null $tax Tax.
187
	 * @return void
188
	 */
189 1
	public function set_tax( Money $tax = null ) {
190 1
		$this->tax = $tax;
191 1
	}
192
193
	/**
194
	 * Get category.
195
	 *
196
	 * @return string
197
	 */
198
	public function get_category() {
199
		return $this->category;
200
	}
201
202
	/**
203
	 * Set category.
204
	 *
205
	 * @param string $category Product category: PHYSICAL or DIGITAL.
206
	 * @return void
207
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 8`.
208
	 */
209 2
	public function set_category( $category ) {
210 2
		DataHelper::validate_an( $category, 8, 'OrderItems.category' );
211
212 2
		$this->category = $category;
213 2
	}
214
215
	/**
216
	 * Get VAT category.
217
	 *
218
	 * @return string|null
219
	 */
220
	public function get_vat_category() {
221
		return $this->vat_category;
222
	}
223
224
	/**
225
	 * Set VAT category.
226
	 *
227
	 * @param string|null $vat_category VAT category.
228
	 * @return void
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 jsonSerialize() {
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;
254
255
		if ( null !== $this->tax ) {
256
			$object->tax = $this->tax;
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<string> $fields Fields.
272
	 * @return array<string>
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[] = \strval( (string) $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[] = '';
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