Failed Conditions
Push — develop ( 700cbd...25e20e )
by Remco
03:48
created

NotificationRequestItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
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
use Pronamic\WordPress\Pay\Core\Util;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\Gateways\Adyen\Util. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
/**
20
 * Notification request item
21
 *
22
 * @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem
23
 *
24
 * @author  Remco Tolsma
25
 * @version 1.0.0
26
 * @since   1.0.0
27
 */
28
class NotificationRequestItem extends ResponseObject {
29
	/**
30
	 * Amount.
31
	 *
32
	 * @var Amount
33
	 */
34
	private $amount;
35
36
	/**
37
	 * 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.
38
	 *
39
	 * @var string
40
	 */
41
	private $psp_reference;
42
43
	/**
44
	 * The type of event the notification item refers to.
45
	 *
46
	 * @var string
47
	 */
48
	private $event_code;
49
50
	/**
51
	 * The time when the event was generated.
52
	 *
53
	 * @var DateTime
54
	 */
55
	private $event_date;
56
57
	/**
58
	 * The merchant account identifier used in the transaction the notification item refers to.
59
	 *
60
	 * @var string
61
	 */
62
	private $merchant_account_code;
63
64
	/**
65
	 * This field holds a list of the modification operations supported by the transaction the notification item refers to.
66
	 *
67
	 * @var array
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
	 * @var string
82
	 */
83
	private $payment_method;
84
85
	/**
86
	 * Informs about the outcome of the event (`eventCode`) the notification refers to:
87
	 *
88
	 * - `true`: the event (`eventCode`) the notification refers to was executed successfully.
89
	 * - `false`: the event was not executed successfully.
90
	 *
91
	 * @var boolean
92
	 */
93
	private $success;
94
95
	/**
96
	 * Construct notification request item.
97
	 *
98
	 * @link https://stackoverflow.com/questions/34468660/how-to-use-builder-pattern-with-all-parameters-as-mandatory
99
	 *
100
	 * @param Amount   $amount                Amount.
101
	 * @param string   $psp_reference         PSP reference.
102
	 * @param string   $event_code            Event code.
103
	 * @param DateTime $event_date            Event date.
104
	 * @param string   $merchant_account_code Merchant account code.
105
	 * @param array    $operations            Operations.
106
	 * @param string   $merchant_reference    Merchant reference.
107
	 * @param string   $payment_method        Payment method.
108
	 * @param boolean  $success               Success.
109
	 */
110 5
	public function __construct(
111
		Amount $amount,
112
		$psp_reference,
113
		$event_code,
114
		DateTime $event_date,
115
		$merchant_account_code,
116
		array $operations,
117
		$merchant_reference,
118
		$payment_method,
119
		$success
120
	) {
121 5
		$this->amount                = $amount;
122 5
		$this->psp_reference         = $psp_reference;
123 5
		$this->event_code            = $event_code;
124 5
		$this->event_date            = $event_date;
125 5
		$this->merchant_account_code = $merchant_account_code;
126 5
		$this->operations            = $operations;
127 5
		$this->merchant_reference    = $merchant_reference;
128 5
		$this->payment_method        = $payment_method;
129 5
		$this->success               = $success;
130 5
	}
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
181
	 */
182 1
	public function get_operations() {
183 1
		return $this->operations;
184
	}
185
186
	/**
187
	 * Get operations.
188
	 *
189
	 * @return string
190
	 */
191 4
	public function get_merchant_reference() {
192 4
		return $this->merchant_reference;
193
	}
194
195
	/**
196
	 * Get payment method.
197
	 *
198
	 * @return string
199
	 */
200 1
	public function get_payment_method() {
201 1
		return $this->payment_method;
202
	}
203
204
	/**
205
	 * Is success.
206
	 *
207
	 * @return boolean
208
	 */
209 2
	public function is_success() {
210 2
		return $this->success;
211
	}
212
213
	/**
214
	 * Create notification request item from object.
215
	 *
216
	 * @param object $object Object.
217
	 * @return NotificationRequestItem
218
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
219
	 */
220 5
	public static function from_object( $object ) {
221 5
		$validator = new Validator();
222
223 5
		$validator->validate(
224 5
			$object,
225
			(object) array(
226 5
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/notification-request-item.json' ),
227
			),
228 5
			Constraint::CHECK_MODE_EXCEPTIONS
229
		);
230
231 5
		$item = new self(
232 5
			Amount::from_object( $object->amount ),
233 5
			$object->pspReference,
234 5
			$object->eventCode,
235 5
			new DateTime( $object->eventDate ),
236 5
			$object->merchantAccountCode,
237 5
			$object->operations,
238 5
			$object->merchantReference,
239 5
			$object->paymentMethod,
240 5
			filter_var( $object->success, FILTER_VALIDATE_BOOLEAN )
241
		);
242
243 5
		$item->set_original_object( $object );
244
245 5
		return $item;
246
	}
247
}
248