1 | <?php |
||
2 | |||
3 | namespace codingFive0\octadesk; |
||
4 | |||
5 | /** |
||
6 | * Class Octadesk |
||
7 | */ |
||
8 | abstract class Octadesk |
||
9 | { |
||
10 | /** @var string <b>E-mail</b> cadastrado na octadesk */ |
||
11 | protected $userName; |
||
12 | /** @var string <b>Senha</b> de acesso na octadesk */ |
||
13 | protected $userPassword; |
||
14 | /** @var string <b>Token</b> obtido apartir do painel de controle da octadesk */ |
||
15 | protected $apiToken; |
||
16 | /** @var string <b><em>xxxxx</em>.octadesk.com<b/> nome do sub-dominio na octadesk. (apenas até o primeiro ponto "." ) */ |
||
17 | protected $subDomain; |
||
18 | |||
19 | /** @var string <b>Base</b> da API. Todas as requisições serão direceionadas a esta URL. Com a adição do endpoint final */ |
||
20 | private $apiUrl; |
||
21 | /** @var string <b>Endpoint</b> Independende a cada solicitação e cada objetivo. */ |
||
22 | private $endpoint; |
||
23 | /** @var string <b>Metodo<b/> da requisição */ |
||
24 | private $method; |
||
25 | /** @var array <b>Campos</b> para postagem no metodo <em>POST</em> */ |
||
26 | private $fields; |
||
27 | |||
28 | /** @var array <b>headers</b> para envio junto a requisição CURL */ |
||
29 | private $headers; |
||
30 | |||
31 | /** @var ?object Retorno de dados por parte da octadesk */ |
||
32 | protected $callback; |
||
33 | |||
34 | /** @var string <b>Erro</b> por mensagem do <em>Componente</em>. Sem interação com a API */ |
||
35 | protected $error; |
||
36 | |||
37 | /** @var string <b>Token</b> retornado a partir do login no API */ |
||
38 | protected $accesToken; |
||
39 | |||
40 | |||
41 | public function __construct($apiToken = null, $userName = null, $userPassword = null, $subDomain = null) |
||
42 | { |
||
43 | $this->userName = $userName; |
||
44 | $this->apiToken = $apiToken; |
||
45 | $this->userPassword = $userPassword; |
||
46 | $this->subDomain = $subDomain; |
||
47 | |||
48 | $this->apiUrl = "https://api.octadesk.services"; |
||
49 | } |
||
50 | |||
51 | protected function setAccesToken($accToken) |
||
52 | { |
||
53 | $this->headers[] = "Authorization: Bearer " . $accToken; |
||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $method |
||
59 | * @param string $endpoint |
||
60 | * @param array|null $fields |
||
61 | * @param array|null $headers |
||
62 | */ |
||
63 | protected function request($method, $endpoint, $fields = null, $headers = null, $toJson = false) |
||
64 | { |
||
65 | $this->method = $method; |
||
66 | $this->endpoint = $endpoint; |
||
67 | $this->fields = (empty($toJson) ? $fields : json_encode($fields)); |
||
68 | $this->setHeaders($headers); |
||
69 | |||
70 | if($toJson){ |
||
71 | $this->headers[] = "Content-Type: application/json"; |
||
72 | $this->headers[] = "accept: application/json"; |
||
73 | } |
||
74 | |||
75 | $this->dispatch(); |
||
76 | } |
||
77 | |||
78 | public function getError() |
||
79 | { |
||
80 | return $this->error; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return object|null |
||
85 | */ |
||
86 | public function callback() |
||
87 | { |
||
88 | return $this->callback; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return object|null |
||
93 | */ |
||
94 | public function error() |
||
95 | { |
||
96 | if (!empty($this->callback->errorId)) { |
||
97 | return $this->callback->message; |
||
98 | } |
||
99 | |||
100 | return null; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param array|null $headers |
||
105 | */ |
||
106 | private function setHeaders($headers = null) |
||
107 | { |
||
108 | if (!$headers) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | foreach ($headers as $key => $header) { |
||
113 | $this->headers[] = "{$key}: {$header}"; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * |
||
119 | */ |
||
120 | private function dispatch() |
||
121 | { |
||
122 | $curl = curl_init(); |
||
123 | |||
124 | $get = ($this->method === "GET" ? "?" . http_build_query($this->fields) : null); |
||
125 | $this->url = "{$this->apiUrl}/{$this->endpoint}{$get}"; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
126 | |||
127 | curl_setopt_array($curl, array( |
||
128 | CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}{$get}", |
||
129 | CURLOPT_RETURNTRANSFER => true, |
||
130 | CURLOPT_MAXREDIRS => 10, |
||
131 | CURLOPT_TIMEOUT => 30, |
||
132 | CURLOPT_FOLLOWLOCATION => true, |
||
133 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
134 | CURLOPT_CUSTOMREQUEST => $this->method, |
||
135 | CURLOPT_POSTFIELDS => $this->fields, |
||
136 | CURLOPT_HTTPHEADER => $this->headers, |
||
137 | )); |
||
138 | |||
139 | $this->callback = json_decode(curl_exec($curl)); |
||
140 | curl_close($curl); |
||
141 | } |
||
142 | |||
143 | public function __clone() |
||
144 | { |
||
145 | } |
||
146 | } |