6 use app\Github\Storage
;
7 use app\Github\Exception
;
37 function __construct(Config
$config, Storage
$tokens = null, Storage
$cache = null) {
38 $this->config
= $config;
39 $this->client
= new Client
;
40 $this->tokens
= $tokens ?
: new Storage\Session
;
41 $this->cache
= $cache;
44 function getConfig() {
48 function getClient() {
52 function getTokenStorage() {
56 function getCacheStorage() {
60 function getCacheKey($ident, $page = null) {
61 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
65 return $this->tokens
->get("access_token");
68 function setToken($token) {
69 $this->tokens
->set("access_token", $token,
70 $this->config
->storage
->token
->ttl
);
74 if ($this->tokens
->get("access_token", $token, $ttl, true)) {
78 throw new Exception\
TokenExpired($ttl);
80 throw new Exception\TokenNotSet
;
83 function dropToken() {
84 $this->tokens
->del("access_token");
87 function getAuthUrl($callback_url) {
88 $state = base64_encode(openssl_random_pseudo_bytes(24));
89 $this->tokens
->set("state", $state, 5*60);
92 "client_id" => $this->config
->client
->id
,
93 "scope" => $this->config
->client
->scope
,
94 "redirect_uri" => $callback_url,
96 return new Url("https://github.com/login/oauth/authorize", [
97 "query" => new QueryString($param)
101 function fetchToken($code, $state, callable
$callback) {
102 if (!$this->tokens
->get("state", $orig_state, $ttl, true)) {
104 throw new Exception\
StateExpired($ttl);
106 throw new Exception\StateNotSet
;
108 if ($state !== $orig_state) {
109 throw new Exception\
StateMismatch($orig_state, $state);
112 $call = new API\Users\
ReadAuthToken($this, [
114 "client_id" => $this->config
->client
->id
,
115 "client_secret" => $this->config
->client
->secret
,
117 return $call($callback);
120 function fetchUser(callable
$callback) {
121 $call = new API\Users\
ReadAuthUser($this);
122 return $call($callback);
125 function fetchRepos($page, callable
$callback) {
126 $call = new API\Repos\
ListRepos($this, compact("page"));
127 return $call($callback);
130 function fetchRepo($repo, callable
$callback) {
131 $call = new API\Repos\
ReadRepo($this, compact("repo"));
132 return $call($callback);
135 function fetchHooks($repo, callable
$callback) {
136 $call = new API\Hooks\
ListHooks($this, compact("repo"));
137 return $call($callback);
140 function fetchReleases($repo, $page, callable
$callback) {
141 $call = new API\Releases\
ListReleases($this, compact("repo", "page"));
142 return $call($callback);
145 function fetchTags($repo, $page, callable
$callback) {
146 $call = new API\Tags\
ListTags($this, compact("repo", "page"));
147 return $call($callback);
150 function fetchContents($repo, $path, callable
$callback) {
151 $call = new API\Repos\
ReadContents($this, compact("repo", "path"));
152 return $call($callback);
155 function createRepoHook($repo, $conf, callable
$callback) {
156 $call = new API\Hooks\
CreateHook($this, compact("repo", "conf"));
157 return $call($callback);
160 function updateRepoHook($repo, $id, $conf, callable
$callback) {
161 $call = new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf"));
162 return $call($callback);
165 function deleteRepoHook($repo, $id, callable
$callback) {
166 $call = new API\Hooks\
DeleteHook($this, compact("repo", "id"));
167 return $call($callback);
170 function createRelease($repo, $tag, callable
$callback) {
171 $call = new API\Releases\
CreateRelease($this, compact("repo", "tag"));
172 return $call($callback);
175 function createReleaseAsset($url, $asset, $type, callable
$callback) {
176 $call = new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type"));
177 return $call($callback);