164f3205134070d8464ca6927382151f9f050594
[pharext/pharext.org] / app / Controller / Github.php
1 <?php
2
3 namespace app\Controller;
4
5 use app\Controller;
6 use app\Github\API;
7 use app\Session;
8 use app\Web;
9
10 use http\QueryString;
11 use http\Header;
12
13 abstract class Github implements Controller
14 {
15 /**
16 * @var \app\web
17 */
18 protected $app;
19
20 /**
21 * @var \app\Github\API
22 */
23 protected $github;
24
25 /**
26 * @var \app\Session
27 */
28 protected $session;
29
30 function __construct(Web $app, API $github, Session $session) {
31 $this->app = $app;
32 $this->github = $github;
33 $this->session = $session;
34 $this->app->getView()->addData(compact("session") + [
35 "location" => "github",
36 "title" => "Github"
37 ]);
38 $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
39
40 if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
41 $params = $header->getParams();
42 if (!empty($params["no-cache"])) {
43 $this->github->setMaxAge(0);
44 } elseif (!empty($params["max-age"])) {
45 $this->github->setMaxAge($params["max-age"]["value"]);
46 }
47 }
48 }
49
50 protected function checkToken() {
51 if ($this->github->hasToken()) {
52 return true;
53 }
54 $this->app->redirect($this->app->getBaseUrl()->mod([
55 "path" => "github/signin",
56 "query" => new QueryString(["returnto" => $this->session->current])
57 ]));
58 return false;
59 }
60 }