1c9ff7834e772a92cc8fb758bb8701bfaae6bf88
6 use app\Github\Storage
;
7 use app\Github\Exception
;
15 use Psr\Log\LoggerInterface
;
45 * @var \Psr\Log\LoggerInterface;
49 function __construct(Config
$config, LoggerInterface
$logger, Storage
$tokens = null, Storage
$cache = null) {
50 $this->logger
= $logger;
51 $this->config
= $config;
52 $this->client
= new Client
;
53 $this->client
->attach(new ClientObserver($logger));
54 $this->tokens
= $tokens ?
: new Storage\Session
;
55 $this->cache
= $cache;
59 * Set maximum acceptable age of cache items
62 function setMaxAge($seconds) {
63 $this->maxAge
= $seconds;
67 function getMaxAge() {
71 function getLogger() {
75 function getConfig() {
79 function getClient() {
83 function getTokenStorage() {
87 function getCacheStorage() {
91 function getCacheKey($ident, $page = null) {
92 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
96 return $this->tokens
->get("access_token");
99 function setToken($token) {
100 $this->tokens
->set("access_token", new Storage\
Item(
102 $this->config
->storage
->token
->ttl
106 function getToken() {
107 if ($this->tokens
->get("access_token", $token, true)) {
108 return $token->getValue();
111 $this->logger
->notice("Token expired", $token);
112 throw new Exception\
TokenExpired($token->getLTL());
114 throw new Exception\TokenNotSet
;
117 function dropToken() {
118 $this->tokens
->del("access_token");
121 function getAuthUrl($callback_url) {
122 $state = base64_encode(openssl_random_pseudo_bytes(24));
123 $this->tokens
->set("state", new Storage\
Item($state, 5*60));
126 "client_id" => $this->config
->client
->id
,
127 "scope" => $this->config
->client
->scope
,
128 "redirect_uri" => $callback_url,
130 return new Url("https://github.com/login/oauth/authorize", [
131 "query" => new QueryString($param)
135 function fetchToken($code, $state, callable
$callback) {
136 if (!$this->tokens
->get("state", $orig_state, true)) {
137 if (isset($orig_state)) {
138 $this->logger
->notice("State expired", $orig_state);
139 throw new Exception\
StateExpired($orig_state->getLTL());
141 throw new Exception\StateNotSet
;
143 if ($state !== $orig_state->getValue()) {
144 $this->logger
->warning("State mismatch", compact("state", "orig_state"));
145 throw new Exception\
StateMismatch($orig_state->getValue(), $state);
148 $call = new API\Users\
ReadAuthToken($this, [
150 "client_id" => $this->config
->client
->id
,
151 "client_secret" => $this->config
->client
->secret
,
153 return $call($callback);
156 function readAuthUser(callable
$callback) {
157 $call = new API\Users\
ReadAuthUser($this);
158 return $call($callback);
161 function listRepos($page, callable
$callback) {
162 $call = new API\Repos\
ListRepos($this, compact("page"));
163 return $call($callback);
166 function readRepo($repo, callable
$callback) {
167 $call = new API\Repos\
ReadRepo($this, compact("repo"));
168 return $call($callback);
172 * Check if the pharext webhook is set for the repo and return it
173 * @param object $repo
174 * @return stdClass hook
176 function checkRepoHook($repo) {
178 foreach ($repo->hooks
as $hook) {
179 if ($hook->name
=== "web" && $hook->config
->url
=== $this->config
->hook
->url
) {
187 function listHooks($repo, callable
$callback) {
188 $call = new API\Hooks\
ListHooks($this, compact("repo"));
189 return $call($callback);
192 function listReleases($repo, $page, callable
$callback) {
193 $call = new API\Releases\
ListReleases($this, compact("repo", "page"));
194 return $call($callback);
197 function listTags($repo, $page, callable
$callback) {
198 $call = new API\Tags\
ListTags($this, compact("repo", "page"));
199 return $call($callback);
202 function readContents($repo, $path, callable
$callback) {
203 $call = new API\Repos\
ReadContents($this, compact("repo", "path"));
204 return $call($callback);
207 function createRepoHook($repo, $conf, callable
$callback) {
208 $call = new API\Hooks\
CreateHook($this, compact("repo", "conf"));
209 return $call($callback);
212 function updateRepoHook($repo, $id, $conf, callable
$callback) {
213 $call = new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf"));
214 return $call($callback);
217 function deleteRepoHook($repo, $id, callable
$callback) {
218 $call = new API\Hooks\
DeleteHook($this, compact("repo", "id"));
219 return $call($callback);
222 function createRelease($repo, $tag, callable
$callback) {
223 $call = new API\Releases\
CreateRelease($this, compact("repo", "tag"));
224 return $call($callback);
227 function createReleaseAsset($url, $asset, $type, callable
$callback) {
228 $call = new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type"));
229 return $call($callback);
232 function listReleaseAssets($repo, $id, callable
$callback) {
233 $call = new API\Releases\
ListReleaseAssets($this, compact("repo", "id"));
234 return $call($callback);