Passed
Push — develop ( cad7ec...b1970e )
by Remco
03:34
created

NotificationRequestItem::set_payment_method()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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