github: fix notices
[pharext/pharext.org] / app / Controller / Github / RepoHook.php
1 <?php
2
3 namespace app\Controller\Github;
4
5 use app\Controller\Github;
6 use app\Github\API\Hooks\ListHooks;
7
8 class RepoHook extends Github
9 {
10 function __invoke(array $args = null) {
11 if (!$this->checkToken()) {
12 return;
13 }
14 if ($this->app->getRequest()->getRequestMethod() != "POST") {
15 // user had to re-authenticate, and was redirected here
16 $this->app->redirect($this->app->getBaseUrl()->mod([
17 "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
18 "query" => "modal=hook&hook=" . $args["action"]
19 ]));
20 } else {
21 $this->changeHook($args)->done(function() use($args) {
22 $this->redirectBack($args["owner"]."/".$args["name"]);
23 });
24 $this->github->drain();
25 }
26 }
27
28 function changeHook($args) {
29 switch ($args["action"]) {
30 case "upd":
31 return $this->updateHook($args["owner"] ."/". $args["name"]);
32 case "add":
33 return $this->addHook($args["owner"] ."/". $args["name"]);
34 case "del":
35 return $this->delHook($args["owner"] ."/". $args["name"]);
36 default:
37 throw new \Exception("Unknown action ".$args["action"]);
38 }
39 }
40
41 function addHook($repo_name) {
42 $hook_conf = $this->app->getRequest()->getForm();
43 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
44 return $this->github->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
45 $listhooks->dropFromCache();
46 });
47 }
48
49 function updateHook($repo_name) {
50 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
51 return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
52 list($hooks) = $result;
53
54 if (!($hook = $this->github->checkHook($hooks))) {
55 throw new \Exception("Hook is not set");
56 }
57
58 return $this->github->updateRepoHook($repo_name, $hook->id, $this->app->getRequest()->getForm());
59 })->then(function() use($listhooks) {
60 $listhooks->dropFromCache();
61 });
62 }
63
64 function delHook($repo_name) {
65 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
66 return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
67 list($hooks) = $result;
68
69 if (!($hook = $this->github->checkHook($hooks))) {
70 throw new \Exception("Hook is not set");
71 }
72
73 return $this->github->deleteRepoHook($repo_name, $hook->id);
74 })->then(function() use($listhooks) {
75 $listhooks->dropFromCache();
76 });
77 }
78
79 function redirectBack($repo) {
80 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
81 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
82 } else {
83 $this->app->redirect($this->app->getBaseUrl()->mod(":./github/repo/" . $repo));
84 }
85 }
86 }