Completed
Branch develop (8166a6)
by Romain
01:52
created

PaymentSummary   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 160
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
namespace Kerox\Messenger\Model\Common\Buttons\Payment;
3
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
    public function __construct(string $currency,
56
                                string $paymentType,
57
                                string $merchantName,
58
                                array $requestedUserInfo,
59
                                array $priceList
60
    )
61
    {
62
        $this->isValidPaymentType($paymentType);
63
        $this->isValidRequestedUserInfo($requestedUserInfo);
64
65
        $this->currency = $currency;
66
        $this->paymentType = $paymentType;
67
        $this->merchantName = $merchantName;
68
        $this->requestedUserInfo = $requestedUserInfo;
69
        $this->priceList = $priceList;
70
    }
71
72
    /**
73
     * @param bool $isTestPayment
74
     * @return PaymentSummary
75
     */
76
    public function isTestPayment(bool $isTestPayment): PaymentSummary
77
    {
78
        $this->isTestPayment = $isTestPayment;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $label
85
     * @param string $amount
86
     * @internal param array $priceList
87
     * @return PaymentSummary
88
     */
89
    public function addPriceList(string $label, string $amount): PaymentSummary
90
    {
91
        $this->priceList[] = new PriceList($label, $amount);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $paymentType
98
     * @return void
99
     * @throws \InvalidArgumentException
100
     */
101
    private function isValidPaymentType(string $paymentType)
102
    {
103
        $allowedPaymentType = $this->getAllowedPaymentType();
104
        if (!in_array($paymentType, $allowedPaymentType)) {
105
            throw new \InvalidArgumentException('$paymentType must be either ' . implode(',', $allowedPaymentType));
106
        }
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    private function getAllowedPaymentType(): array
113
    {
114
        return [
115
            self::PAYMENT_TYPE_FIXED_AMOUNT,
116
            self::PAYMENT_TYPE_FLEXIBLE_AMOUNT,
117
        ];
118
    }
119
120
    /**
121
     * @param array $requestedUserInfo
122
     * @return void
123
     * @throws \InvalidArgumentException
124
     */
125
    private function isValidRequestedUserInfo(array $requestedUserInfo)
126
    {
127
        $allowedUserInfo = $this->getAllowedUserInfo();
128
        foreach ($requestedUserInfo as $userInfo) {
129
            if (!in_array($userInfo, $allowedUserInfo)) {
130
                throw new \InvalidArgumentException("$userInfo is not a valid value. Valid values are " . implode(',', $allowedUserInfo));
131
            }
132
        }
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    private function getAllowedUserInfo(): array
139
    {
140
        return [
141
            self::USER_INFO_SHIPPING_ADDRESS,
142
            self::USER_INFO_CONTACT_NAME,
143
            self::USER_INFO_CONTACT_PHONE,
144
            self::USER_INFO_CONTACT_EMAIL,
145
        ];
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function jsonSerialize(): array
152
    {
153
        $json = [
154
            'currency' => $this->currency,
155
            'payment_type' => $this->paymentType,
156
            'is_test_payment' => $this->isTestPayment,
157
            'merchant_name' => $this->merchantName,
158
            'requested_user_info' => $this->requestedUserInfo,
159
            'price_list' => $this->priceList,
160
        ];
161
162
        return array_filter($json);
163
    }
164
}
165