Passed
Push — main ( febc1c...c8b151 )
by Chema
02:27
created

LightningConfig::addBackend()   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 1
Bugs 0 Features 0
Metric Value
eloc 3
nc 1
nop 2
dl 0
loc 5
c 1
b 0
f 0
cc 1
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Config;
6
7
use JsonSerializable;
8
use PhpLightning\Config\Backend\LnBitsBackendConfig;
9
use PhpLightning\Shared\Value\SendableRange;
10
use RuntimeException;
11
12
final class LightningConfig implements JsonSerializable
13
{
14
    private ?BackendsConfig $backends = null;
15
    private ?string $domain = null;
16
    private ?string $receiver = null;
17
    private ?SendableRange $sendableRange = null;
18
    private ?string $callbackUrl = null;
19
20 4
    public function setDomain(string $domain): self
21
    {
22 4
        $parseUrl = parse_url($domain);
23 4
        $this->domain = $parseUrl['host'] ?? $domain;
24 4
        return $this;
25
    }
26
27 3
    public function setReceiver(string $receiver): self
28
    {
29 3
        $this->receiver = $receiver;
30 3
        return $this;
31
    }
32
33 3
    public function setSendableRange(int $min, int $max): self
34
    {
35 3
        $this->sendableRange = SendableRange::withMinMax($min, $max);
36 3
        return $this;
37
    }
38
39 2
    public function setCallbackUrl(string $callbackUrl): self
40
    {
41 2
        $this->callbackUrl = $callbackUrl;
42 2
        return $this;
43
    }
44
45 2
    public function addBackendsFile(string $path): self
46
    {
47 2
        $this->backends ??= new BackendsConfig();
48
49 2
        $jsonAsString = (string)file_get_contents($path);
50
        /** @var array<string, array{
51
         *     type: ?string,
52
         *     api_endpoint?: string,
53
         *     api_key?: string,
54
         * }> $json
55
         */
56 2
        $json = json_decode($jsonAsString, true);
57
58 2
        foreach ($json as $user => $settings) {
59 2
            if (!isset($settings['type'])) {
60
                throw new RuntimeException('"type" missing');
61
            }
62
63 2
            if ($settings['type'] === 'lnbits') { // TODO: refactor
64 2
                $this->backends->add(
65 2
                    $user,
66 2
                    LnBitsBackendConfig::withEndpointAndKey(
67 2
                        $settings['api_endpoint'] ?? '',
68 2
                        $settings['api_key'] ?? '',
69 2
                    ),
70 2
                );
71
            }
72
        }
73
74 2
        return $this;
75
    }
76
77 7
    public function jsonSerialize(): array
78
    {
79 7
        $result = [];
80 7
        if ($this->backends !== null) {
81 2
            $result['backends'] = $this->backends->jsonSerialize();
82
        }
83 7
        if ($this->domain !== null) {
84 4
            $result['domain'] = $this->domain;
85
        }
86 7
        if ($this->receiver !== null) {
87 3
            $result['receiver'] = $this->receiver;
88
        }
89 7
        if ($this->sendableRange !== null) {
90 3
            $result['sendable-range'] = $this->sendableRange;
91
        }
92 7
        if ($this->callbackUrl !== null) {
93 2
            $result['callback-url'] = $this->callbackUrl;
94
        }
95
96 7
        return $result;
97
    }
98
}
99