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

Payment::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 13
nc 2
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 Payment implements Emptiable, Serializable
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 string
30
     */
31
    private $softDescriptor;
32
33
    /**
34
     * @return string
35
     */
36
    public function getMethod()
37
    {
38
        return $this->method;
39
    }
40
41
    /**
42
     * @return CreditCard
43
     */
44
    public function getCreditCard()
45
    {
46
        if (is_null($this->creditCard)) {
47
            $this->creditCard = new CreditCard();
48
        }
49
50
        return $this->creditCard;
51
    }
52
53
    /**
54
     * @param string $method
55
     */
56
    public function setMethod($method)
57
    {
58
        $this->method = $method;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param CreditCard $creditCard
65
     */
66
    public function setCreditCard(CreditCard $creditCard)
67
    {
68
        $this->creditCard = $creditCard;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getInstructions()
77
    {
78
        return $this->instructions;
79
    }
80
81
    /**
82
     * @param string $instructions
83
     */
84
    public function setInstructions($instructions)
85
    {
86
        if ($this->instructionsAreNotFull()) {
87
            $this->instructions[] = substr($instructions, 0, 80);
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getSoftDescriptor()
97
    {
98
        return $this->softDescriptor;
99
    }
100
101
    /**
102
     * @param string $softDescriptor
103
     */
104
    public function setSoftDescriptor($softDescriptor)
105
    {
106
        $this->softDescriptor = substr($softDescriptor, 0, 22);
107
108
        return $this;
109
    }
110
111
    private function instructionsAreNotFull()
112
    {
113
        return (bool) (count($this->instructions) < 3);
114
    }
115
116
    public function serialize()
117
    {
118
        if ($this->isEmpty()) {
119
            throw new \Exception('É necessário informar os dados do Pagamento (Payment)');
120
        }
121
122
        $_method = [
123
            'metodo' => urlencode($this->getMethod()),
124
        ];
125
126
        $_instructions = $this->serializeInstructions();
127
128
        $_softDescriptor = $this->serializeSoftDescriptor();
129
130
        $_creditCard = $this->getCreditCard()->serialize();
131
132
        return array_merge(
133
            $_method,
134
            $_instructions,
135
            $_softDescriptor,
136
            $_creditCard
137
        );
138
    }
139
140
    private function serializeInstructions()
141
    {
142
        $_instructions = [];
143
        foreach ($this->getInstructions() as $key => $instruction) {
144
            $_instructions["instrucoes[{$key}]"] = urlencode($instruction);
145
        }
146
147
        return $_instructions;
148
    }
149
150
    private function serializeSoftDescriptor()
151
    {
152
        $_softDescriptor = [];
153
        $softDescriptor = $this->getSoftDescriptor();
154
155
        if (!empty($softDescriptor)) {
156
            $_softDescriptor['softdescriptor'] = urlencode($softDescriptor);
157
        }
158
159
        return $_softDescriptor;
160
    }
161
}
162