Completed
Push — master ( d2b3f3...05ef8e )
by João Felipe Magro
02:25
created

CreditCard::serializeCreditCardWithNumber()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 0
1
<?php
2
3
namespace Ipag\Classes;
4
5
use Ipag\Classes\Contracts\Emptiable;
6
use Ipag\Classes\Contracts\Serializable;
7
use Ipag\Classes\Traits\EmptiableTrait;
8
9
final class CreditCard extends BaseResource implements Emptiable, Serializable
10
{
11
    use EmptiableTrait;
12
13
    /**
14
     * @var string
15
     */
16
    private $number;
17
18
    /**
19
     * @var string
20
     */
21
    private $holder;
22
23
    /**
24
     * @var string
25
     */
26
    private $expiryMonth;
27
28
    /**
29
     * @var string
30
     */
31
    private $expiryYear;
32
33
    /**
34
     * @var string
35
     */
36
    private $cvc;
37
38
    /**
39
     * @var string
40
     */
41
    private $token;
42
43
    /**
44
     * @var bool
45
     */
46
    private $save;
47
48
    /**
49
     * @return string
50
     */
51
    public function getNumber()
52
    {
53
        return $this->number;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getHolder()
60
    {
61
        return $this->holder;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getExpiryMonth()
68
    {
69
        return $this->expiryMonth;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getExpiryYear()
76
    {
77
        return $this->expiryYear;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getCvc()
84
    {
85
        return $this->cvc;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getToken()
92
    {
93
        return $this->token;
94
    }
95
96
    /**
97
     * @param string number
98
     */
99
    public function setNumber($number)
100
    {
101
        $this->number = $this->getNumberUtil()->getOnlyNumbers($number);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string holder
108
     */
109
    public function setHolder($holder)
110
    {
111
        $this->holder = substr((string) $holder, 0, 50);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param string expiryMonth
118
     */
119
    public function setExpiryMonth($expiryMonth)
120
    {
121
        if (!$this->isValidMonth($expiryMonth)) {
122
            throw new \UnexpectedValueException(
123
                'O mês de expiração do cartão deve ser um número entre 1 e 12'
124
            );
125
        }
126
        $this->expiryMonth = $expiryMonth;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param string expiryYear
133
     */
134
    public function setExpiryYear($expiryYear)
135
    {
136
        if (!$this->isValidYear($expiryYear)) {
137
            throw new \UnexpectedValueException(
138
                'O ano de expiração do cartão deve ser um número de 2 ou 4 dígitos'
139
            );
140
        }
141
        $this->expiryYear = sprintf('20%d', substr($expiryYear, -2, 2));
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param string cvc
148
     */
149
    public function setCvc($cvc)
150
    {
151
        if (!$this->isValidCvc($cvc)) {
152
            throw new \UnexpectedValueException(
153
                'O código de segurança deve ser um número e deve ter 3 ou 4 dígitos'
154
            );
155
        }
156
        $this->cvc = $cvc;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param string token
163
     */
164
    public function setToken($token)
165
    {
166
        $this->token = (string) $token;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    public function hasSave()
175
    {
176
        return (bool) $this->save;
177
    }
178
179
    /**
180
     * @param bool $save
181
     */
182
    public function setSave($save)
183
    {
184
        $this->save = (bool) $save;
185
186
        return $this;
187
    }
188
189
    private function isValidMonth($month)
190
    {
191
        return (bool) (is_numeric($month) && $month >= 1 && $month <= 12);
192
    }
193
194
    private function isValidYear($year)
195
    {
196
        return (bool) (is_numeric($year) && strlen($year) >= 2 && strlen($year) <= 4);
197
    }
198
199
    private function isValidCvc($cvc)
200
    {
201
        return (bool) (is_numeric($cvc) && (strlen($cvc) == 3 || strlen($cvc) == 4));
202
    }
203
204
    public function hasToken()
205
    {
206
        return !empty($this->token);
207
    }
208
209
    public function hasCvc()
210
    {
211
        return !empty($this->cvc);
212
    }
213
214
    public function hide()
215
    {
216
        $this->hideCard();
217
        $this->hideCvc();
218
    }
219
220
    public function hideCard()
221
    {
222
        $this->number = preg_replace('/^(\d{6})(\d+)(\d{4})$/', '$1******$3', $this->number);
223
    }
224
225
    public function hideCvc()
226
    {
227
        $this->cvc = preg_replace('/\d/', '*', $this->cvc);
228
    }
229
230
    public function serialize()
231
    {
232
        if ($this->isEmpty()) {
233
            return [];
234
        }
235
236
        if ($this->hasToken()) {
237
            return $this->serializeCreditCardWithToken();
238
        }
239
240
        return $this->serializeCreditCardWithNumber();
241
    }
242
243
    private function serializeCreditCardWithNumber()
244
    {
245
        $_creditCard = [
246
            'num_cartao'  => urlencode($this->getNumber()),
247
            'nome_cartao' => urlencode($this->getHolder()),
248
            'mes_cartao'  => urlencode($this->getExpiryMonth()),
249
            'ano_cartao'  => urlencode($this->getExpiryYear()),
250
        ];
251
252
        if ($this->hasCvc()) {
253
            $_creditCard['cvv_cartao'] = urlencode($this->getCvc());
254
        }
255
256
        if ($this->hasSave()) {
257
            $_creditCard['gera_token_cartao'] = urlencode($this->hasSave());
258
        }
259
260
        return $_creditCard;
261
    }
262
263
    private function serializeCreditCardWithToken()
264
    {
265
        return [
266
            'token_cartao' => urlencode($this->getToken()),
267
        ];
268
    }
269
}
270