1ef33a37cb921cae11284f1c8d95a5c2910a8d8b
[pharext/pharext.org] / app / Controller / Github / Repo / Hook.php
1 <?php
2
3 namespace app\Controller\Github\Repo;
4
5 use app\Controller\Github;
6
7 class Hook extends Github
8 {
9 function __invoke(array $args = null) {
10 if ($this->checkToken()) {
11 if ($this->app->getRequest()->getRequestMethod() != "POST") {
12 // user had to re-authenticate, and was redirected here
13 $this->app->redirect($this->app->getBaseUrl()->mod([
14 "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
15 "query" => "modal=hook&hook=" . $args["action"]
16 ]));
17 } else switch ($args["action"]) {
18 case "add":
19 $this->addHook($args["owner"], $args["name"]);
20 break;
21
22 case "del":
23 $this->delHook($args["owner"], $args["name"]);
24 break;
25 }
26 }
27 }
28
29 function addHook($owner, $repo) {
30 $this->github->createRepoHook("$owner/$repo", function($hook) use($owner, $repo) {
31 if (($cache = $this->github->getCacheStorage())) {
32 $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
33 }
34 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
35 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
36 } else {
37 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
38 }
39 })->send();
40 }
41
42 function delHook($owner, $repo) {
43 $this->github->fetchRepo("$owner/$repo", function($repo) {
44 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
45 $repo->hooks = $hooks;
46 if (($id = $this->checkRepoHook($repo))) {
47 $this->github->deleteRepoHook($repo->full_name, $id, function() use($repo) {
48 if (($cache = $this->github->getCacheStorage())) {
49 $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
50 }
51 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
52 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
53 } else {
54 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
55 }
56 });
57 }
58 });
59 })->send();
60 }
61 }