OutputConfigBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
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 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Config\Builder;
6
7
use AlecRabbit\Spinner\Contract\Mode\CursorVisibilityMode;
8
use AlecRabbit\Spinner\Contract\Mode\InitializationMode;
9
use AlecRabbit\Spinner\Contract\Mode\StylingMethodMode;
10
use AlecRabbit\Spinner\Core\Config\Contract\Builder\IOutputConfigBuilder;
11
use AlecRabbit\Spinner\Core\Config\Contract\IOutputConfig;
12
use AlecRabbit\Spinner\Core\Config\OutputConfig;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Core\Config\OutputConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use AlecRabbit\Spinner\Exception\LogicException;
14
15
/**
16
 * @psalm-suppress PossiblyNullArgument
17
 */
18
final class OutputConfigBuilder implements IOutputConfigBuilder
19
{
20
    private ?StylingMethodMode $stylingMethodMode = null;
21
    private ?CursorVisibilityMode $cursorVisibilityMode = null;
22
    private ?InitializationMode $initializationMode = null;
23
    private mixed $stream = null;
24
25
    public function build(): IOutputConfig
26
    {
27
        $this->validate();
28
29
        return new OutputConfig(
30
            stylingMethodMode: $this->stylingMethodMode,
31
            cursorVisibilityMode: $this->cursorVisibilityMode,
32
            initializationMode: $this->initializationMode,
33
            stream: $this->stream,
34
        );
35
    }
36
37
    /**
38
     * @throws LogicException
39
     */
40
    private function validate(): void
41
    {
42
        match (true) {
43
            $this->stylingMethodMode === null => throw new LogicException('StylingMethodMode is not set.'),
44
            $this->cursorVisibilityMode === null => throw new LogicException('CursorVisibilityMode is not set.'),
45
            $this->initializationMode === null => throw new LogicException('InitializationMode is not set.'),
46
            $this->stream === null => throw new LogicException('Stream is not set.'),
47
            default => null,
48
        };
49
    }
50
51
    public function withStylingMethodMode(StylingMethodMode $stylingMethodMode): IOutputConfigBuilder
52
    {
53
        $clone = clone $this;
54
        $clone->stylingMethodMode = $stylingMethodMode;
55
        return $clone;
56
    }
57
58
    public function withCursorVisibilityMode(CursorVisibilityMode $cursorVisibilityMode): IOutputConfigBuilder
59
    {
60
        $clone = clone $this;
61
        $clone->cursorVisibilityMode = $cursorVisibilityMode;
62
        return $clone;
63
    }
64
65
    public function withInitializationMode(InitializationMode $initializationMode): IOutputConfigBuilder
66
    {
67
        $clone = clone $this;
68
        $clone->initializationMode = $initializationMode;
69
        return $clone;
70
    }
71
72
    public function withStream(mixed $stream): IOutputConfigBuilder
73
    {
74
        $clone = clone $this;
75
        $clone->stream = $stream;
76
        return $clone;
77
    }
78
}
79