promise refactoring++
[pharext/pharext.org] / app / Github / API / Hooks / UpdateHook.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 UpdateHook extends Call
11 {
12 function request() {
13 $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args));
14 $request = new Request("PATCH", $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 $config = [
28 "zend" => (int)!empty($this->args["conf"]["zend"]),
29 "pecl" => (int)!empty($this->args["conf"]["pecl"]),
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 $request->getBody()->append(json_encode(compact("events", "config")));
37 return $request;
38 }
39
40 function response(Response $response) {
41 if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
42 throw new RequestException($response);
43 }
44 return [$json];
45 }
46 }