Completed
Push — master ( 3ee5ff...5084e8 )
by Joachim
12:38
created

PaymentRequest   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 596
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 98.09%

Importance

Changes 0
Metric Value
wmc 51
lcom 1
cbo 6
dl 0
loc 596
ccs 154
cts 157
cp 0.9809
rs 8.3206
c 0
b 0
f 0

46 Methods

Rating   Name   Duplication   Size   Complexity  
A parseCookieParts() 0 10 2
A __construct() 0 9 1
B getPayload() 0 32 2
A validate() 0 21 1
A getCookiePart() 0 4 2
A setCookiePart() 0 5 1
A getTerminal() 0 4 1
A setTerminal() 0 5 1
A getShopOrderId() 0 4 1
A setShopOrderId() 0 5 1
A getAmount() 0 4 1
A setAmount() 0 5 1
A getCurrency() 0 4 1
A setCurrency() 0 5 1
A getLanguage() 0 4 1
A setLanguage() 0 5 1
A getTransactionInfo() 0 4 1
A setTransactionInfo() 0 5 1
A getType() 0 4 1
A setType() 0 5 1
A getCcToken() 0 4 1
A setCcToken() 0 5 1
A getSaleReconciliationIdentifier() 0 4 1
A setSaleReconciliationIdentifier() 0 5 1
A getSaleInvoiceNumber() 0 4 1
A setSaleInvoiceNumber() 0 5 1
A getSalesTax() 0 4 1
A setSalesTax() 0 5 1
A getCookieParts() 0 4 1
A setCookieParts() 0 5 1
A getPaymentSource() 0 4 1
A setPaymentSource() 0 5 1
A getFraudService() 0 4 1
A setFraudService() 0 5 1
A getShippingMethod() 0 4 1
A setShippingMethod() 0 5 1
A getCustomerCreatedDate() 0 4 1
A setCustomerCreatedDate() 0 5 1
A getOrganisationNumber() 0 4 1
A setOrganisationNumber() 0 5 1
A getAccountOffer() 0 4 1
A setAccountOffer() 0 5 1
A getCustomerInfo() 0 7 2
A setCustomerInfo() 0 5 1
A getConfig() 0 7 2
A setConfig() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like PaymentRequest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 PaymentRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Loevgaard\AltaPay\Payload;
3
4
use Assert\Assert;
5
use Loevgaard\AltaPay\Payload\PaymentRequest\Config;
6
use Loevgaard\AltaPay\Payload\PaymentRequest\ConfigInterface;
7
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo;
8
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfoInterface;
9
10
class PaymentRequest extends Payload implements PaymentRequestInterface
11
{
12
    use OrderLineArrayTrait;
13
14
    const ACCOUNT_OFFER_REQUIRED = 'required';
15
    const ACCOUNT_OFFER_DISABLED = 'disabled';
16
17
    const PAYMENT_SOURCE_ECOMMERCE = 'eCommerce';
18
    const PAYMENT_SOURCE_MOBI = 'mobi';
19
    const PAYMENT_SOURCE_MOTO = 'moto';
20
    const PAYMENT_SOURCE_MAIL_ORDER = 'mail_order';
21
    const PAYMENT_SOURCE_TELEPHONE_ORDER = 'telephone_order';
22
23
    const SHIPPING_METHOD_LOW_COST = 'LowCost';
24
    const SHIPPING_METHOD_DESIGNATED_BY_CUSTOMER = 'DesignatedByCustomer';
25
    const SHIPPING_METHOD_INTERNATIONAL = 'International';
26
    const SHIPPING_METHOD_MILITARY = 'Military';
27
    const SHIPPING_METHOD_NEXT_DAY = 'NextDay';
28
    const SHIPPING_METHOD_OTHER = 'Other';
29
    const SHIPPING_METHOD_STORE_PICKUP = 'StorePickup';
30
    const SHIPPING_METHOD_TWO_DAY_SERVICE = 'TwoDayService';
31
    const SHIPPING_METHOD_THREE_DAY_SERVICE = 'ThreeDayService';
32
33
    /**
34
     * @var string
35
     */
36
    private $terminal;
37
38
    /**
39
     * @var string
40
     */
41
    private $shopOrderId;
42
43
    /**
44
     * @var float
45
     */
46
    private $amount;
47
48
    /**
49
     * Currency in ISO-4217 format
50
     *
51
     * @var string
52
     */
53
    private $currency;
54
55
    /**
56
     * @var string
57
     */
58
    private $language;
59
60
    /**
61
     * @var array
62
     */
63
    private $transactionInfo;
64
65
    /**
66
     * @var string
67
     */
68
    private $type;
69
70
    /**
71
     * @var string
72
     */
73
    private $ccToken;
74
75
    /**
76
     * @var string
77
     */
78
    private $saleReconciliationIdentifier;
79
80
    /**
81
     * @var string
82
     */
83
    private $saleInvoiceNumber;
84
85
    /**
86
     * @var float
87
     */
88
    private $salesTax;
89
90
    /**
91
     * @var array
92
     */
93
    private $cookieParts;
94
95
    /**
96
     * @var string
97
     */
98
    private $paymentSource;
99
100
    /**
101
     * @var string
102
     */
103
    private $fraudService;
104
105
    /**
106
     * @var string
107
     */
108
    private $shippingMethod;
109
110
    /**
111
     * @var \DateTimeInterface
112
     */
113
    private $customerCreatedDate;
114
115
    /**
116
     * @var string
117
     */
118
    private $organisationNumber;
119
120
    /**
121
     * @var string
122
     */
123
    private $accountOffer;
124
125
    /**
126
     * @var CustomerInfoInterface
127
     */
128
    private $customerInfo;
129
130
    /**
131
     * @var ConfigInterface
132
     */
133
    private $config;
134
135
    public function __construct(string $terminal, string $shopOrderId, float $amount, string $currency)
136
    {
137
        $this->cookieParts = [];
138
        $this->orderLines = [];
139
        $this->terminal = $terminal;
140 6
        $this->shopOrderId = $shopOrderId;
141
        $this->amount = $amount;
142 6
        $this->currency = $currency;
143 6
    }
144 6
145 6
    /**
146 6
     * @return array
147 6
     */
148
    public function getPayload() : array
149
    {
150
        $cookie = static::parseCookieParts($this->cookieParts);
151
152 6
        $payload = [
153
            'terminal' => $this->terminal,
154
            'shop_orderid' => $this->shopOrderId,
155 6
            'amount' => $this->amount,
156 6
            'currency' => $this->currency,
157 6
            'language' => $this->language,
158 6
            'transaction_info' => $this->transactionInfo,
159 6
            'type' => $this->type,
160 6
            'ccToken' => $this->ccToken,
161 6
            'sale_reconciliation_identifier' => $this->saleReconciliationIdentifier,
162 6
            'sale_invoice_number' => $this->saleInvoiceNumber,
163 6
            'sales_tax' => $this->salesTax,
164 6
            'cookie' => $cookie,
165 6
            'payment_source' => $this->paymentSource,
166 6
            'fraud_service' => $this->fraudService,
167 6
            'shipping_method' => $this->shippingMethod,
168 6
            'customer_created_date' => $this->customerCreatedDate ? $this->customerCreatedDate->format('Y-m-d') : null,
169 6
            'organisation_number' => $this->organisationNumber,
170 6
            'account_offer' => $this->accountOffer,
171 6
            'config' => $this->getConfig(),
172 6
            'customer_info' => $this->getCustomerInfo(),
173 4
            'orderLines' => $this->orderLines,
174
        ];
175
176 6
        $this->validate();
177 6
178 3
        return static::simplePayload($payload);
179 2
    }
180
181
    public function validate()
182 6
    {
183 6
        Assert::that($this->terminal)->string();
184 3
        Assert::that($this->shopOrderId)->string();
185 2
        Assert::that($this->amount)->float();
186
        Assert::that($this->currency)->string();
187
        Assert::thatNullOr($this->language)->string();
188 6
        Assert::thatNullOr($this->transactionInfo)->isArray();
189 6
        Assert::thatNullOr($this->type)->string();
190 3
        Assert::thatNullOr($this->ccToken)->string();
191 4
        Assert::thatNullOr($this->saleReconciliationIdentifier)->string();
192
        Assert::thatNullOr($this->saleInvoiceNumber)->string();
193 6
        Assert::thatNullOr($this->salesTax)->float();
194 3
        Assert::thatNullOr($this->paymentSource)->string();
195 2
        Assert::thatNullOr($this->fraudService)->string();
196
        Assert::thatNullOr($this->shippingMethod)->string();
197 6
        Assert::thatNullOr($this->customerCreatedDate)->isInstanceOf(\DateTimeInterface::class);
198
        Assert::thatNullOr($this->organisationNumber)->string();
199
        Assert::thatNullOr($this->accountOffer)->string();
200
        Assert::thatNullOr($this->orderLines)->isArray();
201
    }
202
203
    /**
204 3
     * Takes an array of cookie parts and returns an urlencoded string ready to send
205
     *
206 3
     * @param array $cookieParts
207 3
     * @return string
208
     */
209
    public static function parseCookieParts(array $cookieParts)
210
    {
211
        $cookie = '';
212
        foreach ($cookieParts as $key => $val) {
213 6
            $cookie .= $key.'='.rawurlencode($val).';';
214
        }
215 6
        $cookie = trim($cookie, ';');
216
217
        return $cookie;
218
    }
219
220
    /**
221
     * @param string $key
222 6
     * @return string
223
     */
224 6
    public function getCookiePart(string $key) : string
225 6
    {
226
        return isset($this->cookieParts[$key]) ? $this->cookieParts[$key] : '';
227
    }
228
229
    /**
230
     * @param string $key
231 6
     * @param string $value
232
     * @return PaymentRequest
233 6
     */
234
    public function setCookiePart(string $key, string $value) : self
235
    {
236
        $this->cookieParts[$key] = $value;
237
        return $this;
238
    }
239
240 6
    /**
241
     * @return string
242 6
     */
243 6
    public function getTerminal() : string
244
    {
245
        return $this->terminal;
246
    }
247
248
    /**
249 6
     * @param string $terminal
250
     * @return PaymentRequest
251 6
     */
252
    public function setTerminal(string $terminal) : self
253
    {
254
        $this->terminal = $terminal;
255
        return $this;
256
    }
257
258 6
    /**
259
     * @return string
260 6
     */
261 6
    public function getShopOrderId() : string
262
    {
263
        return $this->shopOrderId;
264
    }
265
266
    /**
267 6
     * @param string $shopOrderId
268
     * @return PaymentRequest
269 6
     */
270
    public function setShopOrderId(string $shopOrderId) : self
271
    {
272
        $this->shopOrderId = $shopOrderId;
273
        return $this;
274
    }
275
276 6
    /**
277
     * @return float
278 6
     */
279 6
    public function getAmount() : float
280
    {
281
        return $this->amount;
282
    }
283
284
    /**
285 6
     * @param float $amount
286
     * @return PaymentRequest
287 6
     */
288
    public function setAmount(float $amount) : self
289
    {
290
        $this->amount = $amount;
291
        return $this;
292
    }
293
294 3
    /**
295
     * @return string
296 3
     */
297 3
    public function getCurrency() : string
298
    {
299
        return $this->currency;
300
    }
301
302
    /**
303 6
     * @param string $currency
304
     * @return PaymentRequest
305 6
     */
306
    public function setCurrency(string $currency) : self
307
    {
308
        $this->currency = $currency;
309
        return $this;
310
    }
311
312 3
    /**
313
     * @return string
314 3
     */
315 3
    public function getLanguage() : ?string
316
    {
317
        return $this->language;
318
    }
319
320
    /**
321 6
     * @param string $language
322
     * @return PaymentRequest
323 6
     */
324
    public function setLanguage(string $language) : self
325
    {
326
        $this->language = $language;
327
        return $this;
328
    }
329
330 3
    /**
331
     * @return array
332 3
     */
333 3
    public function getTransactionInfo() : ?array
334
    {
335
        return $this->transactionInfo;
336
    }
337
338
    /**
339 6
     * @param array $transactionInfo
340
     * @return PaymentRequest
341 6
     */
342
    public function setTransactionInfo(array $transactionInfo) : self
343
    {
344
        $this->transactionInfo = $transactionInfo;
345
        return $this;
346
    }
347
348 3
    /**
349
     * @return string
350 3
     */
351 3
    public function getType() : ?string
352
    {
353
        return $this->type;
354
    }
355
356
    /**
357 6
     * @param string $type
358
     * @return PaymentRequest
359 6
     */
360
    public function setType(string $type) : self
361
    {
362
        $this->type = $type;
363
        return $this;
364
    }
365
366 3
    /**
367
     * @return string
368 3
     */
369 3
    public function getCcToken() : ?string
370
    {
371
        return $this->ccToken;
372
    }
373
374
    /**
375 6
     * @param string $ccToken
376
     * @return PaymentRequest
377 6
     */
378
    public function setCcToken(string $ccToken) : self
379
    {
380
        $this->ccToken = $ccToken;
381
        return $this;
382
    }
383
384 3
    /**
385
     * @return string
386 3
     */
387 3
    public function getSaleReconciliationIdentifier() : ?string
388
    {
389
        return $this->saleReconciliationIdentifier;
390
    }
391
392
    /**
393 6
     * @param string $saleReconciliationIdentifier
394
     * @return PaymentRequest
395 6
     */
396
    public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self
397
    {
398
        $this->saleReconciliationIdentifier = $saleReconciliationIdentifier;
399
        return $this;
400
    }
401
402 3
    /**
403
     * @return string
404 3
     */
405 3
    public function getSaleInvoiceNumber() : ?string
406
    {
407
        return $this->saleInvoiceNumber;
408
    }
409
410
    /**
411 6
     * @param string $saleInvoiceNumber
412
     * @return PaymentRequest
413 6
     */
414
    public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self
415
    {
416
        $this->saleInvoiceNumber = $saleInvoiceNumber;
417
        return $this;
418
    }
419
420 3
    /**
421
     * @return float
422 3
     */
423 3
    public function getSalesTax() : ?float
424
    {
425
        return $this->salesTax;
426
    }
427
428
    /**
429 6
     * @param float $salesTax
430
     * @return PaymentRequest
431 6
     */
432
    public function setSalesTax(float $salesTax) : self
433
    {
434
        $this->salesTax = $salesTax;
435
        return $this;
436
    }
437
438 3
    /**
439
     * @return array
440 3
     */
441 3
    public function getCookieParts(): array
442
    {
443
        return $this->cookieParts;
444
    }
445
446
    /**
447 6
     * @param array $cookieParts
448
     * @return PaymentRequest
449 6
     */
450
    public function setCookieParts(array $cookieParts) : self
451
    {
452
        $this->cookieParts = $cookieParts;
453
        return $this;
454
    }
455
456 3
    /**
457
     * @return string
458 3
     */
459 3
    public function getPaymentSource() : ?string
460
    {
461
        return $this->paymentSource;
462
    }
463
464
    /**
465 6
     * @param string $paymentSource
466
     * @return PaymentRequest
467 6
     */
468
    public function setPaymentSource(string $paymentSource) : self
469
    {
470
        $this->paymentSource = $paymentSource;
471
        return $this;
472
    }
473
474 3
    /**
475
     * @return string
476 3
     */
477 3
    public function getFraudService() : ?string
478
    {
479
        return $this->fraudService;
480
    }
481
482
    /**
483 6
     * @param string $fraudService
484
     * @return PaymentRequest
485 6
     */
486
    public function setFraudService(string $fraudService) : self
487
    {
488
        $this->fraudService = $fraudService;
489
        return $this;
490
    }
491
492 3
    /**
493
     * @return string
494 3
     */
495 3
    public function getShippingMethod() : ?string
496
    {
497
        return $this->shippingMethod;
498
    }
499
500
    /**
501 6
     * @param string $shippingMethod
502
     * @return PaymentRequest
503 6
     */
504
    public function setShippingMethod(string $shippingMethod) : self
505
    {
506
        $this->shippingMethod = $shippingMethod;
507
        return $this;
508
    }
509
510 3
    /**
511
     * @return \DateTimeInterface
512 3
     */
513 3
    public function getCustomerCreatedDate() : ?\DateTimeInterface
514
    {
515
        return $this->customerCreatedDate;
516
    }
517
518
    /**
519 6
     * @param \DateTimeInterface $customerCreatedDate
520
     * @return PaymentRequest
521 6
     */
522
    public function setCustomerCreatedDate(\DateTimeInterface $customerCreatedDate) : self
523
    {
524
        $this->customerCreatedDate = $customerCreatedDate;
525
        return $this;
526
    }
527
528 3
    /**
529
     * @return string
530 3
     */
531 3
    public function getOrganisationNumber() : ?string
532
    {
533
        return $this->organisationNumber;
534
    }
535
536
    /**
537 6
     * @param string $organisationNumber
538
     * @return PaymentRequest
539 6
     */
540
    public function setOrganisationNumber(string $organisationNumber) : self
541
    {
542
        $this->organisationNumber = $organisationNumber;
543
        return $this;
544
    }
545
546
    /**
547
     * @return string
548
     */
549
    public function getAccountOffer() : ?string
550
    {
551
        return $this->accountOffer;
552
    }
553
554
    /**
555 6
     * @param string $accountOffer
556
     * @return PaymentRequest
557 6
     */
558 3
    public function setAccountOffer(string $accountOffer) : self
559 2
    {
560 6
        $this->accountOffer = $accountOffer;
561
        return $this;
562
    }
563
564
    /**
565
     * @return CustomerInfoInterface
566
     */
567 3
    public function getCustomerInfo() : CustomerInfoInterface
568
    {
569 3
        if (!$this->customerInfo) {
570 3
            $this->customerInfo = new CustomerInfo();
571
        }
572
        return $this->customerInfo;
573
    }
574
575
    /**
576 6
     * @param CustomerInfoInterface $customerInfo
577
     * @return PaymentRequest
578 6
     */
579 3
    public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self
580 2
    {
581 6
        $this->customerInfo = $customerInfo;
582
        return $this;
583
    }
584
585
    /**
586
     * @return ConfigInterface
587
     */
588 3
    public function getConfig() : ConfigInterface
589
    {
590 3
        if (!$this->config) {
591 3
            $this->config = new Config();
592
        }
593
        return $this->config;
594
    }
595
596
    /**
597
     * @param ConfigInterface $config
598
     * @return PaymentRequest
599
     */
600
    public function setConfig(ConfigInterface $config) : self
601
    {
602
        $this->config = $config;
603
        return $this;
604
    }
605
}
606