Passed
Push — main ( a1c31e...3240e3 )
by Chema
02:34
created

LightningConfig::setSendableRange()   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
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 ?BackendsConfig $backends = null;
17
18 4
    public function setDomain(string $domain): self
19
    {
20 4
        $parseUrl = parse_url($domain);
21 4
        $this->domain = $parseUrl['host'] ?? $domain;
22 4
        return $this;
23
    }
24
25 3
    public function setReceiver(string $receiver): self
26
    {
27 3
        $this->receiver = $receiver;
28 3
        return $this;
29
    }
30
31 3
    public function setSendableRange(int $min, int $max): self
32
    {
33 3
        $this->sendableRange = SendableRange::withMinMax($min, $max);
34 3
        return $this;
35
    }
36
37 3
    public function addBackend(BackendConfigInterface $backendConfig): self
38
    {
39 3
        $this->backends ??= new BackendsConfig();
40 3
        $this->backends->add($backendConfig);
41 3
        return $this;
42
    }
43
44 8
    public function jsonSerialize(): array
45
    {
46 8
        $result = [];
47 8
        if ($this->backends !== null) {
48 3
            $result['backends'] = $this->backends->jsonSerialize();
49
        }
50 8
        if ($this->domain !== null) {
51 4
            $result['domain'] = $this->domain;
52
        }
53 8
        if ($this->receiver !== null) {
54 3
            $result['receiver'] = $this->receiver;
55
        }
56 8
        if ($this->sendableRange !== null) {
57 3
            $result['sendable-range'] = $this->sendableRange;
58
        }
59
60 8
        return $result;
61
    }
62
}
63