HttpCliente::enviarArquivo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace OBRSDK\HttpClient\Nucleo;
10
11
/**
12
 * Description of ObjetoBoletosRegistradosCliente
13
 *
14
 * @author Antonio
15
 */
16
class HttpCliente implements \OBRSDK\HttpClient\Interfaces\ICoreCliente {
17
18
    /**
19
     * Base URL para acesso a API boletos registrados
20
     */
21
    const BASE_URL = 'http://localhost/objetoboletosregistrados/api.php/';
22
23
    /**
24
     * Versão da API que esse SDK trabalha
25
     */
26
    const API_VERSION = 'v1';
27
28
    /**
29
     * Interface de acesso
30
     * @var \GuzzleHttp\Client 
31
     */
32
    private $client;
33
    private static $instance = null;
34
35
    public function __construct() {
36
        if (self::$instance == null) {
37
            self::$instance = $this;
38
        }
39
40
        $this->client = new \GuzzleHttp\Client([
41
            'base_uri' => self::BASE_URL . self::API_VERSION . '/'
42
        ]);
43
    }
44
45
    private $headers = [];
46
    private $response;
47
    private $requestCalling = false;
48
49
    /**
50
     * 
51
     * @return \OBRSDK\HttpClient\Nucleo\HttpCliente
52
     */
53
    public static function getInstance() {
54
        if (self::$instance == null) {
55
            self::$instance = new HttpCliente();
56
        }
57
58
        return self::$instance;
59
    }
60
61
    /**
62
     * Adiciona um header para a requisicao
63
     * 
64
     * @param string $nome
65
     * @param string $valor
66
     * @return \OBRSDK\HttpClient\Nucleo\HttpCliente
67
     */
68
    public function addHeader($nome, $valor) {
69
        $this->headers[$nome] = $valor;
70
        return $this;
71
    }
72
73
    /**
74
     * Envia um POST para API com JSON no body application/json
75
     * 
76
     * @param string $endpoint
77
     * @param array $body
78
     * @return \OBRSDK\HttpClient\Nucleo\HttpCliente
79
     */
80
    public function postJson($endpoint, array $body = null) {
81
        $this->post($endpoint, $body != null ? ['json' => $body] : null);
82
        return $this;
83
    }
84
85
    /**
86
     * Envia um POST para a API com parametros application/x-www-form-urlencoded
87
     * @param string $endpoint
88
     * @param array $param
89
     * @return \OBRSDK\HttpClient\Nucleo\HttpCliente
90
     */
91
    public function postParam($endpoint, array $param = null) {
92
        $this->post($endpoint, $param != null ? ['form_params' => $param] : null);
93
        return $this;
94
    }
95
96
    /**
97
     * Envia um GET para API
98
     * 
99
     * @param string $endpoint
100
     * @param array $queryString
101
     * @return \OBRSDK\HttpClient\Nucleo\HttpCliente
102
     */
103
    public function get($endpoint, array $queryString = null) {
104
        $this->request('GET', $endpoint, $queryString != null ? ['query' => $queryString] : null);
105
        return $this;
106
    }
107
108
    public function enviarArquivo($endpoint, $arquivo) {
109
        $this->upload($endpoint, $arquivo);
110
        return $this;
111
    }
112
113
    private function post($uri, array $post = null) {
114
        $this->request('POST', $uri, $post);
115
    }
116
117
    public function delete($endpoint) {
118
        $this->request('DELETE', $endpoint);
119
        return $this;
120
    }
121
122
    public function patchJson($endpoint, array $body = null) {
123
        $this->request('PATCH', $endpoint, $body != null ? ['json' => $body] : null);
124
        return $this;
125
    }
126
127
    public function putJson($endpoint, array $body = null) {
128
        $this->request('PUT', $endpoint, $body != null ? ['json' => $body] : null);
129
        return $this;
130
    }
131
132
    public function getResposta($assoc = false) {
133
        $this->requestCalling = false;
134
        $decoded = json_decode($this->response, $assoc);
135
136
        if ($decoded === false) {
137
            throw new \OBRSDK\Exceptions\RespostaException(json_encode([
138
                "mensagem" => "Não foi possivel fazer a leitura da resposta",
139
                "body" => $this->response
140
            ]));
141
        }
142
143
        return $decoded;
144
    }
145
146
    private function upload($uri, $arquivo) {
147
        if (!file_exists($arquivo)) {
148
            throw new \Exception("Arquivo {$arquivo} não encontrado para ser enviado");
149
        }
150
151
        $this->request('POST', $uri, ["__uploadfile__" => $arquivo]);
152
    }
153
154
    private function request($type, $uri, $data = null) {
155
        if ($this->requestCalling) {
156
            throw new \Exception("Não é possivel fazer uma requisicão sem obter a resposta da anterior");
157
        }
158
159
        $dataObjeto = new ApiData($data);
160
        $dataObjeto->addHeaders($this->headers);
161
        $this->headers = [];
162
163
        $requisicao = new ApiRequisicao($this->client, $type, $uri, $dataObjeto);
164
        $this->response = $requisicao->getRespostaConteudo();
165
        $this->requestCalling = true;
166
    }
167
168
}
169