From ee3977104c9ea0984c76f75f965c528bc4b3b64e Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 9 Jun 2015 17:57:04 +0200 Subject: [PATCH] promise refactoring++ --- app/Controller/Github/RepoHook.php | 37 ++++++----- app/Github/API/Hooks/CreateHook.php | 19 +++--- app/Github/API/Hooks/DeleteHook.php | 17 ++--- app/Github/API/Hooks/UpdateHook.php | 27 ++++---- app/Github/API/Releases/CreateRelease.php | 19 +++--- .../API/Releases/CreateReleaseAsset.php | 19 +++--- app/Github/API/Releases/PublishRelease.php | 19 +++--- app/views/github/repo.phtml | 65 ++++++++++--------- 8 files changed, 120 insertions(+), 102 deletions(-) diff --git a/app/Controller/Github/RepoHook.php b/app/Controller/Github/RepoHook.php index 39f6f1b..14d3d69 100644 --- a/app/Controller/Github/RepoHook.php +++ b/app/Controller/Github/RepoHook.php @@ -35,45 +35,52 @@ 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) { + $this->github->createRepoHook("$owner/$repo", $hook_conf)->then(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->github->queue($call)->then(function() use($owner, $repo) { $this->redirectBack("$owner/$repo"); }); - })->send(); + }); + $this->github->drain(); } 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; + $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, function($changed_hook) use($repo, $hook, $hooks, $links, &$call) { + $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([$hooks, $links]); + $call->saveToCache($result); $this->redirectBack($repo->full_name); }); } }); - })->send(); + }); + $this->github->drain(); } 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; + $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, function() use($repo, &$call) { + $this->github->deleteRepoHook($repo->full_name, $hook->id)->then(function() use($repo, $call) { $call->dropFromCache(); $this->redirectBack($repo->full_name); }); } }); - })->send(); + }); + $this->github->drain(); } function redirectBack($repo) { diff --git a/app/Github/API/Hooks/CreateHook.php b/app/Github/API/Hooks/CreateHook.php index 146929b..0d36ad6 100644 --- a/app/Github/API/Hooks/CreateHook.php +++ b/app/Github/API/Hooks/CreateHook.php @@ -5,10 +5,11 @@ namespace app\Github\API\Hooks; use app\Github\API\Call; use app\Github\Exception\RequestException; use http\Client\Request; +use http\Client\Response; class CreateHook extends Call { - function enqueue(callable $callback) { + function request() { $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks"); $request = new Request("POST", $url, [ "Authorization" => "token " . $this->api->getToken(), @@ -37,13 +38,13 @@ class CreateHook extends Call ] ])); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { - throw new RequestException($response); - } - $this->result = [$json]; - $callback($json); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new RequestException($response); + } + return [$json]; } } diff --git a/app/Github/API/Hooks/DeleteHook.php b/app/Github/API/Hooks/DeleteHook.php index a94f285..3635f6e 100644 --- a/app/Github/API/Hooks/DeleteHook.php +++ b/app/Github/API/Hooks/DeleteHook.php @@ -5,21 +5,22 @@ namespace app\Github\API\Hooks; use app\Github\API\Call; use app\Github\Exception\RequestException; use http\Client\Request; +use http\Client\Response; class DeleteHook extends Call { - function enqueue(callable $callback) { + function request() { $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args)); $request = new Request("DELETE", $url, [ "Authorization" => "token " . $this->api->getToken(), "Accept" => $this->config->api->accept, ]); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400) { - throw new RequestException($response); - } - $callback(); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400) { + throw new RequestException($response); + } } } diff --git a/app/Github/API/Hooks/UpdateHook.php b/app/Github/API/Hooks/UpdateHook.php index 2d994b5..f030107 100644 --- a/app/Github/API/Hooks/UpdateHook.php +++ b/app/Github/API/Hooks/UpdateHook.php @@ -2,11 +2,16 @@ namespace app\Github\API\Hooks; -class UpdateHook extends \app\Github\API\Call +use app\Github\API\Call; +use app\Github\Exception\RequestException; +use http\Client\Request; +use http\Client\Response; + +class UpdateHook extends Call { - function enqueue(callable $callback) { + function request() { $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args)); - $request = new \http\Client\Request("PATCH", $url, [ + $request = new Request("PATCH", $url, [ "Authorization" => "token ". $this->api->getToken(), "Accept" => $this->config->api->accept, "Content-Type" => "application/json", @@ -29,13 +34,13 @@ class UpdateHook extends \app\Github\API\Call ]; $request->getBody()->append(json_encode(compact("events", "config"))); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { - throw new \app\Github\Exception\RequestException($response); - } - $this->result = [$json]; - $callback($json); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new RequestException($response); + } + return [$json]; } } diff --git a/app/Github/API/Releases/CreateRelease.php b/app/Github/API/Releases/CreateRelease.php index 1cb2222..c7b1a4a 100644 --- a/app/Github/API/Releases/CreateRelease.php +++ b/app/Github/API/Releases/CreateRelease.php @@ -5,10 +5,11 @@ namespace app\Github\API\Releases; use app\Github\API\Call; use app\Github\Exception\RequestException; use http\Client\Request; +use http\Client\Response; class CreateRelease extends Call { - function enqueue(callable $callback) { + function request() { $url = $this->url->mod("/repos/". $this->args["repo"] ."/releases"); $request = new Request("POST", $url, [ "Authorization" => "token ". $this->api->getToken(), @@ -19,13 +20,13 @@ class CreateRelease extends Call "tag_name" => $this->args["tag"], "draft" => true, ])); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { - throw new RequestException($response); - } - $this->result = [$json]; - $callback($json); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new RequestException($response); + } + return [$json]; } } diff --git a/app/Github/API/Releases/CreateReleaseAsset.php b/app/Github/API/Releases/CreateReleaseAsset.php index 88b5a49..ce1ad36 100644 --- a/app/Github/API/Releases/CreateReleaseAsset.php +++ b/app/Github/API/Releases/CreateReleaseAsset.php @@ -5,24 +5,25 @@ namespace app\Github\API\Releases; use app\Github\API\Call; use app\Github\Exception\RequestException; use http\Client\Request; +use http\Client\Response; use http\Message\Body; class CreateReleaseAsset extends Call { - function enqueue(callable $callback) { + function request() { $body = new Body(fopen($this->args["asset"], "rb")); $request = new Request("POST", $this->args["url"], [ "Authorization" => "token ". $this->api->getToken(), "Accept" => $this->config->api->accept, "Content-Type" => $this->args["type"], ], $body); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { - throw new RequestException($response); - } - $this->result = [$json]; - $callback($json); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new RequestException($response); + } + return [$json]; } } diff --git a/app/Github/API/Releases/PublishRelease.php b/app/Github/API/Releases/PublishRelease.php index 39c5169..d480376 100644 --- a/app/Github/API/Releases/PublishRelease.php +++ b/app/Github/API/Releases/PublishRelease.php @@ -5,10 +5,11 @@ namespace app\Github\API\Releases; use app\Github\API\Call; use app\Github\Exception\RequestException; use http\Client\Request; +use http\Client\Response; class PublishRelease extends Call { - function enqueue(callable $callback) { + function request() { $url = $this->url->mod(uri_template("./repos/{+repo}/releases{/id}", $this->args)); $request = new Request("PATCH", $url, [ "Authorization" => "token ". $this->api->getToken(), @@ -19,13 +20,13 @@ class PublishRelease extends Call "draft" => false, "tag_name" => $this->args["tag"], ])); - $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { - throw new RequestException($response); - } - $this->result = [$json]; - $callback($json); - return true; - }); + return $request; + } + + function response(Response $response) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new RequestException($response); + } + return [$json]; } } \ No newline at end of file diff --git a/app/views/github/repo.phtml b/app/views/github/repo.phtml index 9d5f7eb..fff952d 100644 --- a/app/views/github/repo.phtml +++ b/app/views/github/repo.phtml @@ -204,42 +204,43 @@ release)) : ?>

md($v->release->body) ?>

-
- +
-- 2.30.2