1 | <?php |
||
2 | |||
3 | namespace Codeat3\FoaasClient; |
||
4 | |||
5 | use Codeat3\FoaasClient\Exceptions\InvalidArguments; |
||
6 | use Codeat3\FoaasClient\Exceptions\InvalidMethodCall; |
||
7 | use Codeat3\FoaasClient\Response\ArrayResponse; |
||
8 | use Codeat3\FoaasClient\Response\HtmlResponse; |
||
9 | use Codeat3\FoaasClient\Response\JsonResponse; |
||
10 | use Codeat3\FoaasClient\Response\TextResponse; |
||
11 | use Codeat3\FoaasClient\Response\XmlResponse; |
||
12 | use Codeat3\FoaasClient\ResponseFilters\Filter; |
||
13 | use Codeat3\FoaasClient\ResponseFilters\NoFilter; |
||
14 | use GuzzleHttp\Client; |
||
15 | |||
16 | /** |
||
17 | * @method anyway(string $company, string $from) |
||
18 | * @method asshole(string $from) |
||
19 | * @method awesome(string $from) |
||
20 | * @method back(string $name, string $from) |
||
21 | * @method bag(string $from) |
||
22 | * @method ballmer(string $name, string $company, string $from) |
||
23 | * @method bday(string $name, string $from) |
||
24 | * @method because(string $from) |
||
25 | * @method blackadder(string $name, string $from) |
||
26 | * @method bm(string $name, string $from) |
||
27 | * @method bucket(string $from) |
||
28 | * @method bus(string $name, string $from) |
||
29 | * @method bye(string $from) |
||
30 | * @method caniuse(string $tool, string $from) |
||
31 | * @method chainsaw(string $name, string $from) |
||
32 | * @method cocksplat(string $name, string $from) |
||
33 | * @method cool(string $from) |
||
34 | * @method cup(string $from) |
||
35 | * @method dalton(string $name, string $from) |
||
36 | * @method deraadt(string $name, string $from) |
||
37 | * @method diabetes(string $from) |
||
38 | * @method donut(string $name, string $from) |
||
39 | * @method dosomething(string $do, string $something, string $from) |
||
40 | * @method equity(string $name, string $from) |
||
41 | * @method everyone(string $from) |
||
42 | * @method everything(string $from) |
||
43 | * @method family(string $from) |
||
44 | * @method fascinating(string $from) |
||
45 | * @method field(string $name, string $from, string $reference) |
||
46 | * @method flying(string $from) |
||
47 | * @method ftfy(string $from) |
||
48 | * @method fts(string $name, string $from) |
||
49 | * @method fyyff(string $from) |
||
50 | * @method gfy(string $name, string $from) |
||
51 | * @method give(string $from) |
||
52 | * @method greed(string $noun, string $from) |
||
53 | * @method horse(string $from) |
||
54 | * @method immensity(string $from) |
||
55 | * @method ing(string $name, string $from) |
||
56 | * @method jinglebells(string $from) |
||
57 | * @method keep(string $name, string $from) |
||
58 | * @method keepcalm(string $reaction, string $from) |
||
59 | * @method king(string $name, string $from) |
||
60 | * @method life(string $from) |
||
61 | * @method linus(string $name, string $from) |
||
62 | * @method logs(string $from) |
||
63 | * @method look(string $name, string $from) |
||
64 | * @method looking(string $from) |
||
65 | * @method madison(string $name, string $from) |
||
66 | * @method maybe(string $from) |
||
67 | * @method me(string $from) |
||
68 | * @method mornin(string $from) |
||
69 | * @method no(string $from) |
||
70 | * @method nugget(string $name, string $from) |
||
71 | * @method off(string $name, string $from) |
||
72 | * @method off-with(string $behavior, string $from) |
||
73 | * @method outside(string $name, string $from) |
||
74 | * @method particular(string $thing, string $from) |
||
75 | * @method pink(string $from) |
||
76 | * @method problem(string $name, string $from) |
||
77 | * @method programmer(string $from) |
||
78 | * @method pulp(string $language, string $from) |
||
79 | * @method question(string $from) |
||
80 | * @method ratsarse(string $from) |
||
81 | * @method retard(string $from) |
||
82 | * @method ridiculous(string $from) |
||
83 | * @method rtfm(string $from) |
||
84 | * @method sake(string $from) |
||
85 | * @method shakespeare(string $name, string $from) |
||
86 | * @method shit(string $from) |
||
87 | * @method shutup(string $name, string $from) |
||
88 | * @method single(string $from) |
||
89 | * @method thanks(string $from) |
||
90 | * @method that(string $from) |
||
91 | * @method think(string $name, string $from) |
||
92 | * @method thinking(string $name, string $from) |
||
93 | * @method this(string $from) |
||
94 | * @method thumbs(string $name, string $from) |
||
95 | * @method too(string $from) |
||
96 | * @method tucker(string $from) |
||
97 | * @method version(string ) |
||
98 | * @method waste(string $name, string $from) |
||
99 | * @method what(string $from) |
||
100 | * @method xmas(string $name, string $from) |
||
101 | * @method yoda(string $name, string $from) |
||
102 | * @method you(string $name, string $from) |
||
103 | * @method zayn(string $from) |
||
104 | * @method zero(string $from) |
||
105 | */ |
||
106 | class FoaasClient |
||
107 | { |
||
108 | protected static $endpoints; |
||
109 | |||
110 | protected $responseType; |
||
111 | |||
112 | protected static $responseAs; |
||
113 | |||
114 | protected $url; |
||
115 | |||
116 | protected $responseTypeMap = [ |
||
117 | 'xml' => XmlResponse::class, |
||
118 | 'json' => JsonResponse::class, |
||
119 | 'text' => TextResponse::class, |
||
120 | 'html' => HtmlResponse::class, |
||
121 | 'array' => ArrayResponse::class, |
||
122 | ]; |
||
123 | |||
124 | protected $method; |
||
125 | |||
126 | protected $arguments; |
||
127 | |||
128 | protected $decencyLevel; |
||
129 | |||
130 | public function __construct(array $config = []) |
||
131 | { |
||
132 | $this->decencyLevel = $config['decency'] ?? null; |
||
133 | $this->responseType = $config['responseAs'] ?? null; |
||
134 | |||
135 | $this->responseTypeMap = array_merge($this->responseTypeMap, ResponseFormatValidator::validate($config['responseFormats'] ?? [])); |
||
136 | } |
||
137 | |||
138 | private function getResponseType() |
||
139 | { |
||
140 | if (is_null(self::$responseAs)) { |
||
141 | if (array_key_exists($this->responseType, $this->responseTypeMap)) { |
||
142 | self::$responseAs = new $this->responseTypeMap[$this->responseType](); |
||
143 | } else { |
||
144 | return new $this->responseTypeMap['json'](); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | return self::$responseAs; |
||
149 | } |
||
150 | |||
151 | public function guzzleClient() |
||
152 | { |
||
153 | $headers = $this->getResponseType()->getHeaders(); |
||
154 | |||
155 | return new Client([ |
||
156 | 'headers' => [ |
||
157 | 'Accept' => $headers, |
||
158 | ], |
||
159 | ]); |
||
160 | } |
||
161 | |||
162 | public function __call($method, $arguments) |
||
163 | { |
||
164 | // check if the method is supported |
||
165 | if (array_key_exists($method, $this->getAvailableEndpoints())) { |
||
166 | $argumentsExpected = $this->getAvailableEndpoints()[$method]['fields']; |
||
167 | if (count($arguments) === count($argumentsExpected)) { |
||
168 | $this->url = UrlBuilder::buildUrl($method, $arguments); |
||
169 | } else { |
||
170 | throw new InvalidArguments(); |
||
171 | } |
||
172 | } else { |
||
173 | throw new InvalidMethodCall(); |
||
174 | } |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | public function getAvailableEndpoints(): array |
||
180 | { |
||
181 | if (is_null(self::$endpoints)) { |
||
182 | $response = json_decode($this->apiCall(UrlBuilder::buildUrl('operations')), true); |
||
183 | self::$responseAs = null; |
||
184 | foreach ($response as $operation) { |
||
185 | $arrUrl = array_filter(explode('/', $operation['url'])); |
||
186 | $endpoint = $arrUrl[1]; |
||
187 | $fields = array_slice($arrUrl, 1); |
||
188 | self::$endpoints[$endpoint] = [ |
||
189 | 'fields' => $fields, |
||
190 | 'name' => $operation['name'], |
||
191 | ]; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | return self::$endpoints; |
||
196 | } |
||
197 | |||
198 | public function apiCall($path) |
||
199 | { |
||
200 | // echo $path . PHP_EOL; |
||
201 | $response = $this->guzzleClient()->get($path); |
||
202 | |||
203 | return $response->getBody()->getContents(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * A method to get the response. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function get() |
||
212 | { |
||
213 | $apiResponse = $this->apiCall($this->url); |
||
214 | |||
215 | $filter = (empty($this->decencyLevel)) |
||
216 | ? new NoFilter() |
||
217 | : new Filter($this->decencyLevel); |
||
218 | |||
219 | return $this->getResponseType()->response($apiResponse, $filter); |
||
220 | } |
||
221 | |||
222 | private function resetResponseType($type) |
||
223 | { |
||
224 | self::$responseAs = null; |
||
225 | $this->responseType = $type; |
||
226 | } |
||
227 | |||
228 | public function getAsJson(): string |
||
229 | { |
||
230 | $this->resetResponseType('json'); |
||
231 | |||
232 | return $this->get(); |
||
233 | } |
||
234 | |||
235 | public function getAsXml(): string |
||
236 | { |
||
237 | $this->resetResponseType('xml'); |
||
238 | |||
239 | return $this->get(); |
||
240 | } |
||
241 | |||
242 | public function getAsArray(): array |
||
243 | { |
||
244 | $this->resetResponseType('array'); |
||
245 | |||
246 | return $this->get(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
247 | } |
||
248 | |||
249 | public function getAsText(): string |
||
250 | { |
||
251 | $this->resetResponseType('text'); |
||
252 | |||
253 | return $this->get(); |
||
254 | } |
||
255 | |||
256 | public function getAsHtml(): string |
||
257 | { |
||
258 | $this->resetResponseType('html'); |
||
259 | |||
260 | return $this->get(); |
||
261 | } |
||
262 | } |
||
263 |