webhook create&delete
[pharext/pharext.org] / app / Github / API.php
1 <?php
2
3 namespace app\Github;
4
5 use app\Github\Storage;
6 use app\Github\Exception;
7
8 use merry\Config;
9
10 use http\Client;
11 use http\QueryString;
12 use http\Url;
13
14 class API
15 {
16 /**
17 * @var Client
18 */
19 private $client;
20
21 /**
22 * @var Storage
23 */
24 private $tokens;
25
26 /**
27 * @var Storage
28 */
29 private $cache;
30
31 /**
32 * @var merry\Config
33 */
34 private $config;
35
36 function __construct(Config $config, Storage $tokens = null, Storage $cache = null) {
37 $this->config = $config;
38 $this->client = new Client;
39 $this->tokens = $tokens ?: new Storage\Session;
40 $this->cache = $cache;
41 }
42
43 function getConfig() {
44 return $this->config;
45 }
46
47 function getClient() {
48 return $this->client;
49 }
50
51 function getTokenStorage() {
52 return $this->tokens;
53 }
54
55 function getCacheStorage() {
56 return $this->cache;
57 }
58
59 function getCacheKey($ident, $page = null) {
60 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?: 1);
61 }
62
63 function hasToken() {
64 return $this->tokens->get("access_token");
65 }
66
67 function setToken($token) {
68 $this->tokens->set("access_token", $token,
69 $this->config->storage->token->ttl);
70 }
71
72 function getToken() {
73 if ($this->tokens->get("access_token", $token, $ttl, true)) {
74 return $token;
75 }
76 if (isset($ttl)) {
77 throw new Exception\TokenExpired($ttl);
78 }
79 throw new Exception\TokenNotSet;
80 }
81
82 function dropToken() {
83 $this->tokens->del("access_token");
84 }
85
86 function getAuthUrl($callback_url) {
87 $state = base64_encode(openssl_random_pseudo_bytes(24));
88 $this->tokens->set("state", $state, 5*60);
89 $param = [
90 "state" => $state,
91 "client_id" => $this->config->client->id,
92 "scope" => $this->config->client->scope,
93 "redirect_uri" => $callback_url,
94 ];
95 return new Url("https://github.com/login/oauth/authorize", [
96 "query" => new QueryString($param)
97 ], 0);
98 }
99
100 function fetchToken($code, $state, callable $callback) {
101 if (!$this->tokens->get("state", $orig_state, $ttl, true)) {
102 if (isset($ttl)) {
103 throw new Exception\StateExpired($ttl);
104 }
105 throw new Exception\StateNotSet;
106 }
107 if ($state !== $orig_state) {
108 throw new Exception\StateMismatch($orig_state, $state);
109 }
110
111 $fetch = new Fetch\Token($this, compact("code") + $this->config->client->toArray());
112 return $fetch($callback);
113 }
114
115 function fetchUser(callable $callback) {
116 $fetch = new Fetch\User($this);
117 return $fetch($callback);
118 }
119
120 function fetchRepos($page, callable $callback) {
121 $fetch = new Fetch\Repos($this);
122 $fetch->setPage($page);
123 return $fetch($callback);
124 }
125
126 function fetchRepo($repo, callable $callback) {
127 $fetch = new Fetch\Repo($this, compact("repo"));
128 return $fetch($callback);
129 }
130
131 function fetchHooks($repo, callable $callback) {
132 $fetch = new Fetch\Hooks($this, compact("repo"));
133 return $fetch($callback);
134 }
135
136 function fetchReleases($repo, $page, callable $callback) {
137 $fetch = new Fetch\Releases($this, compact("repo"));
138 $fetch->setPage($page);
139 return $fetch($callback);
140 }
141
142 function fetchTags($repo, $page, callable $callback) {
143 $fetch = new Fetch\Tags($this, compact("repo"));
144 $fetch->setPage($page);
145 return $fetch($callback);
146 }
147
148 function fetchContents($repo, $path, callable $callback) {
149 $fetch = new Fetch\Contents($this, compact("repo", "path"));
150 return $fetch($callback);
151 }
152
153 function createRepoHook($repo, callable $callback) {
154 $create = new Create\Webhook($this, compact("repo"));
155 return $create($callback);
156 }
157
158 function deleteRepoHook($repo, $id, callable $callback) {
159 $delete = new Delete\Webhook($this, compact("repo", "id"));
160 return $delete($callback);
161 }
162 }