Failed Conditions
Push — develop ( 05bf6b...a342cf )
by Reüel
03:02
created

LineItem::set_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Line 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\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use InvalidArgumentException;
14
15
/**
16
 * Line item.
17
 *
18
 * @author  Reüel van der Steege
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class LineItem {
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 Amount
55
	 */
56
	private $amount;
57
58
	/**
59
	 * Tax.
60
	 *
61
	 * @var Amount|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 line item.
81
	 *
82
	 * @param string $name     Name.
83
	 * @param int    $quantity Quantity.
84
	 * @param Amount $amount   Amount.
85
	 * @param string $category Category.
86
	 *
87
	 * @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
88
	 */
89
	public function __construct( $name, $quantity, Amount $amount, $category ) {
90
		$this->set_name( $name );
91
		$this->quantity = $quantity;
92
		$this->amount   = $amount;
93
		$this->set_category( $category );
94
	}
95
96
	/**
97
	 * Get item ID.
98
	 *
99
	 * @return string|null
100
	 */
101
	public function get_id() {
102
		return $this->id;
103
	}
104
105
	/**
106
	 * Set item ID.
107
	 *
108
	 * @param string|null $id ID.
109
	 */
110
	public function set_id( $id = null ) {
111
		$this->id = $id;
112
	}
113
114
	/**
115
	 * Get item name.
116
	 *
117
	 * @return string
118
	 */
119
	public function get_name() {
120
		return $this->name;
121
	}
122
123
	/**
124
	 * Set item name.
125
	 *
126
	 * @param string $name Name.
127
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 50`.
128
	 */
129
	public function set_name( $name ) {
130
		DataHelper::validate_an( $name, 50 );
131
132
		$this->name = $name;
133
	}
134
135
	/**
136
	 * Get item description.
137
	 *
138
	 * @return string|null
139
	 */
140
	public function get_description() {
141
		return $this->description;
142
	}
143
144
	/**
145
	 * Set item description.
146
	 *
147
	 * @param string|null $description Description.
148
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`.
149
	 */
150
	public function set_description( $description ) {
151
		if ( null !== $description ) {
152
			DataHelper::validate_an( $description, 100 );
153
		}
154
155
		$this->description = $description;
156
	}
157
158
	/**
159
	 * Get quantity.
160
	 *
161
	 * @return int
162
	 */
163
	public function get_quantity() {
164
		return $this->quantity;
165
	}
166
167
	/**
168
	 * Get amount.
169
	 *
170
	 * @return Amount
171
	 */
172
	public function get_amount() {
173
		return $this->amount;
174
	}
175
176
	/**
177
	 * Get tax.
178
	 *
179
	 * @return Amount|null
180
	 */
181
	public function get_tax() {
182
		return $this->tax;
183
	}
184
185
	/**
186
	 * Set tax.
187
	 *
188
	 * @param Amount|null $tax Tax.
189
	 */
190
	public function set_tax( Amount $tax = null ) {
191
		$this->tax = $tax;
192
	}
193
194
	/**
195
	 * Get category.
196
	 *
197
	 * @return string
198
	 */
199
	public function get_category() {
200
		return $this->category;
201
	}
202
203
	/**
204
	 * Set category.
205
	 *
206
	 * @param string $category Product category: PHYSICAL or DIGITAL.
207
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 8`.
208
	 */
209
	public function set_category( $category ) {
210
		DataHelper::validate_an( $category, 8 );
211
212
		$this->category = $category;
213
	}
214
215
	/**
216
	 * Get VAT category.
217
	 *
218
	 * @return int|null
219
	 */
220
	public function get_vat_category() {
221
		return $this->vat_category;
222
	}
223
224
	/**
225
	 * Set VAT category.
226
	 *
227
	 * @param int|null $vat_category VAT category.
228
	 */
229
	public function set_vat_category( $vat_category ) {
230
		$this->vat_category = $vat_category;
231
	}
232
233
	/**
234
	 * Get JSON.
235
	 *
236
	 * @return object
237
	 */
238
	public function get_json() {
239
		$object = (object) array();
240
241
		if ( null !== $this->id ) {
242
			$object->id = $this->id;
243
		}
244
245
		$object->name = $this->name;
246
247
		if ( null !== $this->description ) {
248
			$object->description = $this->description;
249
		}
250
251
		$object->quantity = $this->quantity;
252
		$object->amount   = $this->amount->get_json();
253
254
		if ( null !== $this->tax ) {
255
			$object->tax = $this->tax->get_json();
256
		}
257
258
		$object->category = $this->category;
259
260
		if ( null !== $this->vat_category ) {
261
			$object->vatCategory = $this->vat_category;
262
		}
263
264
		return $object;
265
	}
266
}
267