refactor guthub api
[pharext/pharext.org] / app / Github / API / Hooks / CreateHook.php
1 <?php
2
3 namespace app\Github\API\Hooks;
4
5 use app\Github\API\Call;
6 use app\Github\Exception\RequestException;
7 use http\Client\Request;
8
9 class CreateHook extends Call
10 {
11 function enqueue(callable $callback) {
12 $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks");
13 $request = new Request("POST", $url, [
14 "Authorization" => "token " . $this->api->getToken(),
15 "Accept" => $this->config->api->accept,
16 "Content-Type" => "application/json",
17 ]);
18
19 if (!empty($this->args["conf"]["tag"])) {
20 $events[] = "create";
21 }
22 if (!empty($this->args["conf"]["release"])) {
23 $events[] = "release";
24 }
25
26 $request->getBody()->append(json_encode([
27 "name" => "web",
28 "events" => $events,
29 "config" => [
30 "url" => $this->config->hook->url,
31 "content_type" => $this->config->hook->content_type,
32 "insecure_ssl" => $this->config->hook->insecure_ssl,
33 "secret" => $this->config->client->secret, // FIXME: bad idea?
34 ]
35 ]));
36
37 $this->api->getClient()->enqueue($request, function($response) use($callback) {
38 if ($response->getReesponseCode() != 400 || null === ($json = json_decode($response->getBody()))) {
39 throw new RequestException($response);
40 }
41 $callback($json);
42 return true;
43 });
44 }
45 }