promise refactoring++
[pharext/pharext.org] / app / Github / API / Hooks / UpdateHook.php
index cd249bad2b7bb544fbfaf147566af44bb28926cd..f03010770be115da3358b1d8e6ce0212b9ece5a7 100644 (file)
@@ -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",
@@ -19,14 +24,23 @@ class UpdateHook extends \app\Github\API\Call
                if (!empty($this->args["conf"]["release"])) {
                        $events[] = "release";
                }
-               
-               $request->getBody()->append(json_encode(compact("events")));
-               $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);
-                       }
-                       $callback($json);
-                       return true;
-               });
+               $config = [
+                       "zend" => (int)!empty($this->args["conf"]["zend"]),
+                       "pecl" => (int)!empty($this->args["conf"]["pecl"]),
+                       "url" => $this->config->hook->url,
+                       "content_type" => $this->config->hook->content_type,
+                       "insecure_ssl" => $this->config->hook->insecure_ssl,
+                       "secret" => $this->config->client->secret, // FIXME: bad idea?
+               ];
+
+               $request->getBody()->append(json_encode(compact("events", "config")));
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }