Passed
Pull Request — main (#18)
by Chema
02:34
created

InvoiceConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 43
c 2
b 0
f 0
ccs 0
cts 15
cp 0
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultLnAddress() 0 3 1
A getBackendOptionsFor() 0 10 2
A getSendableRange() 0 3 1
A getCallback() 0 3 1
A getDomain() 0 3 1
A getReceiver() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice;
6
7
use Gacela\Framework\AbstractConfig;
8
use PhpLightning\Shared\Value\SendableRange;
9
use RuntimeException;
10
11
final class InvoiceConfig extends AbstractConfig
12
{
13
    public function getCallback(): string
14
    {
15
        return (string)$this->get('callback-url', 'undefined:callback-url');
16
    }
17
18
    public function getDefaultLnAddress(): string
19
    {
20
        return sprintf('%s@%s', $this->getReceiver(), $this->getDomain());
21
    }
22
23
    /**
24
     * @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...
25
     *     api_endpoint: string,
26
     *     api_key: string,
27
     * }
28
     */
29
    public function getBackendOptionsFor(string $username): array
30
    {
31
        /** @var  array{api_endpoint?: string, api_key?: string} $result */
32
        $result = $this->get('backends')[$username] ?? []; // @phpstan-ignore-line
33
34
        if (!isset($result['api_endpoint'], $result['api_key'])) {
35
            throw new RuntimeException('Missing backend options for ' . $username);
36
        }
37
38
        return $result;
39
    }
40
41
    public function getSendableRange(): SendableRange
42
    {
43
        return $this->get('sendable-range', SendableRange::default());
44
    }
45
46
    public function getDomain(): string
47
    {
48
        return (string)$this->get('domain', $_SERVER['HTTP_HOST'] ?? 'localhost');
49
    }
50
51
    private function getReceiver(): string
52
    {
53
        return (string)$this->get('receiver', $_SERVER['REQUEST_URI'] ?? 'unknown-receiver');
54
    }
55
}
56