Passed
Push — master ( 823aee...b9b37f )
by Alec
13:15 queued 12s
created

ADriverBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 58
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A withDriverSettings() 0 5 1
A build() 0 14 1
A withTimer() 0 5 1
A __construct() 0 3 1
A withOutput() 0 5 1
A withTerminalSettings() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
// 17.03.23
5
namespace AlecRabbit\Spinner\Core\A;
6
7
use AlecRabbit\Spinner\Contract\IDriver;
8
use AlecRabbit\Spinner\Contract\ITimer;
9
use AlecRabbit\Spinner\Core\Contract\IDriverBuilder;
10
use AlecRabbit\Spinner\Core\Defaults\Contract\IDefaults;
11
use AlecRabbit\Spinner\Core\Defaults\Contract\IDriverSettings;
12
use AlecRabbit\Spinner\Core\Defaults\Contract\ITerminalSettings;
13
use AlecRabbit\Spinner\Core\Driver;
14
use AlecRabbit\Spinner\Core\Output\Contract\IOutput;
15
use AlecRabbit\Spinner\Core\Output\StreamOutput;
16
use AlecRabbit\Spinner\Core\Timer;
17
18
abstract class ADriverBuilder implements IDriverBuilder
19
{
20
    protected ?IDriverSettings $driverSettings = null;
21
    protected ?IOutput $output = null;
22
    protected ?ITerminalSettings $terminalSettings = null;
23
    protected ?ITimer $timer = null;
24
25
    public function __construct(
26
        protected IDefaults $defaults,
27
    ) {
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function build(): IDriver
34
    {
35
        $this->output ??= new StreamOutput($this->defaults->getOutputStream());
36
        $this->timer ??= new Timer();
37
        $this->terminalSettings ??= $this->defaults->getTerminalSettings();
38
        $this->driverSettings ??= $this->defaults->getDriverSettings();
39
40
        return
41
            new Driver(
42
                output: $this->output,
0 ignored issues
show
Bug introduced by
It seems like $this->output can also be of type null; however, parameter $output of AlecRabbit\Spinner\Core\Driver::__construct() does only seem to accept AlecRabbit\Spinner\Core\Output\Contract\IOutput, 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

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

43
                /** @scrutinizer ignore-type */ timer: $this->timer,
Loading history...
44
                hideCursor: $this->terminalSettings->isCursorDisabled(),
45
                interruptMessage: $this->driverSettings->getInterruptMessage(),
46
                finalMessage: $this->driverSettings->getFinalMessage(),
47
            );
48
    }
49
50
    public function withOutput(IOutput $output): static
51
    {
52
        $clone = clone $this;
53
        $clone->output = $output;
54
        return $clone;
55
    }
56
57
    public function withTimer(ITimer $timer): static
58
    {
59
        $clone = clone $this;
60
        $clone->timer = $timer;
61
        return $clone;
62
    }
63
64
    public function withTerminalSettings(ITerminalSettings $terminalSettings): static
65
    {
66
        $clone = clone $this;
67
        $clone->terminalSettings = $terminalSettings;
68
        return $clone;
69
    }
70
71
    public function withDriverSettings(IDriverSettings $driverSettings): static
72
    {
73
        $clone = clone $this;
74
        $clone->driverSettings = $driverSettings;
75
        return $clone;
76
    }
77
78
79
}