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("curl", "github");
53 $this->client
->configure($config->http
->configure
->toArray());
54 $this->client
->attach(new ClientObserver($logger));
55 $this->tokens
= $tokens ?
: new Storage\Session
;
56 $this->cache
= $cache;
60 * Set maximum acceptable age of cache items
63 function setMaxAge($seconds) {
64 $this->maxAge
= $seconds;
68 function getMaxAge() {
72 function getLogger() {
76 function getConfig() {
80 function getClient() {
84 function getTokenStorage() {
88 function getCacheStorage() {
92 function getCacheKey($ident, $page = null) {
93 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
97 return $this->tokens
->get("access_token");
100 function setToken($token) {
101 $this->tokens
->set("access_token", new Storage\
Item(
103 $this->config
->storage
->token
->ttl
107 function getToken() {
108 if ($this->tokens
->get("access_token", $token, true)) {
109 return $token->getValue();
112 $this->logger
->notice("Token expired", $token);
113 throw new Exception\
TokenExpired($token->getLTL());
115 throw new Exception\TokenNotSet
;
118 function dropToken() {
119 $this->tokens
->del("access_token");
122 function getAuthUrl($callback_url) {
123 $state = base64_encode(openssl_random_pseudo_bytes(24));
124 $this->tokens
->set("state", new Storage\
Item($state, 5*60));
127 "client_id" => $this->config
->client
->id
,
128 "scope" => $this->config
->client
->scope
,
129 "redirect_uri" => $callback_url,
131 return new Url("https://github.com/login/oauth/authorize", [
132 "query" => new QueryString($param)
136 function fetchToken($code, $state, callable
$callback) {
137 if (!$this->tokens
->get("state", $orig_state, true)) {
138 if (isset($orig_state)) {
139 $this->logger
->notice("State expired", $orig_state);
140 throw new Exception\
StateExpired($orig_state->getLTL());
142 throw new Exception\StateNotSet
;
144 if ($state !== $orig_state->getValue()) {
145 $this->logger
->warning("State mismatch", compact("state", "orig_state"));
146 throw new Exception\
StateMismatch($orig_state->getValue(), $state);
149 $call = new API\Users\
ReadAuthToken($this, [
151 "client_id" => $this->config
->client
->id
,
152 "client_secret" => $this->config
->client
->secret
,
154 return $call($callback);
157 function readAuthUser(callable
$callback) {
158 $call = new API\Users\
ReadAuthUser($this);
159 return $call($callback);
162 function listRepos($page, callable
$callback) {
163 $call = new API\Repos\
ListRepos($this, compact("page"));
164 return $call($callback);
167 function readRepo($repo, callable
$callback) {
168 $call = new API\Repos\
ReadRepo($this, compact("repo"));
169 return $call($callback);
173 * Check if the pharext webhook is set for the repo and return it
174 * @param object $repo
175 * @return stdClass hook
177 function checkRepoHook($repo) {
179 foreach ($repo->hooks
as $hook) {
180 if ($hook->name
=== "web" && $hook->config
->url
=== $this->config
->hook
->url
) {
188 function listHooks($repo, callable
$callback) {
189 $call = new API\Hooks\
ListHooks($this, compact("repo"));
190 return $call($callback);
193 function listReleases($repo, $page, callable
$callback) {
194 $call = new API\Releases\
ListReleases($this, compact("repo", "page"));
195 return $call($callback);
198 function listTags($repo, $page, callable
$callback) {
199 $call = new API\Tags\
ListTags($this, compact("repo", "page"));
200 return $call($callback);
203 function readContents($repo, $path, callable
$callback) {
204 $call = new API\Repos\
ReadContents($this, compact("repo", "path"));
205 return $call($callback);
208 function createRepoHook($repo, $conf, callable
$callback) {
209 $call = new API\Hooks\
CreateHook($this, compact("repo", "conf"));
210 return $call($callback);
213 function updateRepoHook($repo, $id, $conf, callable
$callback) {
214 $call = new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf"));
215 return $call($callback);
218 function deleteRepoHook($repo, $id, callable
$callback) {
219 $call = new API\Hooks\
DeleteHook($this, compact("repo", "id"));
220 return $call($callback);
223 function createRelease($repo, $tag, callable
$callback) {
224 $call = new API\Releases\
CreateRelease($this, compact("repo", "tag"));
225 return $call($callback);
228 function publishRelease($repo, $id, $tag, callable
$callback) {
229 $call = new API\Releases\
PublishRelease($this, compact("repo", "id", "tag"));
230 return $call($callback);
233 function createReleaseAsset($url, $asset, $type, callable
$callback) {
234 $call = new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type"));
235 return $call($callback);
238 function listReleaseAssets($repo, $id, callable
$callback) {
239 $call = new API\Releases\
ListReleaseAssets($this, compact("repo", "id"));
240 return $call($callback);