Failed Conditions
Push — develop ( 158730...90e575 )
by Reüel
04:11
created

Payment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 13
dl 0
loc 15
ccs 0
cts 14
cp 0
crap 2
rs 9.8333

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
 * Payment
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use DateTimeInterface;
14
15
/**
16
 * Payment
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.1.0
20
 * @since   2.1.0
21
 */
22
class Payment extends BaseResource {
23
	/**
24
	 * The payment’s status.
25
	 *
26
	 * @var string
27
	 */
28
	private $status;
29
30
	/**
31
	 * The payment method used for this payment, either forced on creation by specifying the method parameter, or chosen by the customer on our payment method selection screen.
32
	 *
33
	 * @var string|null
34
	 */
35
	private $method;
36
37
	/**
38
	 * The identifier referring to the profile this payment was created on.
39
	 *
40
	 * @var string
41
	 */
42
	private $profile_id;
43
44
	/**
45
	 * If a customer was specified upon payment creation, the customer’s token will be available here as well.
46
	 *
47
	 * @var string|null
48
	 */
49
	private $customer_id;
50
51
	/**
52
	 * If the payment is a first or recurring payment, this field will hold the ID of the mandate.
53
	 *
54
	 * @var string|null
55
	 */
56
	private $mandate_id;
57
58
	/**
59
	 * Payment method specific details.
60
	 *
61
	 * @var PaymentDetails|null
62
	 */
63
	private $details;
64
65
	/**
66
	 * The mode used to create this payment. Mode determines whether a payment is real (live mode) or a test payment.
67
	 *
68
	 * @var string
69
	 */
70
	private $mode;
71
72
	/**
73
	 * The payment’s date and time of creation, in ISO 8601 format.
74
	 *
75
	 * @var DateTimeInterface
76
	 */
77
	private $created_at;
78
79
	/**
80
	 * The amount of the payment, e.g. {"currency":"EUR", "value":"100.00"} for a €100.00 payment.
81
	 *
82
	 * @var Amount
83
	 */
84
	private $amount;
85
86
	/**
87
	 * A short description of the payment. The description is visible in the Dashboard and will be shown on the customer’s bank or card statement when possible.
88
	 *
89
	 * @var string
90
	 */
91
	private $description;
92
93
	/**
94
	 * The URL your customer will be redirected to after completing or canceling the payment process.
95
	 *
96
	 * @var string|null
97
	 */
98
	private $redirect_url;
99
100
	/**
101
	 * The optional metadata you provided upon payment creation. Metadata can for example be used to link an order to a payment.
102
	 *
103
	 * @var string
104
	 */
105
	private $metadata;
106
107
	/**
108
	 * The customer’s locale, either forced on creation by specifying the `locale` parameter, or detected by us during checkout. Will be a full locale, for example `nl_NL`.
109
	 *
110
	 * @var string
111
	 */
112
	private $locale;
113
114
	/**
115
	 * Indicates which type of payment this is in a recurring sequence.
116
	 * Set to `first` for first payments that allow the customer to agree to automatic recurring charges taking place on their account in the future.
117
	 * Set to `recurring` for payments where the customer’s card is charged automatically.
118
	 * Set to `oneoff` by default, which indicates the payment is a regular non-recurring payment.
119
	 *
120
	 * @var string
121
	 */
122
	private $sequence_type;
123
124
	/**
125
	 * For bank transfer payments, the `_links` object will contain some additional URL objects relevant to the payment.
126
	 *
127
	 * @var object
128
	 */
129
	private $links;
130
131
	/**
132
	 * Construct payment.
133
	 *
134
	 * @param string            $id            Identifier.
135
	 * @param string            $mode          Mode.
136
	 * @param DateTimeInterface $created_at    Created at.
137
	 * @param string            $status        Status.
138
	 * @param Amount            $amount        Amount.
139
	 * @param string            $description   Description.
140
	 * @param string|null       $redirect_url  Redirect URL.
141
	 * @param string|null       $method        Method.
142
	 * @param string            $metadata      Metadata.
143
	 * @param string            $locale        Locale.
144
	 * @param string            $profile_id    Profile ID.
145
	 * @param string            $sequence_type Sequence type.
146
	 * @param object            $links         Links.
147
	 */
148
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $locale, $profile_id, $sequence_type, $links ) {
149
		parent::__construct( $id );
150
151
		$this->mode          = $mode;
152
		$this->created_at    = $created_at;
153
		$this->status        = $status;
154
		$this->amount        = $amount;
155
		$this->description   = $description;
156
		$this->redirect_url  = $redirect_url;
157
		$this->method        = $method;
158
		$this->metadata      = $metadata;
159
		$this->locale        = $locale;
160
		$this->profile_id    = $profile_id;
161
		$this->sequence_type = $sequence_type;
162
		$this->links         = $links;
163
	}
164
165
	/**
166
	 * Get status.
167
	 *
168
	 * @return string
169
	 */
170
	public function get_status() {
171
		return $this->status;
172
	}
173
174
	/**
175
	 * Get method.
176
	 *
177
	 * @return string|null
178
	 */
179
	public function get_method() {
180
		return $this->method;
181
	}
182
183
	/**
184
	 * Get profile ID.
185
	 *
186
	 * @return string
187
	 */
188
	public function get_profile_id() {
189
		return $this->profile_id;
190
	}
191
192
	/**
193
	 * Get customer ID.
194
	 *
195
	 * @return string|null
196
	 */
197
	public function get_customer_id() {
198
		return $this->customer_id;
199
	}
200
201
	/**
202
	 * Set customer ID.
203
	 *
204
	 * @param string|null $customer_id Customer ID.
205
	 * @return void
206
	 */
207
	public function set_customer_id( $customer_id ) {
208
		$this->customer_id = $customer_id;
209
	}
210
211
	/**
212
	 * Get mandate ID.
213
	 *
214
	 * @return string|null
215
	 */
216
	public function get_mandate_id() {
217
		return $this->mandate_id;
218
	}
219
220
	/**
221
	 * Set mandate ID.
222
	 *
223
	 * @param string|null $mandate_id Mandate ID.
224
	 * @return void
225
	 */
226
	public function set_mandate_id( $mandate_id ) {
227
		$this->mandate_id = $mandate_id;
228
	}
229
230
	/**
231
	 * Has chargebacks.
232
	 *
233
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
234
	 * @return bool True if payment has chargebacks, false otherwise.
235
	 */
236
	public function has_chargebacks() {
237
		return ! empty( $this->links->chargebacks );
238
	}
239
240
	/**
241
	 * Get payment method specific details.
242
	 *
243
	 * @return PaymentDetails|null
244
	 */
245
	public function get_details() {
246
		return $this->details;
247
	}
248
249
	/**
250
	 * Set payment method specific details.
251
	 *
252
	 * @param PaymentDetails|null $details Details.
253
	 * @return void
254
	 */
255
	public function set_details( PaymentDetails $details = null ) {
256
		$this->details = $details;
257
	}
258
259
	/**
260
	 * Get links.
261
	 *
262
	 * @return object
263
	 */
264
	public function get_links() {
265
		return $this->links;
266
	}
267
268
	/**
269
	 * Set links.
270
	 *
271
	 * @param object $links Links.
272
	 * @return void
273
	 */
274
	public function set_links( $links ) {
275
		$this->links = $links;
276
	}
277
278
	/**
279
	 * Create payment from JSON.
280
	 *
281
	 * @link https://docs.mollie.com/reference/v2/payments-api/get-payment
282
	 * @param object $json JSON object.
283
	 * @return Payment
284
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
285
	 */
286
	public static function from_json( $json ) {
287
		$validator = new \JsonSchema\Validator();
288
289
		$validator->validate(
290
			$json,
291
			(object) array(
292
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
293
			),
294
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
295
		);
296
297
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
298
		$payment = new Payment(
299
			$json->id,
300
			$json->mode,
301
			new \DateTimeImmutable( $json->createdAt ),
302
			$json->status,
303
			Amount::from_json( $json->amount ),
304
			$json->description,
305
			$json->redirectUrl,
306
			$json->method,
307
			$json->metadata,
308
			$json->locale,
309
			$json->profileId,
310
			$json->sequenceType,
311
			$json->_links
312
		);
313
314
		if ( \property_exists( $json, 'customerId' ) ) {
315
			$payment->set_customer_id( $json->customerId );
316
		}
317
318
		if ( \property_exists( $json, 'mandateId' ) ) {
319
			$payment->set_mandate_id( $json->mandateId );
320
		}
321
322
		if ( \property_exists( $json, 'details' ) ) {
323
			$payment->set_details( PaymentDetails::from_json( (string) $payment->get_method(), $json->details ) );
324
		}
325
326
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
327
328
		return $payment;
329
	}
330
}
331