Passed
Push — develop ( 86dd3b...cad7ec )
by Remco
03:47
created

NotificationRequestItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
	 * @var string
85
	 */
86
	private $payment_method;
87
88
	/**
89
	 * Informs about the outcome of the event (`eventCode`) the notification refers to:
90
	 *
91
	 * - `true`: the event (`eventCode`) the notification refers to was executed successfully.
92
	 * - `false`: the event was not executed successfully.
93
	 *
94
	 * @var boolean
95
	 */
96
	private $success;
97
98
	/**
99
	 * Construct notification request item.
100
	 *
101
	 * @link https://stackoverflow.com/questions/34468660/how-to-use-builder-pattern-with-all-parameters-as-mandatory
102
	 *
103
	 * @param Amount   $amount                Amount.
104
	 * @param string   $psp_reference         PSP reference.
105
	 * @param string   $event_code            Event code.
106
	 * @param DateTime $event_date            Event date.
107
	 * @param string   $merchant_account_code Merchant account code.
108
	 * @param string   $merchant_reference    Merchant reference.
109
	 * @param string   $payment_method        Payment method.
110
	 * @param boolean  $success               Success.
111
	 */
112 9
	public function __construct(
113
		Amount $amount,
114
		$psp_reference,
115
		$event_code,
116
		DateTime $event_date,
117
		$merchant_account_code,
118
		$merchant_reference,
119
		$payment_method,
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->payment_method        = $payment_method;
129 9
		$this->success               = $success;
130 9
	}
131
132
	/**
133
	 * Get amount.
134
	 *
135
	 * @return Amount
136
	 */
137 1
	public function get_amount() {
138 1
		return $this->amount;
139
	}
140
141
	/**
142
	 * Get PSP reference.
143
	 *
144
	 * @return string
145
	 */
146 1
	public function get_psp_reference() {
147 1
		return $this->psp_reference;
148
	}
149
150
	/**
151
	 * Get event code.
152
	 *
153
	 * @return string
154
	 */
155 2
	public function get_event_code() {
156 2
		return $this->event_code;
157
	}
158
159
	/**
160
	 * Get event date.
161
	 *
162
	 * @return DateTime
163
	 */
164 1
	public function get_event_date() {
165 1
		return $this->event_date;
166
	}
167
168
	/**
169
	 * Get merchant account code.
170
	 *
171
	 * @return string
172
	 */
173 1
	public function get_merchant_account_code() {
174 1
		return $this->merchant_account_code;
175
	}
176
177
	/**
178
	 * Get operations.
179
	 *
180
	 * @return array|null
181
	 */
182 1
	public function get_operations() {
183 1
		return $this->operations;
184
	}
185
186
	/**
187
	 * Set operations.
188
	 *
189
	 * @param array|null $operations Operations.
190
	 * @return void
191
	 */
192 7
	public function set_operations( array $operations = null ) {
193 7
		$this->operations = $operations;
194 7
	}
195
196
	/**
197
	 * Get operations.
198
	 *
199
	 * @return string
200
	 */
201 4
	public function get_merchant_reference() {
202 4
		return $this->merchant_reference;
203
	}
204
205
	/**
206
	 * Get payment method.
207
	 *
208
	 * @return string
209
	 */
210 1
	public function get_payment_method() {
211 1
		return $this->payment_method;
212
	}
213
214
	/**
215
	 * Is success.
216
	 *
217
	 * @return boolean
218
	 */
219 2
	public function is_success() {
220 2
		return $this->success;
221
	}
222
223
	/**
224
	 * Create notification request item from object.
225
	 *
226
	 * @param object $object Object.
227
	 * @return NotificationRequestItem
228
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
229
	 */
230 9
	public static function from_object( $object ) {
231 9
		$validator = new Validator();
232
233 9
		$validator->validate(
234 9
			$object,
235
			(object) array(
236 9
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/notification-request-item.json' ),
237
			),
238 9
			Constraint::CHECK_MODE_EXCEPTIONS
239
		);
240
241 9
		$item = new self(
242 9
			Amount::from_object( $object->amount ),
243 9
			$object->pspReference,
244 9
			$object->eventCode,
245 9
			new DateTime( $object->eventDate ),
246 9
			$object->merchantAccountCode,
247 9
			$object->merchantReference,
248 9
			$object->paymentMethod,
249 9
			filter_var( $object->success, FILTER_VALIDATE_BOOLEAN )
250
		);
251
252 9
		if ( property_exists( $object, 'operations' ) ) {
253 7
			$item->set_operations( $object->operations );
254
		}
255
256 9
		$item->set_original_object( $object );
257
258 9
		return $item;
259
	}
260
}
261