PaymentSummary   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 160
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A create() 0 9 1
A isTestPayment() 0 6 1
A addPriceList() 0 6 1
A isValidPaymentType() 0 7 2
A getAllowedPaymentType() 0 7 1
A isValidRequestedUserInfo() 0 9 3
A getAllowedUserInfo() 0 9 1
A toArray() 0 13 1
A jsonSerialize() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Common\Button\Payment;
6
7
use Kerox\Messenger\Exception\InvalidKeyException;
8
use Kerox\Messenger\Exception\InvalidTypeException;
9
10
class PaymentSummary implements \JsonSerializable
11
{
12
    public const PAYMENT_TYPE_FIXED_AMOUNT = 'FIXED_AMOUNT';
13
    public const PAYMENT_TYPE_FLEXIBLE_AMOUNT = 'FLEXIBLE_AMOUNT';
14
15
    public const USER_INFO_SHIPPING_ADDRESS = 'shipping_address';
16
    public const USER_INFO_CONTACT_NAME = 'contact_name';
17
    public const USER_INFO_CONTACT_PHONE = 'contact_phone';
18
    public const USER_INFO_CONTACT_EMAIL = 'contact_email';
19
20
    /**
21
     * @var string
22
     */
23
    protected $currency;
24
25
    /**
26
     * @var bool|null
27
     */
28
    protected $isTestPayment;
29
30
    /**
31
     * @var string
32
     */
33
    protected $paymentType;
34
35
    /**
36
     * @var string
37
     */
38
    protected $merchantName;
39
40
    /**
41
     * @var array
42
     */
43
    protected $requestedUserInfo = [];
44
45
    /**
46
     * @var array
47
     */
48
    protected $priceList = [];
49
50
    /**
51
     * PaymentSummary constructor.
52
     *
53
     * @param PriceList[] $priceList
54
     *
55
     * @throws \Kerox\Messenger\Exception\MessengerException
56
     */
57 3
    public function __construct(
58
        string $currency,
59
        string $paymentType,
60
        string $merchantName,
61
        array $requestedUserInfo,
62
        array $priceList
63
    ) {
64 3
        $this->isValidPaymentType($paymentType);
65 2
        $this->isValidRequestedUserInfo($requestedUserInfo);
66
67 1
        $this->currency = $currency;
68 1
        $this->paymentType = $paymentType;
69 1
        $this->merchantName = $merchantName;
70 1
        $this->requestedUserInfo = $requestedUserInfo;
71 1
        $this->priceList = $priceList;
72 1
    }
73
74
    /**
75
     * @throws \Kerox\Messenger\Exception\MessengerException
76
     *
77
     * @return \Kerox\Messenger\Model\Common\Button\Payment\PaymentSummary
78
     */
79 3
    public static function create(
80
        string $currency,
81
        string $paymentType,
82
        string $merchantName,
83
        array $requestedUserInfo,
84
        array $priceList
85
    ): self {
86 3
        return new self($currency, $paymentType, $merchantName, $requestedUserInfo, $priceList);
87
    }
88
89
    /**
90
     * @return PaymentSummary
91
     */
92 1
    public function isTestPayment(bool $isTestPayment): self
93
    {
94 1
        $this->isTestPayment = $isTestPayment;
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @return PaymentSummary
101
     */
102 1
    public function addPriceList(string $label, string $amount): self
103
    {
104 1
        $this->priceList[] = new PriceList($label, $amount);
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * @throws \Kerox\Messenger\Exception\MessengerException
111
     */
112 3
    private function isValidPaymentType(string $paymentType): void
113
    {
114 3
        $allowedPaymentType = $this->getAllowedPaymentType();
115 3
        if (!\in_array($paymentType, $allowedPaymentType, true)) {
116 1
            throw new InvalidTypeException(sprintf('paymentType must be either "%s".', implode(', ', $allowedPaymentType)));
117
        }
118 2
    }
119
120 3
    private function getAllowedPaymentType(): array
121
    {
122
        return [
123 3
            self::PAYMENT_TYPE_FIXED_AMOUNT,
124 3
            self::PAYMENT_TYPE_FLEXIBLE_AMOUNT,
125
        ];
126
    }
127
128
    /**
129
     * @throws \Kerox\Messenger\Exception\MessengerException
130
     */
131 2
    private function isValidRequestedUserInfo(array $requestedUserInfo): void
132
    {
133 2
        $allowedUserInfo = $this->getAllowedUserInfo();
134 2
        foreach ($requestedUserInfo as $userInfo) {
135 2
            if (!\in_array($userInfo, $allowedUserInfo, true)) {
136 1
                throw new InvalidKeyException(sprintf('%s is not a valid value. Valid values are "%s".', $userInfo, implode(', ', $allowedUserInfo)));
137
            }
138
        }
139 1
    }
140
141 2
    private function getAllowedUserInfo(): array
142
    {
143
        return [
144 2
            self::USER_INFO_SHIPPING_ADDRESS,
145 2
            self::USER_INFO_CONTACT_NAME,
146 2
            self::USER_INFO_CONTACT_PHONE,
147 2
            self::USER_INFO_CONTACT_EMAIL,
148
        ];
149
    }
150
151 1
    public function toArray(): array
152
    {
153
        $array = [
154 1
            'currency' => $this->currency,
155 1
            'payment_type' => $this->paymentType,
156 1
            'is_test_payment' => $this->isTestPayment,
157 1
            'merchant_name' => $this->merchantName,
158 1
            'requested_user_info' => $this->requestedUserInfo,
159 1
            'price_list' => $this->priceList,
160
        ];
161
162 1
        return array_filter($array);
163
    }
164
165 1
    public function jsonSerialize(): array
166
    {
167 1
        return $this->toArray();
168
    }
169
}
170