Cancelled
Pull Request — master (#11)
by Laurens
23:42
created

PaymentBuilder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 9
dl 28
loc 105
ccs 57
cts 57
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFromJsonArray() 0 9 2
B buildFromJson() 0 19 7
A parseCardPayment() 0 15 1
A parseCashPayment() 0 8 1
A parseInvoicePayment() 7 7 1
A parseMobilePayment() 7 7 1
A parseSwichPayment() 7 7 1
A parseVippsPayment() 7 7 1
A getFromKey() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Purchase;
6
7
use LauLamanApps\IzettleApi\API\Purchase\AbstractPayment;
8
use LauLamanApps\IzettleApi\API\Purchase\Payment\CardPayment;
9
use LauLamanApps\IzettleApi\API\Purchase\Payment\CashPayment;
10
use LauLamanApps\IzettleApi\API\Purchase\Payment\InvoicePayment;
11
use LauLamanApps\IzettleApi\API\Purchase\Payment\MobilePayment;
12
use LauLamanApps\IzettleApi\API\Purchase\Payment\SwishPayment;
13
use LauLamanApps\IzettleApi\API\Purchase\Payment\VippsPayment;
14
use LauLamanApps\IzettleApi\Client\Exceptions\PaymentTypeNotConfiguredException;
15
use Money\Currency;
16
use Money\Money;
17
use Ramsey\Uuid\Uuid;
18
19
final class PaymentBuilder implements PaymentBuilderInterface
20
{
21
    const CARD = 'IZETTLE_CARD';
22
    const CASH = 'IZETTLE_CASH';
23
    const INVOICE = 'IZETTLE_INVOICE';
24
    const MOBILE = 'MOBILE_PAY';
25
    const SWISH = 'SWISH';
26
    const VIPPS = 'VIPPS';
27
28 1
    public function buildFromJsonArray(array $payments, Currency $currency): array
29
    {
30 1
        $data = [];
31 1
        foreach ($payments as $payment) {
32 1
            $data[] = $this->buildFromJson($payment, $currency);
33
        }
34
35 1
        return $data;
36
    }
37
38 9
    public function buildFromJson(array $payment, Currency $currency): AbstractPayment
39
    {
40 9
        switch ($payment['type']) {
41 9
            case self::CARD:
42 2
                return $this->parseCardPayment($payment, $currency);
43 7
            case self::CASH:
44 2
                return $this->parseCashPayment($payment, $currency);
45 5
            case self::INVOICE:
46 1
                return $this->parseInvoicePayment($payment, $currency);
47 4
            case self::MOBILE:
48 1
                return $this->parseMobilePayment($payment, $currency);
49 3
            case self::SWISH:
50 1
                return $this->parseSwichPayment($payment, $currency);
51 2
            case self::VIPPS:
52 1
                return $this->parseVippsPayment($payment, $currency);
53
            default:
54 1
                throw new PaymentTypeNotConfiguredException('Payment type \'' . $payment['type'] . '\' not configured');
55
        }
56
    }
57
58 2
    private function parseCardPayment($payment, Currency $currency): CardPayment
59
    {
60 2
        return new CardPayment(
61 2
            Uuid::fromString($payment['uuid']),
62 2
            new Money($payment['amount'], $currency),
63 2
            $payment['attributes']['referenceNumber'],
64 2
            $payment['attributes']['maskedPan'],
65 2
            $payment['attributes']['cardType'],
66 2
            $payment['attributes']['cardPaymentEntryMode'],
67 2
            $this->getFromKey('applicationName', $payment['attributes']),
68 2
            $this->getFromKey('applicationIdentifier', $payment['attributes']),
69 2
            $this->getFromKey('terminalVerificationResults', $payment['attributes']),
70 2
            (int) $this->getFromKey('nrOfInstallments', $payment['attributes'])
71
        );
72
    }
73
74 2
    private function parseCashPayment($payment, Currency $currency): CashPayment
75
    {
76 2
        return new CashPayment(
77 2
            Uuid::fromString($payment['uuid']),
78 2
            new Money($payment['amount'], $currency),
79 2
            new Money($payment['attributes']['handedAmount'], $currency)
80
        );
81
    }
82
83 1 View Code Duplication
    private function parseInvoicePayment($payment, Currency $currency): InvoicePayment
84
    {
85 1
        return new InvoicePayment(
86 1
            Uuid::fromString($payment['uuid']),
87 1
            new Money($payment['amount'], $currency)
88
        );
89
    }
90
91 1 View Code Duplication
    private function parseMobilePayment($payment, Currency $currency): MobilePayment
92
    {
93 1
        return new MobilePayment(
94 1
            Uuid::fromString($payment['uuid']),
95 1
            new Money($payment['amount'], $currency)
96
        );
97
    }
98
99 1 View Code Duplication
    private function parseSwichPayment($payment, Currency $currency): SwishPayment
100
    {
101 1
        return new SwishPayment(
102 1
            Uuid::fromString($payment['uuid']),
103 1
            new Money($payment['amount'], $currency)
104
        );
105
    }
106
107 1 View Code Duplication
    private function parseVippsPayment($payment, Currency $currency): VippsPayment
108
    {
109 1
        return new VippsPayment(
110 1
            Uuid::fromString($payment['uuid']),
111 1
            new Money($payment['amount'], $currency)
112
        );
113
    }
114
115 2
    private function getFromKey($key, array $data)
116
    {
117 2
        if (!array_key_exists($key, $data)) {
118 1
            return null;
119
        }
120
121 2
        return $data[$key];
122
    }
123
}
124