ContainerBuilder::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php declare(strict_types = 1);
2
3
/**
4
 * This file is part of the Simplex package.
5
 *
6
 * (c) Freddie Frantzen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Simplex;
12
13
use DI\Container;
14
use DI\ContainerBuilder as PHPDIContainerBuilder;
15
use Simplex\DefinitionLoader\DefinitionLoader;
16
17
class ContainerBuilder
18
{
19
    private const CONTAINER_CLASS_SUFFIX = 'Container';
20
21
    private const CONTAINER_CLASS_FILE_EXTENSION = '.php';
22
23
    /** @var PHPDIContainerBuilder */
24
    private $phpDiContainerBuilder;
25
26
    /** @var DefinitionLoader */
27
    private $definitionLoader;
28
29
    /** @var string */
30
    private $environment;
31
32
    /** @var bool */
33
    private $compileContainer = false;
34
35
    /** @var \SplFileInfo */
36
    private $compiledContainerDirectory;
37
38
    /** @var string */
39
    private $compiledContainerClassName;
40
41 4
    public function __construct(
42
        PHPDIContainerBuilder $phpDiContainerBuilder,
43
        DefinitionLoader $definitionLoader,
44
        string $environment
45
    ) {
46 4
        $this->phpDiContainerBuilder = $phpDiContainerBuilder;
47 4
        $this->definitionLoader = $definitionLoader;
48 4
        $this->environment = $environment;
49 4
    }
50
51 3
    public function enableCompilation(\SplFileInfo $compiledContainerDirectory): void
52
    {
53 3
        $this->compileContainer = true;
54 3
        $this->compiledContainerDirectory = $compiledContainerDirectory;
55 3
        $this->compiledContainerClassName =  ucfirst($this->environment) . self::CONTAINER_CLASS_SUFFIX;
56
57 3
        $this->phpDiContainerBuilder->enableCompilation(
58 3
            $this->compiledContainerDirectory->getPathname(),
59 3
            $this->compiledContainerClassName
60
        );
61 3
    }
62
63 4
    public function build(): Container
64
    {
65 4
        if (!$this->compileContainer) {
66 1
            $this->definitionLoader->load($this->phpDiContainerBuilder);
67 1
            return $this->phpDiContainerBuilder->build();
68
        }
69
70 3
        return $this->buildWithCompilation();
71
    }
72
73 3
    private function buildWithCompilation(): Container
74
    {
75 3
        $compiledContainerClassFile = $this->compiledContainerDirectory->getPathname()
76 3
            . DIRECTORY_SEPARATOR
77 3
            . $this->compiledContainerClassName
78 3
            . self::CONTAINER_CLASS_FILE_EXTENSION;
79
80 3
        if (!file_exists($compiledContainerClassFile)) {
81 3
            $this->definitionLoader->load($this->phpDiContainerBuilder);
82
        }
83
84 3
        return $this->phpDiContainerBuilder->build();
85
    }
86
}
87