Passed
Push — main ( 756af1...e120fa )
by Chema
03:55 queued 01:19
created

LightningConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 74.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 72
c 1
b 0
f 0
ccs 26
cts 35
cp 0.7429
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setDomain() 0 5 1
A setSendableRange() 0 4 1
A setReceiver() 0 4 1
A setBackends() 0 7 2
A jsonSerialize() 0 20 6
A addBackend() 0 5 1
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\BackendConfigInterface;
9
use PhpLightning\Shared\Value\SendableRange;
10
11
final class LightningConfig implements JsonSerializable
12
{
13
    private ?string $domain = null;
14
    private ?string $receiver = null;
15
    private ?SendableRange $sendableRange = null;
16
    private ?string $callbackUrl = null;
17
    private ?BackendsConfig $backends = null;
18
19 2
    public function setDomain(string $domain): self
20
    {
21 2
        $parseUrl = parse_url($domain);
22 2
        $this->domain = $parseUrl['host'] ?? $domain;
23 2
        return $this;
24
    }
25
26 1
    public function setReceiver(string $receiver): self
27
    {
28 1
        $this->receiver = $receiver;
29 1
        return $this;
30
    }
31
32 1
    public function setSendableRange(int $min, int $max): self
33
    {
34 1
        $this->sendableRange = SendableRange::withMinMax($min, $max);
35 1
        return $this;
36
    }
37
38
    public function setCallbackUrl(string $callbackUrl): self
39
    {
40
        $this->callbackUrl = $callbackUrl;
41
        return $this;
42
    }
43
44
    /**
45
     * @param array<string,BackendConfigInterface> $list
46
     */
47
    public function setBackends(array $list): self
48
    {
49
        $this->backends ??= new BackendsConfig();
50
        foreach ($list as $username => $config) {
51
            $this->backends->add($username, $config);
52
        }
53
        return $this;
54
    }
55
56 1
    public function addBackend(string $username, BackendConfigInterface $backendConfig): self
57
    {
58 1
        $this->backends ??= new BackendsConfig();
59 1
        $this->backends->add($username, $backendConfig);
60 1
        return $this;
61
    }
62
63 6
    public function jsonSerialize(): array
64
    {
65 6
        $result = [];
66 6
        if ($this->backends !== null) {
67 1
            $result['backends'] = $this->backends->jsonSerialize();
68
        }
69 6
        if ($this->domain !== null) {
70 2
            $result['domain'] = $this->domain;
71
        }
72 6
        if ($this->receiver !== null) {
73 1
            $result['receiver'] = $this->receiver;
74
        }
75 6
        if ($this->sendableRange !== null) {
76 1
            $result['sendable-range'] = $this->sendableRange;
77
        }
78 6
        if ($this->callbackUrl !== null) {
79
            $result['callback-url'] = $this->callbackUrl;
80
        }
81
82 6
        return $result;
83
    }
84
}
85