Passed
Push — master ( 15546f...a5474d )
by
unknown
04:06
created

Payment::serializePixExpiresIn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
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 Payment implements Emptiable, ObjectSerializable
10
{
11
    use EmptiableTrait;
12
13
    /**
14
     * @var string
15
     */
16
    private $method;
17
18
    /**
19
     * @var CreditCard
20
     */
21
    private $creditCard;
22
23
    /**
24
     * @var array
25
     */
26
    private $instructions = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $splitRules = [];
32
33
    /**
34
     * @var string
35
     */
36
    private $softDescriptor;
37
38
    /**
39
     * @var string
40
     */
41
    private $pixExpiresIn;
42
43
    /**
44
     * @return string
45
     */
46
    public function getMethod()
47
    {
48
        return $this->method;
49
    }
50
51
    /**
52
     * @return CreditCard
53
     */
54
    public function getCreditCard()
55
    {
56
        if (is_null($this->creditCard)) {
57
            $this->creditCard = new CreditCard();
58
        }
59
60
        return $this->creditCard;
61
    }
62
63
    /**
64
     * @param string $method
65
     */
66
    public function setMethod($method)
67
    {
68
        $this->method = $method;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param CreditCard $creditCard
75
     */
76
    public function setCreditCard(CreditCard $creditCard)
77
    {
78
        $this->creditCard = $creditCard;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getInstructions()
87
    {
88
        return $this->instructions;
89
    }
90
91
    /**
92
     * @param string $instructions
93
     */
94
    public function setInstructions($instructions)
95
    {
96
        if ($this->instructionsAreNotFull()) {
97
            $this->instructions[] = substr($instructions, 0, 80);
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getSoftDescriptor()
107
    {
108
        return $this->softDescriptor;
109
    }
110
111
    /**
112
     * @param string $softDescriptor
113
     */
114
    public function setSoftDescriptor($softDescriptor)
115
    {
116
        $this->softDescriptor = substr($softDescriptor, 0, 22);
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return int
123
     */
124
    public function getPixExpiresIn()
125
    {
126
        return $this->pixExpiresIn;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pixExpiresIn returns the type string which is incompatible with the documented return type integer.
Loading history...
127
    }
128
129
    /**
130
     * @param int $pixExpiresIn
131
     */
132
    public function setPixExpiresIn($pixExpiresIn)
133
    {
134
        $this->pixExpiresIn = (int) $pixExpiresIn;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getSplitRules()
143
    {
144
        return $this->splitRules;
145
    }
146
147
    /**
148
     * @param array $splitRule
149
     *
150
     * @return self
151
     */
152
    public function addSplitRule(SplitRule $splitRule)
153
    {
154
        $this->splitRules[] = $splitRule;
155
156
        return $this;
157
    }
158
159
    private function instructionsAreNotFull()
160
    {
161
        return (bool) (count($this->instructions) < 3);
162
    }
163
164
    public function serialize()
165
    {
166
        if ($this->isEmpty()) {
167
            throw new \Exception('É necessário informar os dados do Pagamento (Payment)');
168
        }
169
170
        return array_merge(
171
            [
172
                'metodo' => urlencode($this->getMethod()),
173
            ],
174
            $this->serializeInstructions(),
175
            $this->serializeSoftDescriptor(),
176
            $this->serializePixExpiresIn(),
177
            $this->serializeSplitRules(),
178
            $this->getCreditCard()->serialize()
179
        );
180
    }
181
182
    private function serializeSplitRules()
183
    {
184
        $_splitRules = [];
185
        foreach ($this->getSplitRules() as $key => $splitRule) {
186
            $_splitRules["split[{$key}][seller_id]"] = urlencode($splitRule->getSellerId());
187
            $_splitRules["split[{$key}][percentage]"] = urlencode($splitRule->getPercentage());
188
            $_splitRules["split[{$key}][amount]"] = urlencode($splitRule->getAmount());
189
            $_splitRules["split[{$key}][liable]"] = urlencode($splitRule->getLiable());
190
            $_splitRules["split[{$key}][charge_processing_fee]"] = urlencode($splitRule->getChargeProcessingFee());
191
        }
192
193
        return $_splitRules;
194
    }
195
196
    private function serializeInstructions()
197
    {
198
        $_instructions = [];
199
        foreach ($this->getInstructions() as $key => $instruction) {
200
            $_instructions["instrucoes[{$key}]"] = urlencode($instruction);
201
        }
202
203
        return $_instructions;
204
    }
205
206
    private function serializeSoftDescriptor()
207
    {
208
        $_softDescriptor = [];
209
        $softDescriptor = $this->getSoftDescriptor();
210
211
        if (!empty($softDescriptor)) {
212
            $_softDescriptor['softdescriptor'] = urlencode($softDescriptor);
213
        }
214
215
        return $_softDescriptor;
216
    }
217
218
    private function serializePixExpiresIn()
219
    {
220
        $_pixExpiresIn = [];
221
        $pixExpiresIn = $this->getPixExpiresIn();
222
223
        if (!empty($pixExpiresIn)) {
224
            $_pixExpiresIn['pix_expires_in'] = urlencode($pixExpiresIn);
225
        }
226
227
        return $_pixExpiresIn;
228
    }
229
}
230