5 use app\Github\Storage
;
6 use app\Github\Exception
;
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;
43 function getConfig() {
47 function getClient() {
51 function getTokenStorage() {
55 function getCacheStorage() {
59 function getCacheKey($ident, $page = null) {
60 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
64 return $this->tokens
->get("access_token");
67 function setToken($token) {
68 $this->tokens
->set("access_token", $token,
69 $this->config
->storage
->token
->ttl
);
73 if ($this->tokens
->get("access_token", $token, $ttl, true)) {
77 throw new Exception\
TokenExpired($ttl);
79 throw new Exception\TokenNotSet
;
82 function dropToken() {
83 $this->tokens
->del("access_token");
86 function getAuthUrl($callback_url) {
87 $state = base64_encode(openssl_random_pseudo_bytes(24));
88 $this->tokens
->set("state", $state, 5*60);
91 "client_id" => $this->config
->client
->id
,
92 "scope" => $this->config
->client
->scope
,
93 "redirect_uri" => $callback_url,
95 return new Url("https://github.com/login/oauth/authorize", [
96 "query" => new QueryString($param)
100 function fetchToken($code, $state, callable
$callback) {
101 if (!$this->tokens
->get("state", $orig_state, $ttl, true)) {
103 throw new Exception\
StateExpired($ttl);
105 throw new Exception\StateNotSet
;
107 if ($state !== $orig_state) {
108 throw new Exception\
StateMismatch($orig_state, $state);
111 $fetch = new Fetch\
Token($this, compact("code") +
$this->config
->client
->toArray());
112 return $fetch($callback);
115 function fetchUser(callable
$callback) {
116 $fetch = new Fetch\
User($this);
117 return $fetch($callback);
120 function fetchRepos($page, callable
$callback) {
121 $fetch = new Fetch\
Repos($this);
122 $fetch->setPage($page);
123 return $fetch($callback);
126 function fetchRepo($repo, callable
$callback) {
127 $fetch = new Fetch\
Repo($this, compact("repo"));
128 return $fetch($callback);
131 function fetchHooks($repo, callable
$callback) {
132 $fetch = new Fetch\
Hooks($this, compact("repo"));
133 return $fetch($callback);
136 function fetchReleases($repo, $page, callable
$callback) {
137 $fetch = new Fetch\
Releases($this, compact("repo"));
138 $fetch->setPage($page);
139 return $fetch($callback);
142 function fetchTags($repo, $page, callable
$callback) {
143 $fetch = new Fetch\
Tags($this, compact("repo"));
144 $fetch->setPage($page);
145 return $fetch($callback);
148 function fetchContents($repo, $path, callable
$callback) {
149 $fetch = new Fetch\
Contents($this, compact("repo", "path"));
150 return $fetch($callback);
153 function createRepoHook($repo, $conf, callable
$callback) {
154 $create = new Create\
Webhook($this, compact("repo", "conf"));
155 return $create($callback);
158 function updateRepoHook($repo, $id, $conf, callable
$callback) {
159 $update = new Update\
Webhook($this, compact("repo", "id", "conf"));
160 return $update($callback);
163 function deleteRepoHook($repo, $id, callable
$callback) {
164 $delete = new Delete\
Webhook($this, compact("repo", "id"));
165 return $delete($callback);
168 function createRelease($repo, $tag, callable
$callback) {
169 $create = new Create\
Release($this, compact("repo", "tag"));
170 return $create($callback);
173 function createReleaseAsset($url, $asset, $type, callable
$callback) {
174 $create = new Create\
ReleaseAsset($this, compact("url", "asset", "type"));
175 return $create($callback);