Passed
Pull Request — main (#10)
by Chema
02:14
created

HttpApi   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 27
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A postRequestInvoice() 0 25 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Infrastructure\Http;
6
7
use PhpLightning\Invoice\Domain\Http\HttpApiInterface;
8
9
final class HttpApi implements HttpApiInterface
10
{
11
    public function postRequestInvoice(string $uri, string $body, array $headers = []): ?array
12
    {
13
        // TODO: Consider using something like this:
14
        //        HttpClient::create()
15
        //            ->request('POST', $url, $options)
16
        //            ->getContent();
17
        $options = [
18
            explode(':', $uri)[0] => [ // extract protocol from $uri
19
                'header' => implode(
20
                    "\r\n",
21
                    array_map(
22
                        static fn (string $v, string $k) => sprintf('%s: %s', $k, $v),
23
                        $headers,
24
                        array_keys($headers),
25
                    ),
26
                ),
27
                'method' => 'POST',
28
                'content' => $body,
29
            ],
30
        ];
31
32
        $context = stream_context_create($options);
33
        $response = (string)file_get_contents($uri, false, $context);
34
35
        return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
36
    }
37
}
38