webhook create&delete
[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 switch ($args["action"]) {
11 case "add":
12 $this->addHook($args["owner"], $args["name"]);
13 break;
14
15 case "del":
16 $this->delHook($args["owner"], $args["name"]);
17 break;
18 }
19 }
20
21 function addHook($owner, $repo) {
22 $this->github->createRepoHook("$owner/$repo", function($hook) use($owner, $repo) {
23 if (($cache = $this->github->getCacheStorage())) {
24 $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
25 }
26 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
27 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
28 } else {
29 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
30 }
31 })->send();
32 }
33
34 function delHook($owner, $repo) {
35 $this->github->fetchRepo("$owner/$repo", function($repo) {
36 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
37 $repo->hooks = $hooks;
38 if (($id = $this->checkRepoHook($repo))) {
39 $this->github->deleteRepoHook($repo->full_name, $id, function() use($repo) {
40 if (($cache = $this->github->getCacheStorage())) {
41 $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
42 }
43 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
44 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
45 } else {
46 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
47 }
48 });
49 }
50 });
51 })->send();
52 }
53 }