Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Client
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 post
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Webhooks;
6
7/**
8 * Posts one webhook request with short timeouts.
9 *
10 * @internal
11 */
12class Client
13{
14    /**
15     * @param array<string, string> $headers
16     * @return ?int the response status, null without a response
17     */
18    public function post(string $url, array $headers, string $body): ?int
19    {
20        $handle = curl_init($url);
21        curl_setopt_array($handle, [
22            CURLOPT_POST => true,
23            CURLOPT_POSTFIELDS => $body,
24            CURLOPT_HTTPHEADER => array_map(
25                static fn(string $name, string $value): string => "{$name}: {$value}",
26                array_keys($headers),
27                $headers,
28            ),
29            CURLOPT_RETURNTRANSFER => true,
30            CURLOPT_CONNECTTIMEOUT => 2,
31            CURLOPT_TIMEOUT => 5,
32            CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
33        ]);
34        curl_exec($handle);
35        $status = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
36
37        return $status > 0 ? $status : null;
38    }
39}