Passed
Push — master ( cc4a0b...c8d669 )
by João Felipe Magro
02:41
created

Order::getCapture()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Ipag\Classes;
4
5
use Ipag\Classes\Contracts\Emptiable;
6
use Ipag\Classes\Contracts\ObjectSerializable;
7
use Ipag\Classes\Traits\EmptiableTrait;
8
9
final class Order extends BaseResource implements Emptiable, ObjectSerializable
10
{
11
    use EmptiableTrait;
12
13
    /**
14
     * @var string
15
     */
16
    private $orderId;
17
18
    /**
19
     * @var string
20
     */
21
    private $operation;
22
23
    /**
24
     * @var string
25
     */
26
    private $callbackUrl;
27
28
    /**
29
     * @var float
30
     */
31
    private $amount;
32
33
    /**
34
     * @var int
35
     */
36
    private $installments;
37
38
    /**
39
     * @var string
40
     */
41
    private $expiry;
42
43
    /**
44
     * @var string
45
     */
46
    private $fingerprint;
47
48
    /**
49
     * @var string
50
     */
51
    private $visitorId;
52
53
    /**
54
     * @var string
55
     */
56
    private $internetProtocol;
57
58
    /**
59
     * @var bool
60
     */
61
    private $antifraud;
62
63
    /**
64
     * @var Payment
65
     */
66
    private $payment;
67
68
    /**
69
     * @var Cart
70
     */
71
    private $cart;
72
73
    /**
74
     * @var Customer
75
     */
76
    private $customer;
77
78
    /**
79
     * @var Subscription
80
     */
81
    private $subscription;
82
83
    /**
84
     * @var string
85
     */
86
    private $capture;
87
88
    /**
89
     * @return string
90
     */
91
    public function getOrderId()
92
    {
93
        return $this->orderId;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getOperation()
100
    {
101
        return $this->operation;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getCallbackUrl()
108
    {
109
        return $this->callbackUrl;
110
    }
111
112
    /**
113
     * @return float
114
     */
115
    public function getAmount()
116
    {
117
        return $this->amount;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getInstallments()
124
    {
125
        return $this->installments;
126
    }
127
128
    /**
129
     * @param string $orderId
130
     */
131
    public function setOrderId($orderId)
132
    {
133
        $this->orderId = substr((string) $orderId, 0, 20);
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param string $operation
140
     */
141
    public function setOperation($operation)
142
    {
143
        $this->operation = $operation;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param string $callbackUrl
150
     */
151
    public function setCallbackUrl($callbackUrl)
152
    {
153
        $this->callbackUrl = substr((string) $callbackUrl, 0, 255);
154
155
        return $this;
156
    }
157
158
    /**
159
     * @param float $amount
160
     */
161
    public function setAmount($amount)
162
    {
163
        $this->amount = $this->getNumberUtil()->convertToDouble($amount);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param int $installments
170
     */
171
    public function setInstallments($installments)
172
    {
173
        $this->installments = $this->checkIfInstallmentsIsValidAndReturn($installments);
174
175
        return $this;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getExpiry()
182
    {
183
        return $this->expiry;
184
    }
185
186
    /**
187
     * @param string $expiry
188
     */
189
    public function setExpiry($expiry)
190
    {
191
        if (!$this->getDateUtil()->isValid($expiry)) {
192
            throw new \UnexpectedValueException(
193
                'A data de vencimento não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa'
194
            );
195
        }
196
        $this->expiry = $expiry;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getFingerprint()
205
    {
206
        return $this->fingerprint;
207
    }
208
209
    /**
210
     * @param string $fingerprint
211
     */
212
    public function setFingerprint($fingerprint)
213
    {
214
        $this->fingerprint = substr((string) $fingerprint, 0, 120);
215
216
        return $this;
217
    }
218
219
    /**
220
     * @return string
221
     */
222
    public function getIp()
223
    {
224
        return $this->internetProtocol;
225
    }
226
227
    /**
228
     * @param string $internetProtocol
229
     */
230
    public function setIp($internetProtocol)
231
    {
232
        if (filter_var(trim($internetProtocol), FILTER_VALIDATE_IP)) {
233
            $this->internetProtocol = trim($internetProtocol);
234
        }
235
236
        return $this;
237
    }
238
239
    private function checkIfInstallmentsIsValidAndReturn($installments)
240
    {
241
        if (empty($installments) || $installments < 1) {
242
            $installments = 1;
243
        } elseif ($installments > 12) {
244
            throw new \UnexpectedValueException(
245
                'O parcelamento não pode ser maior que 12 (doze)'
246
            );
247
        }
248
249
        return (int) $installments;
250
    }
251
252
    /**
253
     * @return Payment
254
     */
255
    public function getPayment()
256
    {
257
        if (is_null($this->payment)) {
258
            $this->payment = new Payment();
259
        }
260
261
        return $this->payment;
262
    }
263
264
    /**
265
     * @param Payment $payment
266
     */
267
    public function setPayment(Payment $payment)
268
    {
269
        $this->payment = $payment;
270
271
        return $this;
272
    }
273
274
    /**
275
     * @return Cart
276
     */
277
    public function getCart()
278
    {
279
        if (is_null($this->cart)) {
280
            $this->cart = new Cart();
281
        }
282
283
        return $this->cart;
284
    }
285
286
    /**
287
     * @param Cart $cart
288
     */
289
    public function setCart(Cart $cart)
290
    {
291
        $this->cart = $cart;
292
293
        return $this;
294
    }
295
296
    /**
297
     * @return Customer
298
     */
299
    public function getCustomer()
300
    {
301
        if (is_null($this->customer)) {
302
            $this->customer = new Customer();
303
        }
304
305
        return $this->customer;
306
    }
307
308
    /**
309
     * @param Customer $customer
310
     */
311
    public function setCustomer(Customer $customer)
312
    {
313
        $this->customer = $customer;
314
315
        return $this;
316
    }
317
318
    /**
319
     * @return Subscription
320
     */
321
    public function getSubscription()
322
    {
323
        if (is_null($this->subscription)) {
324
            $this->subscription = new Subscription();
325
        }
326
327
        return $this->subscription;
328
    }
329
330
    /**
331
     * @param Subscription $subscription
332
     */
333
    public function setSubscription(Subscription $subscription)
334
    {
335
        $this->subscription = $subscription;
336
337
        return $this;
338
    }
339
340
    public function serialize()
341
    {
342
        if ($this->isEmpty()) {
343
            throw new \Exception('É necessário informar os dados do Pedido (Order)');
344
        }
345
346
        $_order = [
347
            'pedido'            => urlencode($this->getOrderId()),
348
            'operacao'          => urlencode($this->getOperation()),
349
            'url_retorno'       => urlencode($this->getCallbackUrl()),
350
            'valor'             => urlencode($this->getAmount()),
351
            'parcelas'          => urlencode($this->getInstallments()),
352
            'vencto'            => urlencode($this->getExpiry()),
353
            'stelo_fingerprint' => urlencode($this->getFingerprint()),
354
            'ip'                => urlencode($this->getIp()),
355
            'antifraude'        => urlencode($this->getAntifraud()),
356
            'visitorId'         => urlencode($this->getVisitorId()),
357
            'captura'           => urlencode($this->getCapture()),
358
        ];
359
360
        return array_merge(
361
            $_order,
362
            $this->getPayment()->serialize(),
363
            $this->getCart()->serialize(),
364
            $this->getCustomer()->serialize(),
365
            $this->getSubscription()->serialize()
366
        );
367
    }
368
369
    /**
370
     * @return string
371
     */
372
    public function getAntifraud()
373
    {
374
        return $this->antifraud;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->antifraud returns the type boolean which is incompatible with the documented return type string.
Loading history...
375
    }
376
377
    /**
378
     * @param bool $antifraud
379
     *
380
     * @return self
381
     */
382
    public function setAntifraud($antifraud)
383
    {
384
        $this->antifraud = (bool) $antifraud;
385
386
        return $this;
387
    }
388
389
    /**
390
     * @return string
391
     */
392
    public function getVisitorId()
393
    {
394
        return $this->visitorId;
395
    }
396
397
    /**
398
     * @param string $visitorId
399
     *
400
     * @return self
401
     */
402
    public function setVisitorId($visitorId)
403
    {
404
        $this->visitorId = trim($visitorId);
405
406
        return $this;
407
    }
408
409
    /**
410
     * @return string
411
     */
412
    public function getCapture()
413
    {
414
        if (empty($this->capture)) {
415
            $this->capture = 'p';
416
        }
417
418
        return $this->capture;
419
    }
420
421
    /**
422
     * @param string $capture
423
     *
424
     * @return self
425
     */
426
    public function setCapture($capture)
427
    {
428
        switch ($capture) {
429
            case 'c':
430
                $this->capture = 'c';
431
                break;
432
            case 'a':
433
                $this->capture = 'a';
434
                break;
435
            default:
436
                $this->capture = 'p';
437
                break;
438
        }
439
440
        return $this;
441
    }
442
}
443