flush
[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 "upd":
19 $this->updateHook($args["owner"], $args["name"]);
20 break;
21
22 case "add":
23 $this->addHook($args["owner"], $args["name"]);
24 break;
25
26 case "del":
27 $this->delHook($args["owner"], $args["name"]);
28 break;
29 }
30 }
31 }
32
33 function addHook($owner, $repo) {
34 $hook_conf = $this->app->getRequest()->getForm();
35 $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo) {
36 if (($cache = $this->github->getCacheStorage())) {
37 $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
38 }
39 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
40 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
41 } else {
42 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
43 }
44 })->send();
45 }
46
47 function delHook($owner, $repo) {
48 $this->github->fetchRepo("$owner/$repo", function($repo) {
49 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
50 $repo->hooks = $hooks;
51 if (($id = $this->checkRepoHook($repo))) {
52 $this->github->deleteRepoHook($repo->full_name, $id, function() use($repo) {
53 if (($cache = $this->github->getCacheStorage())) {
54 $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
55 }
56 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
57 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
58 } else {
59 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
60 }
61 });
62 }
63 });
64 })->send();
65 }
66 }