SettingsProviderBuilder::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Settings\Builder;
6
7
use AlecRabbit\Spinner\Core\Settings\Contract\Builder\ISettingsProviderBuilder;
8
use AlecRabbit\Spinner\Core\Settings\Contract\ISettings;
9
use AlecRabbit\Spinner\Core\Settings\Contract\ISettingsProvider;
10
use AlecRabbit\Spinner\Core\Settings\SettingsProvider;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Core\Settings\SettingsProvider 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...
11
use AlecRabbit\Spinner\Exception\LogicException;
12
13
/**
14
 * @psalm-suppress PossiblyNullArgument
15
 */
16
final class SettingsProviderBuilder implements ISettingsProviderBuilder
17
{
18
    private ?ISettings $settings = null;
19
    private ?ISettings $defaultSettings = null;
20
    private ?ISettings $detectedSettings = null;
21
22
    public function build(): ISettingsProvider
23
    {
24
        $this->validate();
25
26
        return new SettingsProvider(
27
            userSettings: $this->settings,
28
            defaultSettings: $this->defaultSettings,
29
            detectedSettings: $this->detectedSettings,
30
        );
31
    }
32
33
    private function validate(): void
34
    {
35
        match (true) {
36
            $this->settings === null => throw new LogicException('User settings are not set.'),
37
            $this->defaultSettings === null => throw new LogicException('Default settings are not set.'),
38
            $this->detectedSettings === null => throw new LogicException('Detected settings are not set.'),
39
            default => null,
40
        };
41
    }
42
43
    public function withSettings(ISettings $settings): ISettingsProviderBuilder
44
    {
45
        $clone = clone $this;
46
        $clone->settings = $settings;
47
        return $clone;
48
    }
49
50
    public function withDefaultSettings(ISettings $settings): ISettingsProviderBuilder
51
    {
52
        $clone = clone $this;
53
        $clone->defaultSettings = $settings;
54
        return $clone;
55
    }
56
57
    public function withDetectedSettings(ISettings $settings): ISettingsProviderBuilder
58
    {
59
        $clone = clone $this;
60
        $clone->detectedSettings = $settings;
61
        return $clone;
62
    }
63
}
64