Completed
Pull Request — master (#77)
by Romain
03:43 queued 01:15
created

PaymentSummary::getAllowedPaymentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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