Passed
Branch refactoring (232535)
by João Felipe Magro
01:20
created

Order::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 16
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 Payment
50
     */
51
    private $payment;
52
53
    /**
54
     * @var Cart
55
     */
56
    private $cart;
57
58
    /**
59
     * @var Customer
60
     */
61
    private $customer;
62
63
    /**
64
     * @var Subscription
65
     */
66
    private $subscription;
67
68
    /**
69
     * @return string
70
     */
71
    public function getOrderId()
72
    {
73
        return $this->orderId;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getOperation()
80
    {
81
        return $this->operation;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getCallbackUrl()
88
    {
89
        return $this->callbackUrl;
90
    }
91
92
    /**
93
     * @return float
94
     */
95
    public function getAmount()
96
    {
97
        return $this->amount;
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function getInstallments()
104
    {
105
        return $this->installments;
106
    }
107
108
    /**
109
     * @param string $orderId
110
     */
111
    public function setOrderId($orderId)
112
    {
113
        $this->orderId = substr((string) $orderId, 0, 20);
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $operation
120
     */
121
    public function setOperation($operation)
122
    {
123
        $this->operation = $operation;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param string $callbackUrl
130
     */
131
    public function setCallbackUrl($callbackUrl)
132
    {
133
        $this->callbackUrl = substr((string) $callbackUrl, 0, 255);
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param float $amount
140
     */
141
    public function setAmount($amount)
142
    {
143
        $this->amount = $this->getNumberUtil()->convertToDouble($amount);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param int $installments
150
     */
151
    public function setInstallments($installments)
152
    {
153
        $this->installments = $this->checkIfInstallmentsIsValidAndReturn($installments);
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getExpiry()
162
    {
163
        return $this->expiry;
164
    }
165
166
    /**
167
     * @param string $expiry
168
     */
169
    public function setExpiry($expiry)
170
    {
171
        if (!$this->getDateUtil()->isValid($expiry)) {
172
            throw new \UnexpectedValueException(
173
                'A data de vencimento não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa'
174
            );
175
        }
176
        $this->expiry = $expiry;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getFingerprint()
185
    {
186
        return $this->fingerprint;
187
    }
188
189
    /**
190
     * @param string $fingerprint
191
     */
192
    public function setFingerprint($fingerprint)
193
    {
194
        $this->fingerprint = substr((string) $fingerprint, 0, 120);
195
196
        return $this;
197
    }
198
199
    private function checkIfInstallmentsIsValidAndReturn($installments)
200
    {
201
        if (empty($installments) || $installments < 1) {
202
            $installments = 1;
203
        } elseif ($installments > 12) {
204
            throw new \UnexpectedValueException(
205
                'O parcelamento não pode ser maior que 12 (doze)'
206
            );
207
        }
208
209
        return (int) $installments;
210
    }
211
212
    /**
213
     * @return Payment
214
     */
215
    public function getPayment()
216
    {
217
        if (is_null($this->payment)) {
218
            $this->payment = new Payment();
219
        }
220
221
        return $this->payment;
222
    }
223
224
    /**
225
     * @param Payment $payment
226
     */
227
    public function setPayment(Payment $payment)
228
    {
229
        $this->payment = $payment;
230
231
        return $this;
232
    }
233
234
    /**
235
     * @return Cart
236
     */
237
    public function getCart()
238
    {
239
        if (is_null($this->cart)) {
240
            $this->cart = new Cart();
241
        }
242
243
        return $this->cart;
244
    }
245
246
    /**
247
     * @param Cart $cart
248
     */
249
    public function setCart(Cart $cart)
250
    {
251
        $this->cart = $cart;
252
253
        return $this;
254
    }
255
256
    /**
257
     * @return Customer
258
     */
259
    public function getCustomer()
260
    {
261
        if (is_null($this->customer)) {
262
            $this->customer = new Customer();
263
        }
264
265
        return $this->customer;
266
    }
267
268
    /**
269
     * @param Customer $customer
270
     */
271
    public function setCustomer(Customer $customer)
272
    {
273
        $this->customer = $customer;
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return Subscription
280
     */
281
    public function getSubscription()
282
    {
283
        if (is_null($this->subscription)) {
284
            $this->subscription = new Subscription();
285
        }
286
287
        return $this->subscription;
288
    }
289
290
    /**
291
     * @param Subscription $subscription
292
     */
293
    public function setSubscription(Subscription $subscription)
294
    {
295
        $this->subscription = $subscription;
296
297
        return $this;
298
    }
299
300
    public function serialize()
301
    {
302
        if ($this->isEmpty()) {
303
            throw new \Exception('É necessário informar os dados do Pedido (Order)');
304
        }
305
306
        $_order = [
307
            'pedido'            => urlencode($this->getOrderId()),
308
            'operacao'          => urlencode($this->getOperation()),
309
            'url_retorno'       => urlencode($this->getCallbackUrl()),
310
            'valor'             => urlencode($this->getAmount()),
311
            'parcelas'          => urlencode($this->getInstallments()),
312
            'vencto'            => urlencode($this->getExpiry()),
313
            'stelo_fingerprint' => urlencode($this->getFingerprint()),
314
        ];
315
316
        return array_merge(
317
            $_order,
318
            $this->getPayment()->serialize(),
319
            $this->getCart()->serialize(),
320
            $this->getCustomer()->serialize(),
321
            $this->getSubscription()->serialize()
322
        );
323
    }
324
}
325