promise refactoring++
[pharext/pharext.org] / app / Controller / Github / RepoHook.php
index 8ab1eed050a04575af2855b8e30e0bc078a9b5d7..14d3d69da48fa20e0b13b40f613f7e9938532009 100644 (file)
@@ -3,6 +3,7 @@
 namespace app\Controller\Github;
 
 use app\Controller\Github;
+use app\Github\API\Hooks\ListHooks;
 
 class RepoHook extends Github
 {
@@ -34,35 +35,59 @@ class RepoHook extends Github
        
        function addHook($owner, $repo) {
                $hook_conf = $this->app->getRequest()->getForm();
-               $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo) {
-                       if (($cache = $this->github->getCacheStorage())) {
-                               $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
-                       }
-                       if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
-                               $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
-                       } else {
-                               $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
-                       }
-               })->send();
+               $this->github->createRepoHook("$owner/$repo", $hook_conf)->then(function($hook) use($owner, $repo) {
+                       $call = new ListHooks($this->github, ["repo" => "$owner/$repo", "fresh" => true]);
+                       $this->github->queue($call)->then(function() use($owner, $repo) {
+                               $this->redirectBack("$owner/$repo");
+                       });
+               });
+               $this->github->drain();
        }
        
-       function delHook($owner, $repo) {
-               $this->github->fetchRepo("$owner/$repo", function($repo) {
-                       $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
-                               $repo->hooks = $hooks;
-                               if (($hook = $this->checkRepoHook($repo))) {
-                                       $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo) {
-                                               if (($cache = $this->github->getCacheStorage())) {
-                                                       $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
-                                               }
-                                               if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
-                                                       $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
-                                               } else {
-                                                       $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
+       function updateHook($owner, $repo) {
+               $this->github->readRepo("$owner/$repo")->then(function($result) {
+                       list($repo) = $result;
+                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
+                       $this->github->queue($call)->then(function($result) use($repo, $call) {
+                               list($repo->hooks) = $result;
+                               if (($hook = $this->github->checkRepoHook($repo))) {
+                                       $hook_conf = $this->app->getRequest()->getForm();
+                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf)->then(function($hook_result) use($repo, $hook, $result, $call) {
+                                               list($changed_hook) = $hook_result;
+                                               foreach ($changed_hook as $key => $val) {
+                                                       $hook->$key = $val;
                                                }
+                                               $call->saveToCache($result);
+                                               $this->redirectBack($repo->full_name);
                                        });
                                }
                        });
-               })->send();
+               });
+               $this->github->drain();
+       }
+       
+       function delHook($owner, $repo) {
+               $this->github->readRepo("$owner/$repo")->then(function($result) {
+                       list($repo) = $result;
+                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
+                       $this->github->queue($call)->then(function($result) use($repo, $call) {
+                               list($repo->hooks) = $result;
+                               if (($hook = $this->github->checkRepoHook($repo))) {
+                                       $this->github->deleteRepoHook($repo->full_name, $hook->id)->then(function() use($repo, $call) {
+                                               $call->dropFromCache();
+                                               $this->redirectBack($repo->full_name);
+                                       });
+                               }
+                       });
+               });
+               $this->github->drain();
+       }
+       
+       function redirectBack($repo) {
+               if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
+                       $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
+               } else {
+                       $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo));
+               }
        }
 }