TestContainerBuilder::withParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace TomCizek\AsynchronousRouter\Tests\DependencyInjection;
6
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
9
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
10
use Symfony\Component\DependencyInjection\Loader\FileLoader;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
12
13
class TestContainerBuilder
14
{
15
    /** @var FileLoader */
16
    private $fileLoaderFactory;
17
18
    /** @var string */
19
    private $fileExtension;
20
21
    /** @var array[] */
22
    private $parameters;
23
24
    /** @var ExtensionInterface[] */
25
    private $extensions = [];
26
27
    /** @var CompilerPassInterface[] */
28
    private $compilerPasses = [];
29
30
    /** @var string[] */
31
    private $configFiles = [];
32
33
    public static function buildContainer(callable $fileLoaderFactory, string $fileExtension): self
34
    {
35
        return new self($fileLoaderFactory, $fileExtension);
36
    }
37
38
    private function __construct(callable $fileLoaderFactory, string $fileExtension)
39
    {
40
        $this->fileLoaderFactory = $fileLoaderFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fileLoaderFactory of type callable is incompatible with the declared type object<Symfony\Component...tion\Loader\FileLoader> of property $fileLoaderFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->fileExtension = $fileExtension;
42
        $this->parameters = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('kernel.debug' => ...__DIR__ . '/../../src') of type array<string,false|array...el.root_dir":"string"}> is incompatible with the declared type array<integer,array> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
            'kernel.debug' => false,
44
            'kernel.bundles' => [],
45
            'kernel.cache_dir' => sys_get_temp_dir(),
46
            'kernel.environment' => 'test',
47
            'kernel.root_dir' => __DIR__ . '/../../src',
48
        ];
49
    }
50
51
    public function withParameters(array $parameters): self
52
    {
53
        $this->parameters = array_merge($this->parameters, $parameters);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->parameters, $parameters) of type array is incompatible with the declared type array<integer,array> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
55
        return $this;
56
    }
57
58
    public function withExtensions(ExtensionInterface ...$extensions): self
59
    {
60
        $this->extensions = array_merge($this->extensions, $extensions);
61
62
        return $this;
63
    }
64
65
    public function withCompilerPasses(CompilerPassInterface ...$compilerPasses): self
66
    {
67
        $this->compilerPasses = array_merge($this->compilerPasses, $compilerPasses);
68
69
        return $this;
70
    }
71
72
    public function withConfigFiles(string ...$fileNames): self
73
    {
74
        $this->configFiles = array_merge($this->configFiles, $fileNames);
75
76
        return $this;
77
    }
78
79
    public function compile(): SymfonyContainerBuilder
80
    {
81
        $container = new SymfonyContainerBuilder(new ParameterBag($this->parameters));
82
        array_walk($this->extensions, [$container, 'registerExtension']);
83
84
        $fileLoader = call_user_func($this->fileLoaderFactory, $container);
85
        array_walk(
86
            $this->configFiles,
87
            function (string $fileName) use ($fileLoader) {
88
                $fileLoader->load("$fileName.{$this->fileExtension}");
89
            }
90
        );
91
92
        // array_walk is impossible here because the key will be passed as second parameter
93
        array_map([$container, 'addCompilerPass'], $this->compilerPasses);
94
95
        $container->getCompilerPassConfig()->setRemovingPasses([]);
96
        $container->compile();
97
98
        return $container;
99
    }
100
}
101