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

HttpApi::postRequestInvoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 3
dl 0
loc 25
ccs 0
cts 18
cp 0
crap 2
rs 9.8333
c 1
b 0
f 0
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