Passed
Push — main ( b5dda5...51a94e )
by Leandro
01:35
created

Itau::getCertificateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Itau\API;
3
4
use Itau\API\BoleCode\BoleCode;
5
use Itau\API\BoleCode\BoleCodeResponse;
6
use Itau\API\Boleto\Boleto;
7
use Itau\API\Boleto\BoletoResponse;
8
use Itau\API\Pix\Pix;
9
use Itau\API\Pix\PixResponse;
10
use Itau\API\Vencimento\Vencimento;
11
12
/**
13
 * Class Itau
14
 *
15
 * @package Itau\API
16
 */
17
class Itau
18
{
19
20
    private string $client_id;
21
    private string $client_secret;
22
    private string $certificate;
23
    private string $certificateKey;
24
    private $environment;
25
    private $authorizationToken;
26
27
    private $debug = false;
28
29
    public function __construct(string $client_id, string $client_secret, string $certificate, string $certificateKey)
30
    {
31
        $this->setClientId($client_id);
32
        $this->setClientSecret($client_secret);
33
        $this->setCertificate($certificate);
34
        $this->setCertificateKey($certificateKey);
35
        $this->setEnvironment(Environment::production());
36
    }
37
38
    /**
39
     *
40
     * @return string
41
     */
42
    public function getClientId()
43
    {
44
        return $this->client_id;
45
    }
46
47
    /**
48
     *
49
     * @param string $client_id
50
     */
51
    public function setClientId($client_id)
52
    {
53
        $this->client_id = (string) $client_id;
54
55
        return $this;
56
    }
57
58
    /**
59
     *
60
     * @return mixed
61
     */
62
    public function getClientSecret()
63
    {
64
        return $this->client_secret;
65
    }
66
67
    /**
68
     *
69
     * @param mixed $client_secret
70
     */
71
    public function setClientSecret($client_secret)
72
    {
73
        $this->client_secret = (string) $client_secret;
74
75
        return $this;
76
    }
77
78
    public function getCertificate()
79
    {
80
        return $this->certificate;
81
    }
82
83
    public function setCertificate($certificate)
84
    {
85
        $this->certificate = (string) $certificate;
86
87
        return $this;
88
    }
89
90
    public function getCertificateKey()
91
    {
92
        return $this->certificateKey;
93
    }
94
95
    public function setCertificateKey($certificateKey)
96
    {
97
        $this->certificateKey = (string) $certificateKey;
98
99
        return $this;
100
    }
101
102
    public function getEnvironment(): Environment
103
    {
104
        return $this->environment;
105
    }
106
107
    public function setEnvironment(Environment $environment): self
108
    {
109
        $this->environment = $environment;
110
111
        return $this;
112
    }
113
114
    /**
115
     *
116
     * @return mixed
117
     */
118
    public function getAuthorizationToken()
119
    {
120
        return $this->authorizationToken;
121
    }
122
123
    /**
124
     *
125
     * @param mixed $authorizationToken
126
     */
127
    public function setAuthorizationToken($authorizationToken)
128
    {
129
        $this->authorizationToken = (string) $authorizationToken;
130
131
        return $this;
132
    }
133
134
    /**
135
     *
136
     * @return bool|null
137
     */
138
    public function getDebug()
139
    {
140
        return $this->debug;
141
    }
142
143
    /**
144
     *
145
     * @param bool|null $debug
146
     */
147
    public function setDebug($debug = false)
148
    {
149
        $this->debug = $debug;
150
151
        return $this;
152
    }
153
154
    public function pix(Pix $pix): PixResponse
155
    {
156
        $pixResponse = new PixResponse();
157
        try{
158
            if ($this->debug) {
159
                print $pix->toJSON();
160
            }
161
162
            $request = new Request($this);
163
            $response = $request->post($this, "{$this->getEnvironment()->getApiPixUrl()}/cob", $pix->toJSON());
164
           
165
            
166
            // Add fields do not return in response
167
            $pixResponse->mapperJson($pix->toArray());
168
            // Add response fields
169
            $pixResponse->mapperJson($response);
170
            $pixResponse->setStatus(BaseResponse::STATUS_CONFIRMED);
171
            return $pixResponse;
172
173
        } catch (\Exception $e) {
174
            return $this->generateErrorResponse($pixResponse, $e);
175
        }
176
    }
177
178
    public function boleCode(BoleCode $boleCode): BoleCodeResponse
179
    {
180
        $boleCodeResponse = new BoleCodeResponse();
181
        try{
182
            if ($this->debug) {
183
                print $boleCode->toJSON();
184
            }
185
186
            $request = new Request($this);
187
            $response = $request->post($this, "{$this->getEnvironment()->getApiBoleCodeUrl()}/boletos_pix", $boleCode->toJSON());
188
           
189
            // Add fields do not return in response
190
            $boleCodeResponse->mapperJson($boleCode->toArray());
191
            // Add response fields
192
            $boleCodeResponse->mapperJson($response);
193
            $boleCodeResponse->setStatus(BaseResponse::STATUS_CONFIRMED);
194
            return $boleCodeResponse;
195
196
        } catch (\Exception $e) {
197
            return $this->generateErrorResponse($boleCodeResponse, $e);
198
        }
199
    }
200
201
    public function alterarVencimentoBoleto($agencia, $contaComDigito, $carteira, $nossoNumero, Vencimento $vencimento)
202
    {
203
        $boletoResponse = new BoletoResponse();
204
205
        $path = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT).str_pad($carteira, 3, '0', STR_PAD_LEFT).str_pad($nossoNumero, 8, '0', STR_PAD_LEFT);
206
        $request = new Request($this);
207
        $response = $request->patch($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos/{$path}/data_vencimento", $vencimento->toJSON());
208
        $boletoResponse->mapperJson($response);
209
210
        return $boletoResponse;
211
    }
212
213
    public function consultarBoleto($agencia, $contaComDigito, $nossoNumero)
214
    {
215
        $boletoResponse = new BoletoResponse();
216
217
        $id_beneficiario = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT);
218
        $nosso_numero = str_pad($nossoNumero, 8, '0', STR_PAD_LEFT);
219
        $request = new Request($this);
220
        $response = $request->get($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos?id_beneficiario={$id_beneficiario}&nosso_numero={$nosso_numero}");
221
        // Add response fields
222
        $boletoResponse->mapperJson($response);
223
224
        return $boletoResponse;
225
    }
226
227
    public function baixarBoleto($agencia, $contaComDigito, $carteira, $nossoNumero)
228
    {
229
        $boletoResponse = new BoletoResponse();
230
231
        $path = str_pad($agencia, 4, '0', STR_PAD_LEFT).str_pad($contaComDigito, 8, '0', STR_PAD_LEFT).str_pad($carteira, 3, '0', STR_PAD_LEFT).str_pad($nossoNumero, 8, '0', STR_PAD_LEFT);
232
        $request = new Request($this);
233
        $response = $request->patch($this, "{$this->getEnvironment()->getApiBoletoUrl()}/boletos/{$path}/baixa");
234
        // Add response fields
235
        $boletoResponse->mapperJson($response);
236
237
        return $boletoResponse;
238
    }
239
240
    private function generateErrorResponse(BaseResponse $baseResponse, $e)
241
    {
242
        $baseResponse->mapperJson(json_decode($e->getMessage(), true));
243
        
244
        if (empty($baseResponse->getStatus())) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $baseResponse->getStatus() targeting Itau\API\BaseResponse::getStatus() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
245
            $baseResponse->setStatus(BaseResponse::STATUS_ERROR);
246
        }
247
        
248
        return $baseResponse;
249
    }
250
}
251