Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

WidgetSettingsBuilder::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlecRabbit\Spinner\Core\Builder;
7
8
use AlecRabbit\Spinner\Contract\IFrame;
9
use AlecRabbit\Spinner\Contract\Pattern\IPattern;
10
use AlecRabbit\Spinner\Core\Builder\Settings\Contract\IWidgetSettingsBuilder;
11
use AlecRabbit\Spinner\Core\Pattern\Contract\IStylePattern;
12
use AlecRabbit\Spinner\Core\Settings\Contract\IWidgetSettings;
13
use AlecRabbit\Spinner\Core\Settings\WidgetSettings;
14
use AlecRabbit\Spinner\Exception\LogicException;
15
16
final class WidgetSettingsBuilder implements IWidgetSettingsBuilder
17
{
18
    private ?IFrame $leadingSpacer = null;
19
    private ?IFrame $trailingSpacer = null;
20
    private ?IStylePattern $stylePattern = null;
21
    private ?IPattern $charPattern = null;
22
23
    public function build(): IWidgetSettings
24
    {
25
        $this->validate();
26
27
        return
28
            new WidgetSettings(
29
                leadingSpacer: $this->leadingSpacer,
0 ignored issues
show
Bug introduced by
It seems like $this->leadingSpacer can also be of type null; however, parameter $leadingSpacer of AlecRabbit\Spinner\Core\...Settings::__construct() does only seem to accept AlecRabbit\Spinner\Contract\IFrame, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
                /** @scrutinizer ignore-type */ leadingSpacer: $this->leadingSpacer,
Loading history...
30
                trailingSpacer: $this->trailingSpacer,
0 ignored issues
show
Bug introduced by
It seems like $this->trailingSpacer can also be of type null; however, parameter $trailingSpacer of AlecRabbit\Spinner\Core\...Settings::__construct() does only seem to accept AlecRabbit\Spinner\Contract\IFrame, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
                /** @scrutinizer ignore-type */ trailingSpacer: $this->trailingSpacer,
Loading history...
31
                stylePattern: $this->stylePattern,
0 ignored issues
show
Bug introduced by
It seems like $this->stylePattern can also be of type null; however, parameter $stylePattern of AlecRabbit\Spinner\Core\...Settings::__construct() does only seem to accept AlecRabbit\Spinner\Core\...\Contract\IStylePattern, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
                /** @scrutinizer ignore-type */ stylePattern: $this->stylePattern,
Loading history...
32
                charPattern: $this->charPattern,
0 ignored issues
show
Bug introduced by
It seems like $this->charPattern can also be of type null; however, parameter $charPattern of AlecRabbit\Spinner\Core\...Settings::__construct() does only seem to accept AlecRabbit\Spinner\Contract\Pattern\IPattern, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                /** @scrutinizer ignore-type */ charPattern: $this->charPattern,
Loading history...
33
            );
34
    }
35
36
    /**
37
     * @throws LogicException
38
     */
39
    private function validate(): void
40
    {
41
        match (true) {
42
            $this->leadingSpacer === null => throw new LogicException('Leading spacer is not set.'),
43
            $this->trailingSpacer === null => throw new LogicException('Trailing spacer is not set.'),
44
            $this->stylePattern === null => throw new LogicException('Style pattern is not set.'),
45
            $this->charPattern === null => throw new LogicException('Char pattern is not set.'),
46
            default => null,
47
        };
48
    }
49
50
    public function withLeadingSpacer(IFrame $frame): IWidgetSettingsBuilder
51
    {
52
        $clone = clone $this;
53
        $clone->leadingSpacer = $frame;
54
        return $clone;
55
    }
56
57
    public function withTrailingSpacer(IFrame $frame): IWidgetSettingsBuilder
58
    {
59
        $clone = clone $this;
60
        $clone->trailingSpacer = $frame;
61
        return $clone;
62
    }
63
64
    public function withStylePattern(IStylePattern $pattern): IWidgetSettingsBuilder
65
    {
66
        $clone = clone $this;
67
        $clone->stylePattern = $pattern;
68
        return $clone;
69
    }
70
71
    public function withCharPattern(IPattern $pattern): IWidgetSettingsBuilder
72
    {
73
        $clone = clone $this;
74
        $clone->charPattern = $pattern;
75
        return $clone;
76
    }
77
}
78