Failed Conditions
Push — develop ( db783a...8c1843 )
by Reüel
05:52
created

NotificationRequestItem   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Test Coverage

Coverage 72.52%

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 317
ccs 66
cts 91
cp 0.7252
rs 9.92
c 0
b 0
f 0
wmc 31

20 Methods

Rating   Name   Duplication   Size   Complexity  
A get_merchant_reference() 0 2 1
A set_merchant_account_code() 0 2 1
A get_operations() 0 2 1
A set_psp_reference() 0 2 1
A set_event_date() 0 2 1
A get_merchant_account_code() 0 2 1
A get_event_date() 0 2 1
A set_payment_method() 0 2 1
B from_object() 0 54 11
A get_event_code() 0 2 1
A set_operations() 0 2 1
A get_payment_method() 0 2 1
A set_event_code() 0 2 1
A get_json() 0 20 2
A get_amount() 0 2 1
A set_merchant_reference() 0 2 1
A set_amount() 0 2 1
A get_psp_reference() 0 2 1
A is_success() 0 2 1
A set_success() 0 2 1
1
<?php
2
/**
3
 * Notification request 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 DateTime;
14
use InvalidArgumentException;
15
use Pronamic\WordPress\Pay\Core\Util;
16
17
/**
18
 * Notification request item
19
 *
20
 * @link    https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class NotificationRequestItem {
26
	/**
27
	 * Amount.
28
	 *
29
	 * @var Amount
30
	 */
31
	private $amount;
32
33
	/**
34
	 * Adyen's 16-character unique reference associated with the transaction/the request. This value is globally unique; quote it when communicating with us about this request.
35
	 *
36
	 * @var string
37
	 */
38
	private $psp_reference;
39
40
	/**
41
	 * The type of event the notification item refers to.
42
	 *
43
	 * @var string
44
	 */
45
	private $event_code;
46
47
	/**
48
	 * The time when the event was generated.
49
	 *
50
	 * @var DateTime
51
	 */
52
	private $event_date;
53
54
	/**
55
	 * The merchant account identifier used in the transaction the notification item refers to.
56
	 *
57
	 * @var string
58
	 */
59
	private $merchant_account_code;
60
61
	/**
62
	 * This field holds a list of the modification operations supported by the transaction the notification item refers to.
63
	 *
64
	 * @var array
65
	 */
66
	private $operations;
67
68
	/**
69
	 * A reference to uniquely identify the payment.
70
	 *
71
	 * @var string
72
	 */
73
	private $merchant_reference;
74
75
	/**
76
	 * The payment method used in the transaction the notification item refers to.
77
	 *
78
	 * @var string
79
	 */
80
	private $payment_method;
81
82
	/**
83
	 * Informs about the outcome of the event (`eventCode`) the notification refers to:
84
	 *
85
	 * - `true`: the event (`eventCode`) the notification refers to was executed successfully.
86
	 * - `false`: the event was not executed successfully.
87
	 *
88
	 * @var boolean
89
	 */
90
	private $success;
91
92
	/**
93
	 * Get amount.
94
	 *
95
	 * @return Amount
96
	 */
97
	public function get_amount() {
98
		return $this->amount;
99
	}
100
101
	/**
102
	 * Set amount.
103
	 *
104
	 * @param Amount $amount Amount.
105
	 */
106 3
	public function set_amount( Amount $amount ) {
107 3
		$this->amount = $amount;
108 3
	}
109
110
	/**
111
	 * Get PSP reference.
112
	 *
113
	 * @return string
114
	 */
115 1
	public function get_psp_reference() {
116 1
		return $this->psp_reference;
117
	}
118
119
	/**
120
	 * Set PSP reference.
121
	 *
122
	 * @param string $psp_reference PSP reference.
123
	 */
124 3
	public function set_psp_reference( $psp_reference ) {
125 3
		$this->psp_reference = $psp_reference;
126 3
	}
127
128
	/**
129
	 * Get event code.
130
	 *
131
	 * @return string
132
	 */
133 1
	public function get_event_code() {
134 1
		return $this->event_code;
135
	}
136
137
	/**
138
	 * Set event code.
139
	 *
140
	 * @param string $event_code Event code.
141
	 */
142 3
	public function set_event_code( $event_code ) {
143 3
		$this->event_code = $event_code;
144 3
	}
145
146
	/**
147
	 * Get event date.
148
	 *
149
	 * @return DateTime
150
	 */
151 1
	public function get_event_date() {
152 1
		return $this->event_date;
153
	}
154
155
	/**
156
	 * Set event date.
157
	 *
158
	 * @param DateTime $event_date Event date.
159
	 */
160 3
	public function set_event_date( DateTime $event_date ) {
161 3
		$this->event_date = $event_date;
162 3
	}
163
164
	/**
165
	 * Get merchant account code.
166
	 *
167
	 * @return string
168
	 */
169 1
	public function get_merchant_account_code() {
170 1
		return $this->merchant_account_code;
171
	}
172
173
	/**
174
	 * Set merchant account code.
175
	 *
176
	 * @param string $merchant_account_code Merchant account code.
177
	 */
178 3
	public function set_merchant_account_code( $merchant_account_code ) {
179 3
		$this->merchant_account_code = $merchant_account_code;
180 3
	}
181
182
	/**
183
	 * Get operations.
184
	 *
185
	 * @return array
186
	 */
187 1
	public function get_operations() {
188 1
		return $this->operations;
189
	}
190
191
	/**
192
	 * Set operations.
193
	 *
194
	 * @param array $operations Operations.
195
	 */
196 3
	public function set_operations( $operations ) {
197 3
		$this->operations = $operations;
198 3
	}
199
200
	/**
201
	 * Get operations.
202
	 *
203
	 * @return string
204
	 */
205 3
	public function get_merchant_reference() {
206 3
		return $this->merchant_reference;
207
	}
208
209
	/**
210
	 * Set merchant reference.
211
	 *
212
	 * @param string $merchant_reference Merchant reference.
213
	 */
214 3
	public function set_merchant_reference( $merchant_reference ) {
215 3
		$this->merchant_reference = $merchant_reference;
216 3
	}
217
218
	/**
219
	 * Get payment method.
220
	 *
221
	 * @return string
222
	 */
223 1
	public function get_payment_method() {
224 1
		return $this->payment_method;
225
	}
226
227
	/**
228
	 * Set payment method.
229
	 *
230
	 * @param string $payment_method Payment method.
231
	 */
232 3
	public function set_payment_method( $payment_method ) {
233 3
		$this->payment_method = $payment_method;
234 3
	}
235
236
	/**
237
	 * Is success.
238
	 *
239
	 * @return boolean
240
	 */
241 1
	public function is_success() {
242 1
		return $this->success;
243
	}
244
245
	/**
246
	 * Set success.
247
	 *
248
	 * @param boolean $success Success.
249
	 */
250 3
	public function set_success( $success ) {
251 3
		$this->success = $success;
252 3
	}
253
254
	/**
255
	 * Get JSON.
256
	 *
257
	 * @return object|null
258
	 */
259
	public function get_json() {
260
		$data = array(
261
			'amount'              => $this->get_amount(),
262
			'pspReference'        => $this->get_psp_reference(),
263
			'eventCode'           => $this->get_event_code(),
264
			'eventDate'           => $this->get_event_date(),
265
			'merchantAccountCode' => $this->get_merchant_account_code(),
266
			'operations'          => $this->get_operations(),
267
			'merchantReference'   => $this->get_merchant_reference(),
268
			'paymentMethod'       => $this->get_payment_method(),
269
			'success'             => Util::boolean_to_string( $this->is_success() ),
270
		);
271
272
		$data = array_filter( $data );
273
274
		if ( empty( $data ) ) {
275
			return null;
276
		}
277
278
		return (object) $data;
279
	}
280
281
	/**
282
	 * Create notification request item from object.
283
	 *
284
	 * @param object $object Object.
285
	 * @return NotificationRequestItem
286
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
287
	 */
288 4
	public static function from_object( $object ) {
289 4
		if ( ! isset( $object->amount ) ) {
290 1
			throw new InvalidArgumentException( 'Object must contain `amount` property.' );
291
		}
292
293 3
		if ( ! isset( $object->pspReference ) ) {
294
			throw new InvalidArgumentException( 'Object must contain `pspReference` property.' );
295
		}
296
297 3
		if ( ! isset( $object->eventCode ) ) {
298
			throw new InvalidArgumentException( 'Object must contain `eventCode` property.' );
299
		}
300
301 3
		if ( ! isset( $object->eventDate ) ) {
302
			throw new InvalidArgumentException( 'Object must contain `eventDate` property.' );
303
		}
304
305 3
		if ( ! isset( $object->merchantAccountCode ) ) {
306
			throw new InvalidArgumentException( 'Object must contain `merchantAccountCode` property.' );
307
		}
308
309 3
		if ( ! isset( $object->operations ) ) {
310
			throw new InvalidArgumentException( 'Object must contain `operations` property.' );
311
		}
312
313 3
		if ( ! isset( $object->merchantReference ) ) {
314
			throw new InvalidArgumentException( 'Object must contain `merchantReference` property.' );
315
		}
316
317 3
		if ( ! isset( $object->paymentMethod ) ) {
318
			throw new InvalidArgumentException( 'Object must contain `paymentMethod` property.' );
319
		}
320
321 3
		if ( ! isset( $object->success ) ) {
322
			throw new InvalidArgumentException( 'Object must contain `success` property.' );
323
		}
324
325 3
		if ( ! is_array( $object->operations ) ) {
326
			throw new InvalidArgumentException( 'Object property `operations` must be an array.' );
327
		}
328
329 3
		$item = new self();
330
331 3
		$item->set_amount( Amount::from_object( $object->amount ) );
332 3
		$item->set_psp_reference( $object->pspReference );
333 3
		$item->set_event_code( $object->eventCode );
334 3
		$item->set_event_date( new DateTime( $object->eventDate ) );
335 3
		$item->set_merchant_account_code( $object->merchantAccountCode );
336 3
		$item->set_operations( $object->operations );
337 3
		$item->set_merchant_reference( $object->merchantReference );
338 3
		$item->set_payment_method( $object->paymentMethod );
339 3
		$item->set_success( filter_var( $object->success, FILTER_VALIDATE_BOOLEAN ) );
340
341 3
		return $item;
342
	}
343
}
344