d84a24c673f71e438d2a5bb60cb14fceb28124aa
[m6w6/seekat] / lib / API / Call.php
1 <?php
2
3 namespace seekat\API;
4
5 use http\Url;
6 use React\Promise\ExtendedPromiseInterface;
7 use seekat\API;
8 use seekat\Exception;
9
10 class Call
11 {
12 /**
13 * @var API
14 */
15 private $api;
16
17 /**
18 * @var string
19 */
20 private $call;
21
22 function __construct(API $api, string $call) {
23 $this->api = $api;
24 $this->call = $call;
25 }
26
27 function __invoke(array $args) : ExtendedPromiseInterface {
28 $promise = $this->api->{$this->call}->get(...$args);
29
30 /* fetch resource, unless already localized, and try for {$method}_url */
31 if (!$this->api->exists($this->call)) {
32 $promise = $promise->otherwise(function ($error) use($args) {
33 if ($this->api->exists($this->call."_url", $url)) {
34 $url = new Url(uri_template($url, (array)current($args)));
35 return $this->api->withUrl($url)->get(...$args);
36 }
37
38 $message = Exception\message($error);
39 $this->api->getLogger()->error("call($this->call): " . $message, [
40 "url" => (string) $this->api->getUrl()
41 ]);
42
43 throw $error;
44 });
45 }
46
47 return $promise;
48 }
49 }