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 fetchRepos($page, callable
$callback) {
116 $fetch = new Fetch\
Repos($this);
117 $fetch->setPage($page);
118 return $fetch($callback);
121 function fetchRepo($repo, callable
$callback) {
122 $fetch = new Fetch\
Repo($this, compact("repo"));
123 return $fetch($callback);
126 function fetchHooks($repo, callable
$callback) {
127 $fetch = new Fetch\
Hooks($this, compact("repo"));
128 return $fetch($callback);
131 function fetchReleases($repo, $page, callable
$callback) {
132 $fetch = new Fetch\
Releases($this, compact("repo"));
133 $fetch->setPage($page);
134 return $fetch($callback);
137 function fetchTags($repo, $page, callable
$callback) {
138 $fetch = new Fetch\
Tags($this, compact("repo"));
139 $fetch->setPage($page);
140 return $fetch($callback);
143 function fetchContents($repo, $path, callable
$callback) {
144 $fetch = new Fetch\
Contents($this, compact("repo", "path"));
145 return $fetch($callback);