Passed
Push — main ( fb8201...aae9fe )
by Chema
15:02 queued 13:48
created

LightningConfig::setInvoiceMemo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

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