Failed Conditions
Push — develop ( 6f22b8...619326 )
by Reüel
03:40
created

AbstractPaymentRequest   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 520
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 86
dl 0
loc 520
ccs 0
cts 151
cp 0
rs 8.64
c 0
b 0
f 0
wmc 47

35 Methods

Rating   Name   Duplication   Size   Complexity  
A get_return_url() 0 2 1
A get_date_of_birth() 0 2 1
A set_reference() 0 2 1
A set_shopper_locale() 0 2 1
A set_shopper_name() 0 2 1
A __construct() 0 5 1
A set_shopper_statement() 0 2 1
A get_country_code() 0 2 1
A set_shopper_reference() 0 2 1
A get_channel() 0 2 1
A get_delivery_address() 0 2 1
A get_reference() 0 2 1
A set_telephone_number() 0 2 1
A get_shopper_locale() 0 2 1
A new_items() 0 4 1
A set_line_items() 0 2 1
A get_telephone_number() 0 2 1
A set_channel() 0 2 1
A set_shopper_ip() 0 2 1
A get_line_items() 0 2 1
A get_billing_address() 0 2 1
A set_merchant_account() 0 2 1
A set_return_url() 0 2 1
A get_shopper_ip() 0 2 1
A get_shopper_name() 0 2 1
A set_date_of_birth() 0 2 1
A set_billing_address() 0 2 1
F get_json() 0 67 11
A set_country_code() 0 11 3
A set_amount() 0 2 1
A get_shopper_statement() 0 2 1
A get_amount() 0 2 1
A get_merchant_account() 0 2 1
A get_shopper_reference() 0 2 1
A set_delivery_address() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like AbstractPaymentRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractPaymentRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Abstract payment request
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 InvalidArgumentException;
14
15
/**
16
 * Abstract payment request
17
 *
18
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
19
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
abstract class AbstractPaymentRequest {
26
	/**
27
	 * Amount.
28
	 *
29
	 * @var Amount
30
	 */
31
	private $amount;
32
33
	/**
34
	 * Billing address.
35
	 *
36
	 * @var Address|null
37
	 */
38
	private $billing_address;
39
40
	/**
41
	 * Channel.
42
	 *
43
	 * The platform where a payment transaction takes place. This field is optional for filtering out
44
	 * payment methods that are only available on specific platforms. If this value is not set,
45
	 * then we will try to infer it from the sdkVersion or token.
46
	 *
47
	 * Possible values: Android, iOS, Web.
48
	 *
49
	 * @var string|null
50
	 */
51
	private $channel;
52
53
	/**
54
	 * The shopper country.
55
	 *
56
	 * Format: ISO 3166-1 alpha-2 Example: NL or DE
57
	 *
58
	 * @var string|null
59
	 */
60
	private $country_code;
61
62
	/**
63
	 * Date of birth.
64
	 *
65
	 * @var string
66
	 */
67
	private $date_of_birth;
68
69
	/**
70
	 * The address where the purchased goods should be delivered
71
	 *
72
	 * @var Address|null
73
	 */
74
	private $delivery_address;
75
76
	/**
77
	 * Line items regarding the payment.
78
	 *
79
	 * @var LineItems
80
	 */
81
	private $line_items;
82
83
	/**
84
	 * The merchant account identifier, with which you want to process the transaction.
85
	 *
86
	 * @var string
87
	 */
88
	private $merchant_account;
89
90
	/**
91
	 * The reference to uniquely identify a payment. This reference is used in all communication
92
	 * with you about the payment status. We recommend using a unique value per payment;
93
	 * however, it is not a requirement. If you need to provide multiple references for
94
	 * a transaction, separate them with hyphens ("-"). Maximum length: 80 characters.
95
	 *
96
	 * @var string
97
	 */
98
	private $reference;
99
100
	/**
101
	 * The URL to return to.
102
	 *
103
	 * @var string
104
	 */
105
	private $return_url;
106
107
	/**
108
	 * The shopper's IP address.
109
	 *
110
	 * @var string|null
111
	 */
112
	private $shopper_ip;
113
114
	/**
115
	 * The combination of a language code and a country code to specify the language to be used in the payment.
116
	 *
117
	 * @var string|null
118
	 */
119
	private $shopper_locale;
120
121
	/**
122
	 * The shopper's full name and gender (if specified)
123
	 *
124
	 * @var Name|null
125
	 */
126
	private $shopper_name;
127
128
	/**
129
	 * The shopper's reference to uniquely identify this shopper (e.g. user ID or account ID). This field is
130
	 * required for recurring payments
131
	 *
132
	 * @var string|null
133
	 */
134
	private $shopper_reference;
135
136
	/**
137
	 * The text to appear on the shopper's bank statement.
138
	 *
139
	 * @var string|null
140
	 */
141
	private $shopper_statement;
142
143
	/**
144
	 * The shopper's telephone number
145
	 *
146
	 * @var string|null
147
	 */
148
	private $telephone_number;
149
150
	/**
151
	 * Construct a payment request object.
152
	 *
153
	 * @param Amount $amount           The amount information for the transaction.
154
	 * @param string $merchant_account The merchant account identifier, with which you want to process the transaction.
155
	 * @param string $reference        The reference to uniquely identify a payment.
156
	 * @param string $return_url       The URL to return to.
157
	 */
158
	public function __construct( Amount $amount, $merchant_account, $reference, $return_url ) {
159
		$this->set_amount( $amount );
160
		$this->set_merchant_account( $merchant_account );
161
		$this->set_reference( $reference );
162
		$this->set_return_url( $return_url );
163
	}
164
165
	/**
166
	 * Get amount.
167
	 *
168
	 * @return Amount
169
	 */
170
	public function get_amount() {
171
		return $this->amount;
172
	}
173
174
	/**
175
	 * Set amount.
176
	 *
177
	 * @param Amount $amount Amount.
178
	 */
179
	public function set_amount( Amount $amount ) {
180
		$this->amount = $amount;
181
	}
182
183
	/**
184
	 * Get billing address.
185
	 *
186
	 * @return Address|null
187
	 */
188
	public function get_billing_address() {
189
		return $this->billing_address;
190
	}
191
192
	/**
193
	 * Set billing address.
194
	 *
195
	 * @param Address|null $billing_address Billing address.
196
	 */
197
	public function set_billing_address( Address $billing_address = null ) {
198
		$this->billing_address = $billing_address;
199
	}
200
201
	/**
202
	 * Get channel.
203
	 *
204
	 * @return string|null
205
	 */
206
	public function get_channel() {
207
		return $this->channel;
208
	}
209
210
	/**
211
	 * Set channel.
212
	 *
213
	 * @param string|null $channel Channel.
214
	 */
215
	public function set_channel( $channel ) {
216
		$this->channel = $channel;
217
	}
218
219
	/**
220
	 * Get country code.
221
	 *
222
	 * @return string|null
223
	 */
224
	public function get_country_code() {
225
		return $this->country_code;
226
	}
227
228
	/**
229
	 * Set country code.
230
	 *
231
	 * @param string|null $country_code Country code.
232
	 */
233
	public function set_country_code( $country_code ) {
234
		if ( null !== $country_code && 2 !== strlen( $country_code ) ) {
235
			throw new InvalidArgumentException(
236
				sprintf(
237
					'Given country code `%s` not ISO 3166-1 alpha-2 value.',
238
					$country_code
239
				)
240
			);
241
		}
242
243
		$this->country_code = $country_code;
244
	}
245
246
	/**
247
	 * Get date of birth.
248
	 *
249
	 * @return string
250
	 */
251
	public function get_date_of_birth() {
252
		return $this->date_of_birth;
253
	}
254
255
	/**
256
	 * Set date of birth.
257
	 *
258
	 * @param string|null $date_of_birth Date of birth.
259
	 */
260
	public function set_date_of_birth( $date_of_birth ) {
261
		$this->date_of_birth = $date_of_birth;
262
	}
263
264
	/**
265
	 * Get delivery address.
266
	 *
267
	 * @return Address|null
268
	 */
269
	public function get_delivery_address() {
270
		return $this->delivery_address;
271
	}
272
273
	/**
274
	 * Set delivery address.
275
	 *
276
	 * @param Address|null $delivery_address Delivery address.
277
	 */
278
	public function set_delivery_address( Address $delivery_address = null ) {
279
		$this->delivery_address = $delivery_address;
280
	}
281
282
	/**
283
	 * Get line items.
284
	 *
285
	 * @return LineItems
286
	 */
287
	public function get_line_items() {
288
		return $this->line_items;
289
	}
290
291
	/**
292
	 * Set line items.
293
	 *
294
	 * @param LineItems $line_items Line items.
295
	 */
296
	public function set_line_items( $line_items ) {
297
		$this->line_items = $line_items;
298
	}
299
300
	/**
301
	 * Create and set new line items.
302
	 *
303
	 * @return LineItems
304
	 */
305
	public function new_items() {
306
		$this->line_items = new LineItems();
307
308
		return $this->line_items;
309
	}
310
311
	/**
312
	 * Get merchant account.
313
	 *
314
	 * @return string
315
	 */
316
	public function get_merchant_account() {
317
		return $this->merchant_account;
318
	}
319
320
	/**
321
	 * Set merchant account.
322
	 *
323
	 * @param string $merchant_account Merchant account.
324
	 */
325
	public function set_merchant_account( $merchant_account ) {
326
		$this->merchant_account = $merchant_account;
327
	}
328
329
	/**
330
	 * Get reference.
331
	 *
332
	 * @return string
333
	 */
334
	public function get_reference() {
335
		return $this->reference;
336
	}
337
338
	/**
339
	 * Set reference.
340
	 *
341
	 * @param string $reference Reference.
342
	 */
343
	public function set_reference( $reference ) {
344
		$this->reference = $reference;
345
	}
346
347
	/**
348
	 * Get return URL.
349
	 *
350
	 * @return string
351
	 */
352
	public function get_return_url() {
353
		return $this->return_url;
354
	}
355
356
	/**
357
	 * Set return URL.
358
	 *
359
	 * @param string $return_url Return URL.
360
	 */
361
	public function set_return_url( $return_url ) {
362
		$this->return_url = $return_url;
363
	}
364
365
	/**
366
	 * Get shopper IP.
367
	 *
368
	 * @return string|null
369
	 */
370
	public function get_shopper_ip() {
371
		return $this->shopper_ip;
372
	}
373
374
	/**
375
	 * Set shopper IP.
376
	 *
377
	 * @param string|null $shopper_ip Shopper IP.
378
	 */
379
	public function set_shopper_ip( $shopper_ip ) {
380
		$this->shopper_ip = $shopper_ip;
381
	}
382
383
	/**
384
	 * Get shopper locale.
385
	 *
386
	 * @return string|null
387
	 */
388
	public function get_shopper_locale() {
389
		return $this->shopper_locale;
390
	}
391
392
	/**
393
	 * Set shopper locale.
394
	 *
395
	 * @param string|null $shopper_locale Shopper locale.
396
	 */
397
	public function set_shopper_locale( $shopper_locale ) {
398
		$this->shopper_locale = $shopper_locale;
399
	}
400
401
	/**
402
	 * Get shopper name.
403
	 *
404
	 * @return Name|null
405
	 */
406
	public function get_shopper_name() {
407
		return $this->shopper_name;
408
	}
409
410
	/**
411
	 * Set shopper name.
412
	 *
413
	 * @param Name|null $shopper_name Shopper name.
414
	 */
415
	public function set_shopper_name( Name $shopper_name = null ) {
416
		$this->shopper_name = $shopper_name;
417
	}
418
419
	/**
420
	 * Get shopper reference.
421
	 *
422
	 * @return string|null
423
	 */
424
	public function get_shopper_reference() {
425
		return $this->shopper_reference;
426
	}
427
428
	/**
429
	 * Set shopper reference.
430
	 *
431
	 * @param string|null $shopper_reference Shopper reference.
432
	 */
433
	public function set_shopper_reference( $shopper_reference ) {
434
		$this->shopper_reference = $shopper_reference;
435
	}
436
437
	/**
438
	 * Get shopper statement.
439
	 *
440
	 * @return string|null
441
	 */
442
	public function get_shopper_statement() {
443
		return $this->shopper_statement;
444
	}
445
446
	/**
447
	 * Set shopper statement.
448
	 *
449
	 * @param string|null $shopper_statement Shopper statement.
450
	 */
451
	public function set_shopper_statement( $shopper_statement ) {
452
		$this->shopper_statement = $shopper_statement;
453
	}
454
455
	/**
456
	 * Get telephone number.
457
	 *
458
	 * @return string|null
459
	 */
460
	public function get_telephone_number() {
461
		return $this->telephone_number;
462
	}
463
464
	/**
465
	 * Set shopper statement.
466
	 *
467
	 * @param string|null $telephone_number Telephone number.
468
	 */
469
	public function set_telephone_number( $telephone_number ) {
470
		$this->telephone_number = $telephone_number;
471
	}
472
473
	/**
474
	 * Get JSON.
475
	 *
476
	 * @return object
477
	 */
478
	public function get_json() {
479
		$object = (object) array();
480
481
		// Amount.
482
		$object->amount = $this->get_amount()->get_json();
483
484
		// Billing address.
485
		if ( null !== $this->billing_address ) {
486
			$object->billingAddress = $this->billing_address->get_json();
487
		}
488
489
		// Channel.
490
		if ( null !== $this->channel ) {
491
			$object->channel = $this->channel;
492
		}
493
494
		// Country code.
495
		if ( null !== $this->country_code ) {
496
			$object->countryCode = $this->country_code;
497
		}
498
499
		// Line items.
500
		if ( null !== $this->line_items ) {
501
			$object->lineItems = $this->line_items->get_json();
502
		}
503
504
		// Merchant account.
505
		$object->merchantAccount = $this->get_merchant_account();
506
507
		// Reference.
508
		$object->reference = $this->get_reference();
509
510
		// Return URL.
511
		$object->returnUrl = $this->get_return_url();
512
513
		// Shopper IP.
514
		if ( null !== $this->shopper_ip ) {
515
			$object->shopperIP = $this->shopper_ip;
516
		}
517
518
		// Shopper locale.
519
		if ( null !== $this->shopper_locale ) {
520
			$object->shopperLocale = $this->shopper_locale;
521
		}
522
523
		// Shopper name.
524
		if ( null !== $this->shopper_name ) {
525
			$object->shopperName = $this->shopper_name->get_json();
526
		}
527
528
		// Shopper reference.
529
		if ( null !== $this->shopper_reference ) {
530
			$object->shopperReference = $this->shopper_reference;
531
		}
532
533
		// Shopper statement.
534
		if ( null !== $this->shopper_statement ) {
535
			$object->shopperStatement = $this->shopper_statement;
536
		}
537
538
		// Telephone number.
539
		if ( null !== $this->telephone_number ) {
540
			$object->telephoneNumber = $this->telephone_number;
541
		}
542
543
		// Return object.
544
		return $object;
545
	}
546
}
547