Passed
Push — master ( 8ed01e...08cfff )
by Romain
02:40
created

PaymentSummary   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 160
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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