Passed
Push — main ( 328362...5892c7 )
by Chema
02:33
created

LightningConfig::setCallbackUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
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\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 4
    public function setDomain(string $domain): self
20
    {
21 4
        $parseUrl = parse_url($domain);
22 4
        $this->domain = $parseUrl['host'] ?? $domain;
23 4
        return $this;
24
    }
25
26 3
    public function setReceiver(string $receiver): self
27
    {
28 3
        $this->receiver = $receiver;
29 3
        return $this;
30
    }
31
32 3
    public function setSendableRange(int $min, int $max): self
33
    {
34 3
        $this->sendableRange = SendableRange::withMinMax($min, $max);
35 3
        return $this;
36
    }
37
38 2
    public function setCallbackUrl(string $callbackUrl): self
39
    {
40 2
        $this->callbackUrl = $callbackUrl;
41 2
        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 3
    public function addBackend(string $username, BackendConfigInterface $backendConfig): self
57
    {
58 3
        $this->backends ??= new BackendsConfig();
59 3
        $this->backends->add($username, $backendConfig);
60 3
        return $this;
61
    }
62
63 8
    public function jsonSerialize(): array
64
    {
65 8
        $result = [];
66 8
        if ($this->backends !== null) {
67 3
            $result['backends'] = $this->backends->jsonSerialize();
68
        }
69 8
        if ($this->domain !== null) {
70 4
            $result['domain'] = $this->domain;
71
        }
72 8
        if ($this->receiver !== null) {
73 3
            $result['receiver'] = $this->receiver;
74
        }
75 8
        if ($this->sendableRange !== null) {
76 3
            $result['sendable-range'] = $this->sendableRange;
77
        }
78 8
        if ($this->callbackUrl !== null) {
79 2
            $result['callback-url'] = $this->callbackUrl;
80
        }
81
82 8
        return $result;
83
    }
84
}
85