1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vinicinbgs\Autentique\Utils; |
4
|
|
|
|
5
|
|
|
use CURLFile; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class Api |
9
|
|
|
{ |
10
|
|
|
const ACCEPT_CONTENTS = ["json", "form"]; |
11
|
|
|
|
12
|
|
|
const ERR_CONTENT_TYPE = "This content-type not exist"; |
13
|
|
|
const ERR_EMPTY_QUERY = "Query cannot be empty string"; |
14
|
|
|
const ERR_AUTENTIQUE_URL = "AUTENTIQUE_URL cannot be empty"; |
15
|
|
|
const ERR_CURL = "Check your file path"; |
16
|
|
|
const ERR_URL_INVALID = "Invalid url"; |
17
|
|
|
|
18
|
|
|
private $url; |
19
|
|
|
|
20
|
|
|
public function __construct(string $url) |
21
|
|
|
{ |
22
|
|
|
$this->url = $this->setUrl($url); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $token |
27
|
|
|
* @param string $query |
28
|
|
|
* @param string $contentType |
29
|
|
|
* @param string|null $pathFile |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function request( |
33
|
|
|
string $token, |
34
|
|
|
string $query, |
35
|
|
|
string $contentType, |
36
|
|
|
string $pathFile = null |
37
|
|
|
): array { |
38
|
|
|
$this->validateParams($contentType, $query); |
39
|
|
|
|
40
|
|
|
$httpHeader = ["Authorization: Bearer {$token}"]; |
41
|
|
|
|
42
|
|
|
$fields = '{"query":' . $query . "}"; |
43
|
|
|
|
44
|
|
|
if ($contentType == "json") { |
45
|
|
|
$contentType = $this->requestJson(); |
46
|
|
|
} else { |
47
|
|
|
$contentType = $this->requestFormData(); |
48
|
|
|
$fields = [ |
49
|
|
|
"operations" => $fields, |
50
|
|
|
"map" => '{"file": ["variables.file"]}', |
51
|
|
|
"file" => new CURLFile($pathFile), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
array_push($httpHeader, $contentType); |
56
|
|
|
|
57
|
|
|
return $this->connect($httpHeader, $fields); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $httpHeader |
62
|
|
|
* @param string|array $fields |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
private function connect(array $httpHeader, $fields): array |
66
|
|
|
{ |
67
|
|
|
$curl = curl_init(); |
68
|
|
|
|
69
|
|
|
curl_setopt_array( |
70
|
|
|
/** @scrutinizer ignore-type */ |
71
|
|
|
$curl, |
72
|
|
|
[ |
73
|
|
|
CURLOPT_URL => $this->url, |
74
|
|
|
CURLOPT_RETURNTRANSFER => true, |
75
|
|
|
CURLOPT_ENCODING => "", |
76
|
|
|
CURLOPT_MAXREDIRS => 10, |
77
|
|
|
CURLOPT_TIMEOUT => 60, |
78
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
79
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
80
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
81
|
|
|
CURLOPT_POSTFIELDS => $fields, |
82
|
|
|
CURLOPT_HTTPHEADER => $httpHeader, |
83
|
|
|
CURLOPT_CAINFO => __DIR__ . "/../../ssl/ca-bundle.crt", |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$response = curl_exec( |
88
|
|
|
/** @scrutinizer ignore-type */ |
89
|
|
|
$curl |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$errorNo = curl_errno($curl); |
93
|
|
|
|
94
|
|
|
if ($errorNo || $response == "[]") { |
95
|
|
|
throw new Exception(self::ERR_CURL); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
curl_close( |
99
|
|
|
/** @scrutinizer ignore-type */ |
100
|
|
|
$curl |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return is_array($response) ? $response : json_decode($response, true); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function setUrl(string $url): string |
107
|
|
|
{ |
108
|
|
|
if (empty($url)) { |
109
|
|
|
throw new Exception(self::ERR_AUTENTIQUE_URL, 400); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $url; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function validateParams(string $contentType, string $query): void |
116
|
|
|
{ |
117
|
|
|
if (!in_array($contentType, self::ACCEPT_CONTENTS)) { |
118
|
|
|
throw new Exception(self::ERR_CONTENT_TYPE); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (empty($query)) { |
122
|
|
|
throw new Exception(self::ERR_EMPTY_QUERY); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!filter_var($this->url, FILTER_VALIDATE_URL)) { |
126
|
|
|
throw new Exception(self::ERR_URL_INVALID); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function requestJson(): string |
131
|
|
|
{ |
132
|
|
|
return "Content-Type: application/json"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function requestFormData(): string |
136
|
|
|
{ |
137
|
|
|
return "Content-Type: multipart/form-data"; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|