add logging; fix caching
[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 * Queue this call to the API client
40 */
41 abstract function enqueue(callable $callback);
42
43 /**
44 * @param API $api
45 * @param array $args
46 */
47 function __construct(API $api, array $args = null) {
48 $this->api = $api;
49 $this->config = $this->api->getConfig();
50 $this->url = new Url($this->config->api->url, null, 0);
51
52 if ($args) {
53 $this->args = $args;
54 }
55 if (isset($this->config->api->call->{$this}->args)) {
56 $this->args += $this->config->api->call->{$this}->args->toArray();
57 }
58 }
59
60 function __invoke(callable $callback) {
61 if ($this->readFromCache($cached)) {
62 call_user_func_array($callback, $cached);
63 } else {
64 $this->enqueue($callback);
65 }
66 return $this;
67 }
68
69 /**
70 * Get type of call
71 * @return string
72 */
73 function __toString() {
74 $parts = explode("\\", get_class($this));
75 return strtolower(end($parts));
76 }
77
78 /**
79 * Call Client::send()
80 */
81 function send() {
82 return $this->api->getClient()->send();
83 }
84
85 /**
86 * Get associated cache storage
87 * @param int $ttl out param of configure ttl
88 * @return Storage
89 */
90 function getCache(&$ttl = null) {
91 if (isset($this->config->storage->cache->{$this}->ttl)) {
92 $ttl = $this->config->storage->cache->{$this}->ttl;
93 }
94 return $this->api->getCacheStorage();
95 }
96
97 function getCacheKey() {
98 $args = $this->args;
99 unset($args["fresh"]);
100 if (isset($args["page"]) && !strcmp($args["page"], "1")) {
101 unset($args["page"]);
102 }
103 ksort($args);
104 return sprintf("%s:%s:%s", $this->api->getToken(), $this,
105 new QueryString($args));
106 }
107
108 function readFromCache(array &$value = null) {
109 if (!empty($this->args["fresh"])) {
110 return false;
111 }
112 if (!($cache = $this->api->getCacheStorage())) {
113 return false;
114 }
115 if (!strlen($key = $this->getCacheKey())) {
116 return false;
117 }
118 if (!$cache->get($key, $cached)) {
119 return false;
120 }
121 if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) {
122 return false;
123 }
124 $this->api->getLogger()->debug("Cache-Hit: $this", $this->args);
125 $value = $cached->getValue();
126 return true;
127 }
128
129 function saveToCache(array $fresh) {
130 if (($cache = $this->api->getCacheStorage())) {
131 if (isset($this->config->storage->cache->{$this}->ttl)) {
132 $ttl = $this->config->storage->cache->{$this}->ttl;
133 } else {
134 $ttl = null;
135 }
136
137 $key = $this->getCacheKey();
138 $cache->set($key, new Item($fresh, $ttl));
139 }
140 }
141
142 function dropFromCache() {
143 if (($cache = $this->api->getCacheStorage())) {
144 $key = $this->getCacheKey();
145 $cache->del($key);
146 }
147 }
148 }