Passed
Push — main ( 3b8d3e...36fa87 )
by Chema
01:12 queued 13s
created

InvoiceConfig::getSendableRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice;
6
7
use Gacela\Framework\AbstractConfig;
8
use PhpLightning\Invoice\Domain\Transfer\SendableRange;
9
use RuntimeException;
10
11
final class InvoiceConfig extends AbstractConfig
12
{
13
    /** @var int 100 Minimum in msat (sat/1000) */
14
    private const DEFAULT_MIN_SENDABLE_IN_MILLISATS = 100_000;
0 ignored issues
show
Bug introduced by
The constant PhpLightning\Invoice\100_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
16
    /** @var int 10 000 000 Max in msat (sat/1000) */
17
    private const DEFAULT_MAX_SENDABLE_IN_MILLISATS = 10_000_000_000;
0 ignored issues
show
Bug introduced by
The constant PhpLightning\Invoice\10_000_000_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
19 2
    public function getCallback(): string
20
    {
21 2
        return sprintf('https://%s/%s', $this->getDomain(), $this->getReceiver());
22
    }
23
24 2
    public function getLnAddress(): string
25
    {
26 2
        return sprintf('%s@%s', $this->getReceiver(), $this->getDomain());
27
    }
28
29
    /**
30
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
31
     *     api_endpoint: string,
32
     *     api_key: string,
33
     * }
34
     */
35
    public function getBackendOptionsFor(string $backend): array
36
    {
37
        /** @var  array{api_endpoint?: string, api_key?: string} $result */
38
        $result = $this->get('backends')[$backend] ?? []; // @phpstan-ignore-line
39
40
        if (!isset($result['api_endpoint'], $result['api_key'])) {
41
            throw new RuntimeException('Missing backend options for ' . $backend);
42
        }
43
44
        return $result;
45
    }
46
47 2
    public function getSendableRange(): SendableRange
48
    {
49 2
        return SendableRange::withMinMax(
50 2
            (int)$this->get('min-sendable', self::DEFAULT_MIN_SENDABLE_IN_MILLISATS),
51 2
            (int)$this->get('max-sendable', self::DEFAULT_MAX_SENDABLE_IN_MILLISATS),
52 2
        );
53
    }
54
55 2
    private function getDomain(): string
56
    {
57 2
        return (string)$this->get('domain', $_SERVER['HTTP_HOST'] ?? 'localhost');
58
    }
59
60 2
    private function getReceiver(): string
61
    {
62 2
        return (string)$this->get('receiver', $_SERVER['REQUEST_URI'] ?? 'unknown-receiver');
63
    }
64
}
65