Passed
Push — main ( cf3344...756af1 )
by Chema
56s queued 13s
created

InvoiceGenerator::okResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Domain\LnAddress;
6
7
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface;
8
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
9
use PhpLightning\Shared\Value\SendableRange;
10
11
final class InvoiceGenerator
12
{
13
    public const MESSAGE_PAYMENT_RECEIVED = 'Payment received!';
14
15 4
    public function __construct(
16
        private BackendInvoiceInterface $backendInvoice,
17
        private SendableRange $sendableRange,
18
        private string $lnAddress,
19
    ) {
20 4
    }
21
22 4
    public function generateInvoice(int $milliSats): array
23
    {
24 4
        if (!$this->sendableRange->contains($milliSats)) {
25 1
            return [
26 1
                'status' => 'ERROR',
27 1
                'reason' => 'Amount is not between minimum and maximum sendable amount',
28 1
            ];
29
        }
30
        // Modify the description if you want to custom it
31
        // This will be the description on the wallet that pays your ln address
32
        // TODO: Make this customizable from some external configuration file
33 3
        $description = 'Pay to ' . $this->lnAddress;
34
35
        // TODO: images not implemented yet
36 3
        $imageMetadata = '';
37 3
        $metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']';
38
39 3
        $invoice = $this->backendInvoice->requestInvoice((int)($milliSats / 1000), $metadata);
40
41 3
        return $this->mapResponseAsArray($invoice);
42
    }
43
44 3
    private function mapResponseAsArray(BackendInvoiceResponse $invoice): array
45
    {
46 3
        return [
47 3
            'pr' => $invoice->getPaymentRequest(),
48 3
            'status' => $invoice->getStatus(),
49 3
            'successAction' => [
50 3
                'tag' => 'message',
51 3
                'message' => self::MESSAGE_PAYMENT_RECEIVED,
52 3
            ],
53 3
            'routes' => [],
54 3
            'disposable' => false,
55 3
            'reason' => $invoice->getReason(),
56 3
        ];
57
    }
58
}
59