Payment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 1
eloc 12
c 4
b 2
f 0
nc 1
nop 12
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 2
rs 9.8666

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-2022 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.2.2
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 date and time the payment will expire, in ISO 8601 format. This parameter is omitted if the payment can no longer expire.
81
	 *
82
	 * @var DateTimeInterface
83
	 */
84
	private $expires_at;
85
86
	/**
87
	 * The amount of the payment, e.g. {"currency":"EUR", "value":"100.00"} for a €100.00 payment.
88
	 *
89
	 * @var Amount
90
	 */
91
	private $amount;
92
93
	/**
94
	 * 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.
95
	 *
96
	 * @var string
97
	 */
98
	private $description;
99
100
	/**
101
	 * The URL your customer will be redirected to after completing or canceling the payment process.
102
	 *
103
	 * @var string|null
104
	 */
105
	private $redirect_url;
106
107
	/**
108
	 * The optional metadata you provided upon payment creation. Metadata can for example be used to link an order to a payment.
109
	 *
110
	 * @var string
111
	 */
112
	private $metadata;
113
114
	/**
115
	 * 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`.
116
	 *
117
	 * @var string
118
	 */
119
	private $locale;
120
121
	/**
122
	 * Indicates which type of payment this is in a recurring sequence.
123
	 * Set to `first` for first payments that allow the customer to agree to automatic recurring charges taking place on their account in the future.
124
	 * Set to `recurring` for payments where the customer’s card is charged automatically.
125
	 * Set to `oneoff` by default, which indicates the payment is a regular non-recurring payment.
126
	 *
127
	 * @var string
128
	 */
129
	private $sequence_type;
130
131
	/**
132
	 * For bank transfer payments, the `_links` object will contain some additional URL objects relevant to the payment.
133
	 *
134
	 * @var object
135
	 */
136
	private $links;
137
138
	/**
139
	 * Amount refunded.
140
	 *
141
	 * @var Amount|null
142
	 */
143
	private $amount_refunded;
144
145
	/**
146
	 * Construct payment.
147
	 *
148
	 * @param string            $id            Identifier.
149
	 * @param string            $mode          Mode.
150
	 * @param DateTimeInterface $created_at    Created at.
151
	 * @param string            $status        Status.
152
	 * @param Amount            $amount        Amount.
153
	 * @param string            $description   Description.
154
	 * @param string|null       $redirect_url  Redirect URL.
155
	 * @param string|null       $method        Method.
156
	 * @param string            $metadata      Metadata.
157
	 * @param string            $profile_id    Profile ID.
158
	 * @param string            $sequence_type Sequence type.
159
	 * @param object            $links         Links.
160
	 */
161
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $profile_id, $sequence_type, $links ) {
162
		parent::__construct( $id );
163
164
		$this->mode          = $mode;
165
		$this->created_at    = $created_at;
166
		$this->status        = $status;
167
		$this->amount        = $amount;
168
		$this->description   = $description;
169
		$this->redirect_url  = $redirect_url;
170
		$this->method        = $method;
171
		$this->metadata      = $metadata;
172
		$this->profile_id    = $profile_id;
173
		$this->sequence_type = $sequence_type;
174
		$this->links         = $links;
175
	}
176
177
	/**
178
	 * Get status.
179
	 *
180
	 * @return string
181
	 */
182
	public function get_status() {
183
		return $this->status;
184
	}
185
186
	/**
187
	 * Get method.
188
	 *
189
	 * @return string|null
190
	 */
191
	public function get_method() {
192
		return $this->method;
193
	}
194
195
	/**
196
	 * Get profile ID.
197
	 *
198
	 * @return string
199
	 */
200
	public function get_profile_id() {
201
		return $this->profile_id;
202
	}
203
204
	/**
205
	 * Get customer ID.
206
	 *
207
	 * @return string|null
208
	 */
209
	public function get_customer_id() {
210
		return $this->customer_id;
211
	}
212
213
	/**
214
	 * Get locale.
215
	 *
216
	 * @return string
217
	 */
218
	public function get_locale() {
219
		return $this->locale;
220
	}
221
222
	/**
223
	 * Set locale.
224
	 *
225
	 * @param string $locale Locale.
226
	 * @return void
227
	 */
228
	public function set_locale( $locale ) {
229
		$this->locale = $locale;
230
	}
231
232
	/**
233
	 * Set customer ID.
234
	 *
235
	 * @param string|null $customer_id Customer ID.
236
	 * @return void
237
	 */
238
	public function set_customer_id( $customer_id ) {
239
		$this->customer_id = $customer_id;
240
	}
241
242
	/**
243
	 * Get mandate ID.
244
	 *
245
	 * @return string|null
246
	 */
247
	public function get_mandate_id() {
248
		return $this->mandate_id;
249
	}
250
251
	/**
252
	 * Set mandate ID.
253
	 *
254
	 * @param string|null $mandate_id Mandate ID.
255
	 * @return void
256
	 */
257
	public function set_mandate_id( $mandate_id ) {
258
		$this->mandate_id = $mandate_id;
259
	}
260
261
	/**
262
	 * Has chargebacks.
263
	 *
264
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
265
	 * @return bool True if payment has chargebacks, false otherwise.
266
	 */
267
	public function has_chargebacks() {
268
		return ! empty( $this->links->chargebacks );
269
	}
270
271
	/**
272
	 * Get payment method specific details.
273
	 *
274
	 * @return PaymentDetails|null
275
	 */
276
	public function get_details() {
277
		return $this->details;
278
	}
279
280
	/**
281
	 * Set payment method specific details.
282
	 *
283
	 * @param PaymentDetails|null $details Details.
284
	 * @return void
285
	 */
286
	public function set_details( PaymentDetails $details = null ) {
287
		$this->details = $details;
288
	}
289
290
	/**
291
	 * Get amount refunded.
292
	 *
293
	 * @return Amount|null
294
	 */
295
	public function get_amount_refunded() {
296
		return $this->amount_refunded;
297
	}
298
299
	/**
300
	 * Set amount refunded.
301
	 *
302
	 * @param Amount|null $amount_refunded Amount refunded.
303
	 * @return void
304
	 */
305
	public function set_amount_refunded( Amount $amount_refunded = null ) {
306
		$this->amount_refunded = $amount_refunded;
307
	}
308
309
	/**
310
	 * Get expires at.
311
	 *
312
	 * @return DateTimeInterface
313
	 */
314
	public function get_expires_at() {
315
		return $this->expires_at;
316
	}
317
318
	/**
319
	 * Set expires at.
320
	 *
321
	 * @param DateTimeInterface $expires_at Expiry date.
322
	 */
323
	public function set_expires_at( DateTimeInterface $expires_at ) {
324
		$this->expires_at = $expires_at;
325
	}
326
327
	/**
328
	 * Get links.
329
	 *
330
	 * @return object
331
	 */
332
	public function get_links() {
333
		return $this->links;
334
	}
335
336
	/**
337
	 * Set links.
338
	 *
339
	 * @param object $links Links.
340
	 * @return void
341
	 */
342
	public function set_links( $links ) {
343
		$this->links = $links;
344
	}
345
346
	/**
347
	 * Create payment from JSON.
348
	 *
349
	 * @link https://docs.mollie.com/reference/v2/payments-api/get-payment
350
	 * @param object $json JSON object.
351
	 * @return Payment
352
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
353
	 */
354
	public static function from_json( $json ) {
355
		$validator = new \JsonSchema\Validator();
356
357
		$validator->validate(
358
			$json,
359
			(object) array(
360
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
361
			),
362
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
363
		);
364
365
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
366
		$payment = new Payment(
367
			$json->id,
368
			$json->mode,
369
			new \DateTimeImmutable( $json->createdAt ),
370
			$json->status,
371
			Amount::from_json( $json->amount ),
372
			$json->description,
373
			$json->redirectUrl,
374
			$json->method,
375
			$json->metadata,
376
			$json->profileId,
377
			$json->sequenceType,
378
			$json->_links
379
		);
380
381
		if ( \property_exists( $json, 'expiresAt' ) ) {
382
			$payment->set_expires_at( new \DateTimeImmutable( $json->expiresAt ) );
383
		}
384
385
		if ( \property_exists( $json, 'locale' ) ) {
386
			$payment->set_locale( $json->locale );
387
		}
388
389
		if ( \property_exists( $json, 'customerId' ) ) {
390
			$payment->set_customer_id( $json->customerId );
391
		}
392
393
		if ( \property_exists( $json, 'mandateId' ) ) {
394
			$payment->set_mandate_id( $json->mandateId );
395
		}
396
397
		if ( \property_exists( $json, 'details' ) ) {
398
			$payment->set_details( PaymentDetails::from_json( (string) $payment->get_method(), $json->details ) );
399
		}
400
401
		if ( \property_exists( $json, 'amountRefunded' ) ) {
402
			$refunded_amount = Amount::from_json( $json->amountRefunded );
403
404
			$payment->set_amount_refunded( $refunded_amount );
405
		}
406
407
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
408
409
		return $payment;
410
	}
411
}
412