ServiceSpawnerBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Container\Builder;
6
7
use AlecRabbit\Spinner\Container\Contract\ICircularDependencyDetector;
8
use AlecRabbit\Spinner\Container\Contract\IContainer;
9
use AlecRabbit\Spinner\Container\Contract\IServiceFactory;
10
use AlecRabbit\Spinner\Container\Contract\IServiceSpawner;
11
use AlecRabbit\Spinner\Container\Contract\IServiceSpawnerBuilder;
12
use AlecRabbit\Spinner\Container\ServiceSpawner;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Container\ServiceSpawner 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 LogicException;
14
15
/**
16
 * @psalm-suppress PossiblyNullArgument
17
 */
18
final class ServiceSpawnerBuilder implements IServiceSpawnerBuilder
19
{
20
    private ?IContainer $container = null;
21
    private ?ICircularDependencyDetector $circularDependencyDetector = null;
22
    private ?IServiceFactory $serviceObjectFactory = null;
23
24
    public function withContainer(IContainer $container): IServiceSpawnerBuilder
25
    {
26
        $clone = clone $this;
27
        $clone->container = $container;
28
        return $clone;
29
    }
30
31
    public function build(): IServiceSpawner
32
    {
33
        $this->validate();
34
35
        return new ServiceSpawner(
36
            container: $this->container,
37
            circularDependencyDetector: $this->circularDependencyDetector,
38
            serviceObjectFactory: $this->serviceObjectFactory,
39
        );
40
    }
41
42
    private function validate(): void
43
    {
44
        match (true) {
45
            $this->container === null => throw new LogicException('Container is not set.'),
46
            $this->circularDependencyDetector === null => throw new LogicException(
47
                'Circular dependency detector is not set.'
48
            ),
49
            $this->serviceObjectFactory === null => throw new LogicException('Service object factory is not set.'),
50
            default => null,
51
        };
52
    }
53
54
    public function withCircularDependencyDetector(ICircularDependencyDetector $detector): IServiceSpawnerBuilder
55
    {
56
        $clone = clone $this;
57
        $clone->circularDependencyDetector = $detector;
58
        return $clone;
59
    }
60
61
    public function withServiceObjectFactory(IServiceFactory $serviceObjectFactory): IServiceSpawnerBuilder
62
    {
63
        $clone = clone $this;
64
        $clone->serviceObjectFactory = $serviceObjectFactory;
65
        return $clone;
66
    }
67
}
68