yet another github api refactoring
[pharext/pharext.org] / app / Github / API / Call.php
1 <?php
2
3 namespace app\Github\API;
4
5 use app\Github\API;
6 use app\Github\Storage\Item;
7 use http\QueryString;
8 use http\Url;
9 use merry\Config;
10
11 abstract class Call
12 {
13 /**
14 * @var Config
15 */
16 protected $config;
17
18 /**
19 * @var \app\Gituhub\API
20 */
21 protected $api;
22
23 /**
24 * @var array
25 */
26 protected $args = [];
27
28 /**
29 * @var \http\Url
30 */
31 protected $url;
32
33 /**
34 * @var QueryString
35 */
36 protected $query;
37
38 /**
39 * @var array
40 */
41 protected $result;
42
43 /**
44 * Queue this call to the API client
45 */
46 abstract function enqueue(callable $callback);
47
48 /**
49 * @param API $api
50 * @param array $args
51 */
52 function __construct(API $api, array $args = null) {
53 $this->api = $api;
54 $this->config = $this->api->getConfig();
55 $this->url = new Url($this->config->api->url, null, 0);
56
57 if ($args) {
58 $this->args = $args;
59 }
60 if (isset($this->config->api->call->{$this}->args)) {
61 $this->args += $this->config->api->call->{$this}->args->toArray();
62 }
63 }
64
65 function __invoke(callable $callback) {
66 if ($this->readFromCache($this->result)) {
67 call_user_func_array($callback, $this->result);
68 } else {
69 $this->enqueue($callback);
70 }
71 return $this;
72 }
73
74 /**
75 * Get type of call
76 * @return string
77 */
78 function __toString() {
79 $parts = explode("\\", get_class($this));
80 return strtolower(end($parts));
81 }
82
83 /**
84 * Call Client::send()
85 */
86 function send() {
87 $this->api->getClient()->send();
88 return $this->result;
89 }
90
91 /**
92 * Get associated cache storage
93 * @param int $ttl out param of configure ttl
94 * @return Storage
95 */
96 function getCache(&$ttl = null) {
97 if (isset($this->config->storage->cache->{$this}->ttl)) {
98 $ttl = $this->config->storage->cache->{$this}->ttl;
99 }
100 return $this->api->getCacheStorage();
101 }
102
103 function getCacheKey() {
104 $args = $this->args;
105 unset($args["fresh"]);
106 if (isset($args["page"]) && !strcmp($args["page"], "1")) {
107 unset($args["page"]);
108 }
109 ksort($args);
110 return sprintf("%s:%s:%s", $this->api->getToken(), $this,
111 new QueryString($args));
112 }
113
114 function readFromCache(array &$value = null) {
115 if (!empty($this->args["fresh"])) {
116 return false;
117 }
118 if (!($cache = $this->api->getCacheStorage())) {
119 return false;
120 }
121 if (!strlen($key = $this->getCacheKey())) {
122 return false;
123 }
124 if (!$cache->get($key, $cached)) {
125 return false;
126 }
127 if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) {
128 return false;
129 }
130 $this->api->getLogger()->debug("Cache-Hit: $this", $this->args);
131 $value = $cached->getValue();
132 return true;
133 }
134
135 function saveToCache(array $fresh) {
136 if (($cache = $this->api->getCacheStorage())) {
137 if (isset($this->config->storage->cache->{$this}->ttl)) {
138 $ttl = $this->config->storage->cache->{$this}->ttl;
139 } else {
140 $ttl = null;
141 }
142
143 $key = $this->getCacheKey();
144 $cache->set($key, new Item($fresh, $ttl));
145 }
146 }
147
148 function dropFromCache() {
149 if (($cache = $this->api->getCacheStorage())) {
150 $key = $this->getCacheKey();
151 $cache->del($key);
152 }
153 }
154 }