promise refactoring++
[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 use http\Client\Response;
9
10 class CreateHook extends Call
11 {
12 function request() {
13 $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks");
14 $request = new Request("POST", $url, [
15 "Authorization" => "token " . $this->api->getToken(),
16 "Accept" => $this->config->api->accept,
17 "Content-Type" => "application/json",
18 ]);
19
20 $events = [];
21 if (!empty($this->args["conf"]["tag"])) {
22 $events[] = "create";
23 }
24 if (!empty($this->args["conf"]["release"])) {
25 $events[] = "release";
26 }
27
28 $request->getBody()->append(json_encode([
29 "name" => "web",
30 "events" => $events,
31 "config" => [
32 "zend" => (int)!empty($this->args["conf"]["zend"]),
33 "pecl" => (int)!empty($this->args["conf"]["pecl"]),
34 "url" => $this->config->hook->url,
35 "content_type" => $this->config->hook->content_type,
36 "insecure_ssl" => $this->config->hook->insecure_ssl,
37 "secret" => $this->config->client->secret, // FIXME: bad idea?
38 ]
39 ]));
40
41 return $request;
42 }
43
44 function response(Response $response) {
45 if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
46 throw new RequestException($response);
47 }
48 return [$json];
49 }
50 }