Passed
Push — main ( 0b1f45...3b8d3e )
by Chema
02:32 queued 12s
created

CallbackUrl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Domain\CallbackUrl;
6
7
use PhpLightning\Http\HttpFacadeInterface;
8
9
final class CallbackUrl implements CallbackUrlInterface
10
{
11
    /** @var int 100 Minimum in msat (sat/1000) */
12
    public const MIN_SENDABLE = 100_000;
0 ignored issues
show
Bug introduced by
The constant PhpLightning\Invoice\Domain\CallbackUrl\100_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
14
    /** @var int 10 000 000 Max in msat (sat/1000) */
15
    public const MAX_SENDABLE = 10_000_000_000;
0 ignored issues
show
Bug introduced by
The constant PhpLightning\Invoice\Dom...lbackUrl\10_000_000_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
17
    private const TAG_PAY_REQUEST = 'payRequest';
18
19 2
    public function __construct(
20
        private HttpFacadeInterface $httpFacade,
21
        private string $lnAddress,
22
        private string $callback,
23
    ) {
24 2
    }
25
26 2
    public function getCallbackUrl(string $imageFile = ''): array
27
    {
28
        // Modify the description if you want to custom it
29
        // This will be the description on the wallet that pays your ln address
30
        // TODO: Make this customizable from some external configuration file
31 2
        $description = 'Pay to ' . $this->lnAddress;
32
33 2
        $imageMetadata = $this->generateImageMetadata($imageFile);
34 2
        $metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']';
35
36
        // payRequest json data, spec : https://github.com/lnurl/luds/blob/luds/06.md
37 2
        return [
38 2
            'callback' => $this->callback,
39 2
            'maxSendable' => self::MAX_SENDABLE,
40 2
            'minSendable' => self::MIN_SENDABLE,
41 2
            'metadata' => $metadata,
42 2
            'tag' => self::TAG_PAY_REQUEST,
43 2
            'commentAllowed' => false, // TODO: Not implemented yet
44 2
        ];
45
    }
46
47 2
    private function generateImageMetadata(string $imageFile): string
48
    {
49 2
        if ($imageFile === '') {
50 2
            return '';
51
        }
52
        $response = $this->httpFacade->get($imageFile);
53
        if ($response === null) {
54
            return '';
55
        }
56
57
        return ',["image/jpeg;base64","' . base64_encode($response) . '"]';
58
    }
59
}
60