inital commit
[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\Url;
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) {
31 $this->app = $app;
32 $this->github = $github;
33 $this->app->getView()->addData(["location" => "github"]);
34 $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]);
35 }
36
37 protected function checkToken() {
38 if ($this->github->hasToken()) {
39 return true;
40 }
41 $this->app->redirect($this->app->getBaseUrl()->mod([
42 "path" => "github/signin",
43 "query" => new QueryString(["returnto" => $this->session->current])
44 ]));
45 return false;
46 }
47
48 function setSession(Session $session) {
49 $this->session = $session;
50 }
51
52 function checkRepoHook($repo) {
53 if ($repo->hooks) {
54 foreach ($repo->hooks as $hook) {
55 if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
56 return true;
57 }
58 }
59 }
60 return false;
61 }
62
63 function createLinkGenerator($links) {
64 return function($which) use($links) {
65 if (!isset($links[$which])) {
66 if ($which !== "next" || !isset($links["last"])) {
67 return null;
68 } else {
69 $which = "last";
70 }
71 }
72 $url = new Url($links[$which], null, 0);
73 $qry = new QueryString($url->query);
74 return $qry->getInt("page", 1);
75 };
76 }
77
78 }