NotificationRequestItem::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Notification request item
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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
15
/**
16
 * Notification request item
17
 *
18
 * @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.5
22
 * @since   1.0.0
23
 */
24
class NotificationRequestItem extends ResponseObject {
25
	/**
26
	 * Amount.
27
	 *
28
	 * @var Amount
29
	 */
30
	private $amount;
31
32
	/**
33
	 * 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.
34
	 *
35
	 * @var string
36
	 */
37
	private $psp_reference;
38
39
	/**
40
	 * The type of event the notification item refers to.
41
	 *
42
	 * @var string
43
	 */
44
	private $event_code;
45
46
	/**
47
	 * The time when the event was generated.
48
	 *
49
	 * @var DateTime
50
	 */
51
	private $event_date;
52
53
	/**
54
	 * The merchant account identifier used in the transaction the notification item refers to.
55
	 *
56
	 * @var string
57
	 */
58
	private $merchant_account_code;
59
60
	/**
61
	 * This field holds a list of the modification operations supported by the transaction the notification item refers to.
62
	 *
63
	 * This field is populated only in authorisation notifications.
64
	 *
65
	 * In case of HTTP POST notifications, the operation list is a sequence of comma-separated string values.
66
	 *
67
	 * @var array<string, string|array<string, string|array<int, string>>>|null
68
	 */
69
	private $operations;
70
71
	/**
72
	 * A reference to uniquely identify the payment.
73
	 *
74
	 * @var string
75
	 */
76
	private $merchant_reference;
77
78
	/**
79
	 * The payment method used in the transaction the notification item refers to.
80
	 *
81
	 * This field is populated only in authorisation notifications.
82
	 *
83
	 * @var string|null
84
	 */
85
	private $payment_method;
86
87
	/**
88
	 * Informs about the outcome of the event (`eventCode`) the notification refers to:
89
	 *
90
	 * - `true`: the event (`eventCode`) the notification refers to was executed successfully.
91
	 * - `false`: the event was not executed successfully.
92
	 *
93
	 * @var boolean
94
	 */
95
	private $success;
96
97
	/**
98
	 * Construct notification request item.
99
	 *
100
	 * @link https://stackoverflow.com/questions/34468660/how-to-use-builder-pattern-with-all-parameters-as-mandatory
101
	 *
102
	 * @param Amount   $amount                Amount.
103
	 * @param string   $psp_reference         PSP reference.
104
	 * @param string   $event_code            Event code.
105
	 * @param DateTime $event_date            Event date.
106
	 * @param string   $merchant_account_code Merchant account code.
107
	 * @param string   $merchant_reference    Merchant reference.
108
	 * @param boolean  $success               Success.
109
	 */
110 9
	public function __construct(
111
		Amount $amount,
112
		$psp_reference,
113
		$event_code,
114
		DateTime $event_date,
115
		$merchant_account_code,
116
		$merchant_reference,
117
		$success
118
	) {
119 9
		$this->amount                = $amount;
120 9
		$this->psp_reference         = $psp_reference;
121 9
		$this->event_code            = $event_code;
122 9
		$this->event_date            = $event_date;
123 9
		$this->merchant_account_code = $merchant_account_code;
124 9
		$this->merchant_reference    = $merchant_reference;
125 9
		$this->success               = $success;
126 9
	}
127
128
	/**
129
	 * Get amount.
130
	 *
131
	 * @return Amount
132
	 */
133 1
	public function get_amount() {
134 1
		return $this->amount;
135
	}
136
137
	/**
138
	 * Get PSP reference.
139
	 *
140
	 * @return string
141
	 */
142 1
	public function get_psp_reference() {
143 1
		return $this->psp_reference;
144
	}
145
146
	/**
147
	 * Get event code.
148
	 *
149
	 * @return string
150
	 */
151 2
	public function get_event_code() {
152 2
		return $this->event_code;
153
	}
154
155
	/**
156
	 * Get event date.
157
	 *
158
	 * @return DateTime
159
	 */
160 1
	public function get_event_date() {
161 1
		return $this->event_date;
162
	}
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
	 * Get operations.
175
	 *
176
	 * @return array<string, string|array<string, string|array<int, string>>>|null
177
	 */
178 1
	public function get_operations() {
179 1
		return $this->operations;
180
	}
181
182
	/**
183
	 * Set operations.
184
	 *
185
	 * @param array<string, string|array<string, string|array<int, string>>>|null $operations Operations.
186
	 * @return void
187
	 */
188 7
	public function set_operations( array $operations = null ) {
189 7
		$this->operations = $operations;
190 7
	}
191
192
	/**
193
	 * Get operations.
194
	 *
195
	 * @return string
196
	 */
197 4
	public function get_merchant_reference() {
198 4
		return $this->merchant_reference;
199
	}
200
201
	/**
202
	 * Get payment method.
203
	 *
204
	 * @return string|null
205
	 */
206 1
	public function get_payment_method() {
207 1
		return $this->payment_method;
208
	}
209
210
	/**
211
	 * Set payment method.
212
	 *
213
	 * @param string|null $payment_method Payment method.
214
	 * @return void
215
	 */
216 9
	public function set_payment_method( $payment_method ) {
217 9
		$this->payment_method = $payment_method;
218 9
	}
219
220
	/**
221
	 * Is success.
222
	 *
223
	 * @return boolean
224
	 */
225 2
	public function is_success() {
226 2
		return $this->success;
227
	}
228
229
	/**
230
	 * Create notification request item from object.
231
	 *
232
	 * @param object $object Object.
233
	 * @return NotificationRequestItem
234
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
235
	 */
236 9
	public static function from_object( $object ) {
237 9
		$validator = new \JsonSchema\Validator();
238
239 9
		$validator->validate(
240 9
			$object,
241
			(object) array(
242 9
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/notification-request-item.json' ),
243
			),
244 9
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
245
		);
246
247
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
248
249 9
		$item = new self(
250 9
			Amount::from_object( $object->amount ),
251 9
			$object->pspReference,
252 9
			$object->eventCode,
253 9
			new DateTime( $object->eventDate ),
254 9
			$object->merchantAccountCode,
255 9
			$object->merchantReference,
256 9
			filter_var( $object->success, FILTER_VALIDATE_BOOLEAN )
257
		);
258
259 9
		if ( property_exists( $object, 'operations' ) ) {
260 7
			$item->set_operations( $object->operations );
261
		}
262
263 9
		if ( property_exists( $object, 'paymentMethod' ) ) {
264 9
			$item->set_payment_method( $object->paymentMethod );
265
		}
266
267
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
268
269 9
		$item->set_original_object( $object );
270
271 9
		return $item;
272
	}
273
}
274