Passed
Push — master ( 4a1742...ff2740 )
by Alec
03:07 queued 34s
created

ServiceBuilder::withValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Container\Builder;
6
7
use AlecRabbit\Spinner\Container\Contract\IService;
8
use AlecRabbit\Spinner\Container\Contract\IServiceBuilder;
9
use AlecRabbit\Spinner\Container\Service;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Container\Service 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...
10
use InvalidArgumentException;
11
12
/**
13
 * @psalm-suppress PossiblyNullArgument
14
 */
15
final class ServiceBuilder implements IServiceBuilder
16
{
17
    private mixed $value;
18
    private ?bool $isStorable = null;
19
    private ?string $id = null;
20
21
    public function build(): IService
22
    {
23
        $this->validate();
24
25
        return new Service(
26
            value: $this->value,
27
            storable: $this->isStorable,
28
            id: $this->id,
29
        );
30
    }
31
32
    private function validate(): void
33
    {
34
        match (true) {
35
            !isset($this->value) => throw new InvalidArgumentException('Value is not set.'),
36
            $this->id === null => throw new InvalidArgumentException('Id is not set.'),
37
            $this->isStorable === null => throw new InvalidArgumentException('isStorable is not set.'),
38
            default => null,
39
        };
40
    }
41
42
    public function withValue(mixed $value): IServiceBuilder
43
    {
44
        $clone = clone $this;
45
        $clone->value = $value;
46
        return $clone;
47
    }
48
49
    public function withId(string $id): IServiceBuilder
50
    {
51
        $clone = clone $this;
52
        $clone->id = $id;
53
        return $clone;
54
    }
55
56
    public function withIsStorable(bool $isStorable): IServiceBuilder
57
    {
58
        $clone = clone $this;
59
        $clone->isStorable = $isStorable;
60
        return $clone;
61
    }
62
}
63