Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

PaymentSummary::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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