DriverBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 78
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A withInitialInterval() 0 5 1
A withObserver() 0 5 1
A withIntervalComparator() 0 5 1
A validate() 0 9 1
A withDeltaTimer() 0 5 1
A build() 0 11 1
A withRenderer() 0 5 1
A withDriverMessages() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Builder;
6
7
use AlecRabbit\Spinner\Contract\IDeltaTimer;
8
use AlecRabbit\Spinner\Contract\IInterval;
9
use AlecRabbit\Spinner\Contract\IObserver;
10
use AlecRabbit\Spinner\Core\Contract\IDriver;
11
use AlecRabbit\Spinner\Core\Contract\IDriverBuilder;
12
use AlecRabbit\Spinner\Core\Contract\IDriverMessages;
13
use AlecRabbit\Spinner\Core\Contract\IIntervalComparator;
14
use AlecRabbit\Spinner\Core\Contract\IRenderer;
15
use AlecRabbit\Spinner\Core\Driver;
16
use AlecRabbit\Spinner\Exception\LogicException;
17
18
/**
19
 * @psalm-suppress PossiblyNullArgument
20
 */
21
final class DriverBuilder implements IDriverBuilder
22
{
23
    private ?IDeltaTimer $deltaTimer = null;
24
    private ?IInterval $initialInterval = null;
25
    private ?IObserver $observer = null;
26
    private ?IDriverMessages $driverMessages = null;
27
    private ?IIntervalComparator $intervalComparator = null;
28
    private ?IRenderer $renderer = null;
29
30
    public function withDeltaTimer(IDeltaTimer $timer): IDriverBuilder
31
    {
32
        $clone = clone $this;
33
        $clone->deltaTimer = $timer;
34
        return $clone;
35
    }
36
37
    public function withInitialInterval(IInterval $interval): IDriverBuilder
38
    {
39
        $clone = clone $this;
40
        $clone->initialInterval = $interval;
41
        return $clone;
42
    }
43
44
    public function withObserver(IObserver $observer): IDriverBuilder
45
    {
46
        $clone = clone $this;
47
        $clone->observer = $observer;
48
        return $clone;
49
    }
50
51
    public function build(): IDriver
52
    {
53
        $this->validate();
54
55
        return new Driver(
56
            initialInterval: $this->initialInterval,
0 ignored issues
show
Bug introduced by
It seems like $this->initialInterval can also be of type null; however, parameter $initialInterval of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Contract\IInterval, 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

56
            /** @scrutinizer ignore-type */ initialInterval: $this->initialInterval,
Loading history...
57
            driverMessages: $this->driverMessages,
0 ignored issues
show
Bug introduced by
It seems like $this->driverMessages can also be of type null; however, parameter $driverMessages of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Core\Contract\IDriverMessages, 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

57
            /** @scrutinizer ignore-type */ driverMessages: $this->driverMessages,
Loading history...
58
            renderer: $this->renderer,
0 ignored issues
show
Bug introduced by
It seems like $this->renderer can also be of type null; however, parameter $renderer of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Core\Contract\IRenderer, 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

58
            /** @scrutinizer ignore-type */ renderer: $this->renderer,
Loading history...
59
            intervalComparator: $this->intervalComparator,
0 ignored issues
show
Bug introduced by
It seems like $this->intervalComparator can also be of type null; however, parameter $intervalComparator of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Core\...act\IIntervalComparator, 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

59
            /** @scrutinizer ignore-type */ intervalComparator: $this->intervalComparator,
Loading history...
60
            deltaTimer: $this->deltaTimer,
0 ignored issues
show
Bug introduced by
It seems like $this->deltaTimer can also be of type null; however, parameter $deltaTimer of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Contract\IDeltaTimer, 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

60
            /** @scrutinizer ignore-type */ deltaTimer: $this->deltaTimer,
Loading history...
61
            observer: $this->observer,
62
        );
63
    }
64
65
    /**
66
     * @throws LogicException
67
     */
68
    private function validate(): void
69
    {
70
        match (true) {
71
            $this->renderer === null => throw new LogicException('Renderer is not set.'),
72
            $this->deltaTimer === null => throw new LogicException('Timer is not set.'),
73
            $this->initialInterval === null => throw new LogicException('InitialInterval is not set.'),
74
            $this->driverMessages === null => throw new LogicException('DriverMessages is not set.'),
75
            $this->intervalComparator === null => throw new LogicException('IntervalComparator is not set.'),
76
            default => null,
77
        };
78
    }
79
80
    public function withIntervalComparator(IIntervalComparator $intervalComparator): IDriverBuilder
81
    {
82
        $clone = clone $this;
83
        $clone->intervalComparator = $intervalComparator;
84
        return $clone;
85
    }
86
87
    public function withDriverMessages(IDriverMessages $driverMessages): IDriverBuilder
88
    {
89
        $clone = clone $this;
90
        $clone->driverMessages = $driverMessages;
91
        return $clone;
92
    }
93
94
    public function withRenderer(IRenderer $renderer): IDriverBuilder
95
    {
96
        $clone = clone $this;
97
        $clone->renderer = $renderer;
98
        return $clone;
99
    }
100
}
101