Passed
Push — main ( 6c0725...1b173c )
by Chema
16:45 queued 13:33
created

LightningConfig   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 105
rs 10
c 0
b 0
f 0
ccs 41
cts 42
cp 0.9762
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setDomain() 0 5 1
A setSuccessMessage() 0 4 1
A setSendableRange() 0 4 1
A setDescriptionTemplate() 0 4 1
A setReceiver() 0 4 1
B jsonSerialize() 0 26 8
A addBackendsFile() 0 30 4
A setCallbackUrl() 0 4 1
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;
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
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
    private ?string $descriptionTemplate = null;
20 4
    private ?string $successMessage = null;
21
22 4
    public function setDomain(string $domain): self
23 4
    {
24 4
        $parseUrl = parse_url($domain);
25
        $this->domain = $parseUrl['host'] ?? $domain;
26
        return $this;
27 3
    }
28
29 3
    public function setReceiver(string $receiver): self
30 3
    {
31
        $this->receiver = $receiver;
32
        return $this;
33 3
    }
34
35 3
    public function setSendableRange(int $min, int $max): self
36 3
    {
37
        $this->sendableRange = SendableRange::withMinMax($min, $max);
38
        return $this;
39 2
    }
40
41 2
    public function setCallbackUrl(string $callbackUrl): self
42 2
    {
43
        $this->callbackUrl = $callbackUrl;
44
        return $this;
45 2
    }
46
47 2
    public function setDescriptionTemplate(string $template): self
48
    {
49 2
        $this->descriptionTemplate = $template;
50
        return $this;
51
    }
52
53
    public function setSuccessMessage(string $message): self
54
    {
55
        $this->successMessage = $message;
56 2
        return $this;
57
    }
58 2
59 2
    public function addBackendsFile(string $path): self
60
    {
61
        $this->backends ??= new BackendsConfig();
62
63 2
        $jsonAsString = (string)file_get_contents($path);
64 2
        /** @var array<string, array{
65 2
         *     type: ?string,
66 2
         *     api_endpoint?: string,
67 2
         *     api_key?: string,
68 2
         * }> $json
69 2
         */
70 2
        $json = json_decode($jsonAsString, true);
71
72
        foreach ($json as $user => $settings) {
73
            if (!isset($settings['type'])) {
74 2
                throw new RuntimeException('"type" missing');
75
            }
76
77 7
            if ($settings['type'] === 'lnbits') { // TODO: refactor
78
                $this->backends->add(
79 7
                    $user,
80 7
                    LnBitsBackendConfig::withEndpointAndKey(
81 2
                        $settings['api_endpoint'] ?? '',
82
                        $settings['api_key'] ?? '',
83 7
                    ),
84 4
                );
85
            }
86 7
        }
87 3
88
        return $this;
89 7
    }
90 3
91
    public function jsonSerialize(): array
92 7
    {
93 2
        $result = [];
94
        if ($this->backends !== null) {
95
            $result['backends'] = $this->backends->jsonSerialize();
96 7
        }
97
        if ($this->domain !== null) {
98
            $result['domain'] = $this->domain;
99
        }
100
        if ($this->receiver !== null) {
101
            $result['receiver'] = $this->receiver;
102
        }
103
        if ($this->sendableRange !== null) {
104
            $result['sendable-range'] = $this->sendableRange;
105
        }
106
        if ($this->callbackUrl !== null) {
107
            $result['callback-url'] = $this->callbackUrl;
108
        }
109
        if ($this->descriptionTemplate !== null) {
110
            $result['description-template'] = $this->descriptionTemplate;
111
        }
112
        if ($this->successMessage !== null) {
113
            $result['success-message'] = $this->successMessage;
114
        }
115
116
        return $result;
117
    }
118
}
119