LnbitsBackendInvoice   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 36
ccs 22
cts 22
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A requestInvoice() 0 25 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Domain\BackendInvoice;
6
7
use PhpLightning\Invoice\Domain\Http\HttpApiInterface;
8
use PhpLightning\Shared\Transfer\InvoiceTransfer;
9
10
final class LnbitsBackendInvoice implements BackendInvoiceInterface
11
{
12
    /**
13
     * @param  array{api_key:string, api_endpoint:string}  $options
14
     */
15 4
    public function __construct(
16
        private readonly HttpApiInterface $httpApi,
17
        private array $options,
18
    ) {
19 4
    }
20
21 4
    public function requestInvoice(int $satsAmount, string $metadata = ''): InvoiceTransfer
22
    {
23 4
        $endpoint = $this->options['api_endpoint'] . '/api/v1/payments';
24
25 4
        $content = [
26 4
            'out' => false,
27 4
            'amount' => $satsAmount,
28 4
            'unhashed_description' => bin2hex($metadata),
29 4
            'description_hash' => hash('sha256', $metadata),
30 4
        ];
31
32 4
        $response = $this->httpApi->postRequestInvoice(
33 4
            $endpoint,
34 4
            body: json_encode($content, JSON_THROW_ON_ERROR),
35 4
            headers: [
36 4
                'Content-Type' => 'application/json',
37 4
                'X-Api-Key' => $this->options['api_key'],
38 4
            ],
39 4
        );
40
41 4
        if ($response === null) {
42 1
            return new InvoiceTransfer(status: 'ERROR', error: 'Backend "LnBits" unreachable');
43 1
        }
44 1
45
        return InvoiceTransfer::fromArray($response);
46
    }
47
}
48