refactor guthub api
[pharext/pharext.org] / app / Github / API / Hooks / CreateHook.php
diff --git a/app/Github/API/Hooks/CreateHook.php b/app/Github/API/Hooks/CreateHook.php
new file mode 100644 (file)
index 0000000..325dca7
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+namespace app\Github\API\Hooks;
+
+use app\Github\API\Call;
+use app\Github\Exception\RequestException;
+use http\Client\Request;
+
+class CreateHook extends Call
+{
+       function enqueue(callable $callback) {
+               $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks");
+               $request = new Request("POST", $url, [
+                       "Authorization" => "token " . $this->api->getToken(),
+                       "Accept" => $this->config->api->accept,
+                       "Content-Type" => "application/json",
+               ]);
+               
+               if (!empty($this->args["conf"]["tag"])) {
+                       $events[] = "create";
+               }
+               if (!empty($this->args["conf"]["release"])) {
+                       $events[] = "release";
+               }
+               
+               $request->getBody()->append(json_encode([
+                       "name" => "web",
+                       "events" => $events,
+                       "config" => [
+                               "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?
+                       ]
+               ]));
+               
+               $this->api->getClient()->enqueue($request, function($response) use($callback) {
+                       if ($response->getReesponseCode() != 400 || null === ($json = json_decode($response->getBody()))) {
+                               throw new RequestException($response);
+                       }
+                       $callback($json);
+                       return true;
+               });
+       }
+}