Test Failed
Push — main ( 117601...63d9b1 )
by Chema
11:24
created

InvoiceGenerator::generateInvoice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 20
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Application;
6
7
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface;
8
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
9
use PhpLightning\Shared\Value\SendableRange;
0 ignored issues
show
Bug introduced by
The type PhpLightning\Shared\Value\SendableRange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
final class InvoiceGenerator
12
{
13
    public const MESSAGE_PAYMENT_RECEIVED = 'Payment received!';
14
15
    public function __construct(
16
        private BackendInvoiceInterface $backendInvoice,
17
        private SendableRange $sendableRange,
18
        private string $lnAddress,
19
    ) {
20
    }
21
22
    public function generateInvoice(int $milliSats): array
23
    {
24
        if (!$this->sendableRange->contains($milliSats)) {
25
            return [
26
                'status' => 'ERROR',
27
                'reason' => 'Amount is not between minimum and maximum sendable amount',
28
            ];
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
        $description = 'Pay to ' . $this->lnAddress;
34
35
        // TODO: images not implemented yet
36
        $imageMetadata = '';
37
        $metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']';
38
39
        $invoice = $this->backendInvoice->requestInvoice((int)($milliSats / 1000), $metadata);
40
41
        return $this->mapResponseAsArray($invoice);
42
    }
43
44
    private function mapResponseAsArray(BackendInvoiceResponse $invoice): array
45
    {
46
        return [
47
            'pr' => $invoice->getPaymentRequest(),
48
            'status' => $invoice->getStatus(),
49
            'successAction' => [
50
                'tag' => 'message',
51
                'message' => self::MESSAGE_PAYMENT_RECEIVED,
52
            ],
53
            'routes' => [],
54
            'disposable' => false,
55
            'reason' => $invoice->getReason(),
56
        ];
57
    }
58
}
59