2nd round
[pharext/pharext.org] / app / Controller / Github / RepoHook.php
index 39f6f1bb7acab299442e92f0ca50fc30c3c107bf..6bef009e8ca3ecf414e9586bbed1a817bde338d8 100644 (file)
@@ -16,64 +16,63 @@ class RepoHook extends Github
                                        "query" => "modal=hook&hook=" . $args["action"]
                                ]));
                        } else {
-                               switch ($args["action"]) {
-                               case "upd":
-                                       $this->updateHook($args["owner"], $args["name"]);
-                                       break;
-                               
-                               case "add":
-                                       $this->addHook($args["owner"], $args["name"]);
-                                       break;
-
-                               case "del":
-                                       $this->delHook($args["owner"], $args["name"]);
-                                       break;
-                               }
+                               $this->changeHook($args)->done(function() use($args) {
+                                       $this->redirectBack($args["owner"]."/".$args["repo"]);
+                               });
+                               $this->github->drain();
                        }
                }
        }
+
+       function changeHook($args) {
+               switch ($args["action"]) {
+               case "upd":
+                       return $this->updateHook($args["owner"] ."/". $args["name"]);
+               case "add":
+                       return $this->addHook($args["owner"] ."/". $args["name"]);
+               case "del":
+                       return $this->delHook($args["owner"] ."/". $args["name"]);
+               default:
+                       throw new \Exception("Unknown action ".$args["action"]);
+               }
+       }
        
-       function addHook($owner, $repo) {
+       function addHook($repo_name) {
                $hook_conf = $this->app->getRequest()->getForm();
-               $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo) {
-                       $call = new ListHooks($this->github, ["repo" => "$owner/$repo", "fresh" => true]);
-                       $call(function($hooks, $links) use($owner, $repo, $call) {
-                               $call->saveToCache([$hooks, $links]);
-                               $this->redirectBack("$owner/$repo");
-                       });
-               })->send();
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
        }
        
-       function updateHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo", function($repo) {
-                       $call = $this->github->listHooks($repo->full_name, function($hooks, $links) use($repo, &$call) {
-                               $repo->hooks = $hooks;
-                               if (($hook = $this->github->checkRepoHook($repo))) {
-                                       $hook_conf = $this->app->getRequest()->getForm();
-                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, &$call) {
-                                               foreach ($changed_hook as $key => $val) {
-                                                       $hook->$key = $val;
-                                               }
-                                               $call->saveToCache([$hooks, $links]);
-                                               $this->redirectBack($repo->full_name);
-                                       });
-                               }
-                       });
-               })->send();
+       function updateHook($repo_name) {
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
+                       list($hooks) = $result;
+
+                       if (!($hook = $this->github->checkHook($hooks))) {
+                               throw new \Exception("Hook is not set");
+                       }
+
+                       return $this->github->updateRepoHook($repo_name, $hook->id, $this->app->getRequest()->getForm());
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
        }
        
-       function delHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo", function($repo) {
-                       $call = $this->github->listHooks($repo->full_name, function($hooks) use($repo, &$call) {
-                               $repo->hooks = $hooks;
-                               if (($hook = $this->github->checkRepoHook($repo))) {
-                                       $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, &$call) {
-                                               $call->dropFromCache();
-                                               $this->redirectBack($repo->full_name);
-                                       });
-                               }
-                       });
-               })->send();
+       function delHook($repo_name) {
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
+                       list($hooks) = $result;
+
+                       if (!($hook = $this->github->checkHook($hooks))) {
+                               throw new \Exception("Hook is not set");
+                       }
+                       
+                       return $this->github->deleteRepoHook($repo_name, $hook->id);
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
        }
        
        function redirectBack($repo) {