prepare url
[pharext/pharext.org] / app / Github / Fetch / Repos.php
1 <?php
2
3 namespace app\Github\Fetch;
4
5 use app\Github\Exception\ReposFetchFailed;
6 use app\Github\Fetch;
7
8 use http\Client\Request;
9 use http\QueryString;
10
11 class Repos extends Fetch
12 {
13 function getRequest() {
14 $url = $this->url->mod([
15 "path" => "/user/repos",
16 "query" => new QueryString([
17 "page" => $this->getPage()
18 ])
19 ]);
20 return new Request("GET", $url, [
21 "Accept" => "application/vnd.github.v3+json",
22 "Authorization" => "token " . $this->api->getToken(),
23 ]);
24 }
25
26 function getException($message, $code, $previous = null) {
27 return new ReposFetchFailed($message, $code, $previous);
28 }
29
30 function getCacheKey() {
31 return $this->api->getCacheKey("repos", $this->page);
32 }
33
34 protected function wrap(callable $callback) {
35 return parent::wrap(function($json, $links) use($callback) {
36 if (($cache = $this->getStorage($ttl))) {
37 foreach ($json as $repo) {
38 $key = $this->api->getCacheKey(sprintf("repo:%s", $repo->full_name));
39 $cache->set($key, [$repo, $links], $ttl);
40 }
41 }
42
43 $callback($json, $links);
44
45 return true;
46 });
47 }
48
49 }