Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

PaymentParser::parse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 16
nc 7
nop 2
crap 7
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 PaymentParser
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
    /**
29
     * @return AbstractPayment[]
30
     */
31 3
    public static function parseArray($payments, Currency $currency): array
32
    {
33 3
        $data = [];
34 3
        foreach ($payments as $payment) {
35 1
            $data[] = self::parse($payment, $currency);
36
        }
37
38 3
        return $data;
39
    }
40
41 8
    public static function parse(array $payment, Currency $currency): AbstractPayment
42
    {
43 8
        switch ($payment['type']) {
44 8
            case self::CARD:
45 1
                return self::parseCardPayment($payment, $currency);
46 7
            case self::CASH:
47 2
                return self::parseCashPayment($payment, $currency);
48 5
            case self::INVOICE:
49 1
                return self::parseInvoicePayment($payment, $currency);
50 4
            case self::MOBILE:
51 1
                return self::parseMobilePayment($payment, $currency);
52 3
            case self::SWISH:
53 1
                return self::parseSwichPayment($payment, $currency);
54 2
            case self::VIPPS:
55 1
                return self::parseVippsPayment($payment, $currency);
56
            default:
57 1
                throw new PaymentTypeNotConfiguredException('Payment type \'' . $payment['type'] . '\' not configured');
58
        }
59
    }
60
61 1
    private static function parseCardPayment($payment, Currency $currency): CardPayment
62
    {
63 1
        $applicationName = null;
64 1
        $applicationIdentifier = null;
65 1
        $terminalVerificationResults = null;
66 1
        $nrOfInstallments = null;
67
68 1
        $uuid = Uuid::fromString($payment['uuid']);
69 1
        $amount = new Money($payment['amount'], $currency);
70 1
        $referenceNumber = $payment['attributes']['referenceNumber'];
71 1
        $maskedPan = $payment['attributes']['maskedPan'];
72 1
        $cardType = $payment['attributes']['cardType'];
73 1
        $cardPaymentEntryMode = $payment['attributes']['cardPaymentEntryMode'];
74 1
        if (array_key_exists('applicationName', $payment['attributes'])) {
75 1
            $applicationName = $payment['attributes']['applicationName'];
76
        }
77 1
        if (array_key_exists('applicationIdentifier', $payment['attributes'])) {
78 1
            $applicationIdentifier = $payment['attributes']['applicationIdentifier'];
79
        }
80 1
        if (array_key_exists('terminalVerificationResults', $payment['attributes'])) {
81 1
            $terminalVerificationResults = $payment['attributes']['terminalVerificationResults'];
82
        }
83 1
        if (array_key_exists('nrOfInstallments', $payment['attributes'])) {
84 1
            $nrOfInstallments = (int) $payment['attributes']['nrOfInstallments'];
85
        }
86
87 1
        return new CardPayment(
88 1
            $uuid,
89 1
            $amount,
90 1
            $referenceNumber,
91 1
            $maskedPan,
92 1
            $cardType,
93 1
            $cardPaymentEntryMode,
94 1
            $applicationName,
95 1
            $applicationIdentifier,
96 1
            $terminalVerificationResults,
97 1
            $nrOfInstallments
98
        );
99
    }
100
101 2
    private static function parseCashPayment($payment, Currency $currency): CashPayment
102
    {
103 2
        return new CashPayment(
104 2
            Uuid::fromString($payment['uuid']),
105 2
            new Money($payment['amount'], $currency),
106 2
            new Money($payment['attributes']['handedAmount'], $currency)
107
        );
108
    }
109
110 1 View Code Duplication
    private static function parseInvoicePayment($payment, Currency $currency): InvoicePayment
111
    {
112 1
        return new InvoicePayment(
113 1
            Uuid::fromString($payment['uuid']),
114 1
            new Money($payment['amount'], $currency)
115
        );
116
    }
117
118 1 View Code Duplication
    private static function parseMobilePayment($payment, Currency $currency): MobilePayment
119
    {
120 1
        return new MobilePayment(
121 1
            Uuid::fromString($payment['uuid']),
122 1
            new Money($payment['amount'], $currency)
123
        );
124
    }
125
126 1 View Code Duplication
    private static function parseSwichPayment($payment, Currency $currency): SwishPayment
127
    {
128 1
        return new SwishPayment(
129 1
            Uuid::fromString($payment['uuid']),
130 1
            new Money($payment['amount'], $currency)
131
        );
132
    }
133
134 1 View Code Duplication
    private static function parseVippsPayment($payment, Currency $currency): VippsPayment
135
    {
136 1
        return new VippsPayment(
137 1
            Uuid::fromString($payment['uuid']),
138 1
            new Money($payment['amount'], $currency)
139
        );
140
    }
141
}
142