Completed
Push — master ( dc30b6...cc4a0b )
by João Felipe Magro
03:10
created

Payment::serializeSplitRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
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 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
     * @return string
40
     */
41
    public function getMethod()
42
    {
43
        return $this->method;
44
    }
45
46
    /**
47
     * @return CreditCard
48
     */
49
    public function getCreditCard()
50
    {
51
        if (is_null($this->creditCard)) {
52
            $this->creditCard = new CreditCard();
53
        }
54
55
        return $this->creditCard;
56
    }
57
58
    /**
59
     * @param string $method
60
     */
61
    public function setMethod($method)
62
    {
63
        $this->method = $method;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param CreditCard $creditCard
70
     */
71
    public function setCreditCard(CreditCard $creditCard)
72
    {
73
        $this->creditCard = $creditCard;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getInstructions()
82
    {
83
        return $this->instructions;
84
    }
85
86
    /**
87
     * @param string $instructions
88
     */
89
    public function setInstructions($instructions)
90
    {
91
        if ($this->instructionsAreNotFull()) {
92
            $this->instructions[] = substr($instructions, 0, 80);
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getSoftDescriptor()
102
    {
103
        return $this->softDescriptor;
104
    }
105
106
    /**
107
     * @param string $softDescriptor
108
     */
109
    public function setSoftDescriptor($softDescriptor)
110
    {
111
        $this->softDescriptor = substr($softDescriptor, 0, 22);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getSplitRules()
120
    {
121
        return $this->splitRules;
122
    }
123
124
    /**
125
     * @param array $splitRule
126
     *
127
     * @return self
128
     */
129
    public function addSplitRule(SplitRule $splitRule)
130
    {
131
        $this->splitRules[] = $splitRule;
132
133
        return $this;
134
    }
135
136
    private function instructionsAreNotFull()
137
    {
138
        return (bool) (count($this->instructions) < 3);
139
    }
140
141
    public function serialize()
142
    {
143
        if ($this->isEmpty()) {
144
            throw new \Exception('É necessário informar os dados do Pagamento (Payment)');
145
        }
146
147
        return array_merge(
148
            [
149
                'metodo' => urlencode($this->getMethod()),
150
            ],
151
            $this->serializeInstructions(),
152
            $this->serializeSoftDescriptor(),
153
            $this->serializeSplitRules(),
154
            $this->getCreditCard()->serialize()
155
        );
156
    }
157
158
    private function serializeSplitRules()
159
    {
160
        $_splitRules = [];
161
        foreach ($this->getSplitRules() as $key => $splitRule) {
162
            $_splitRules["split[{$key}][seller_id]"] = urlencode($splitRule->getSellerId());
163
            $_splitRules["split[{$key}][percentage]"] = urlencode($splitRule->getPercentage());
164
            $_splitRules["split[{$key}][amount]"] = urlencode($splitRule->getAmount());
165
            $_splitRules["split[{$key}][liable]"] = urlencode($splitRule->getLiable());
166
            $_splitRules["split[{$key}][charge_processing_fee]"] = urlencode($splitRule->getChargeProcessingFee());
167
        }
168
169
        return $_splitRules;
170
    }
171
172
    private function serializeInstructions()
173
    {
174
        $_instructions = [];
175
        foreach ($this->getInstructions() as $key => $instruction) {
176
            $_instructions["instrucoes[{$key}]"] = urlencode($instruction);
177
        }
178
179
        return $_instructions;
180
    }
181
182
    private function serializeSoftDescriptor()
183
    {
184
        $_softDescriptor = [];
185
        $softDescriptor = $this->getSoftDescriptor();
186
187
        if (!empty($softDescriptor)) {
188
            $_softDescriptor['softdescriptor'] = urlencode($softDescriptor);
189
        }
190
191
        return $_softDescriptor;
192
    }
193
}
194