ContainerBuilder::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Maiorano\ContainerConfig;
4
5
use League\Container\Container;
6
use League\Container\Definition\DefinitionAggregate;
7
use League\Container\Inflector\InflectorAggregate;
8
use League\Container\ServiceProvider\ServiceProviderAggregate;
9
use Psr\Container\ContainerInterface;
10
11
/**
12
 * Class ContainerBuilder.
13
 */
14
final class ContainerBuilder implements BuilderInterface
15
{
16
    /**
17
     * @var BuilderInterface
18
     */
19
    private BuilderInterface $definitionBuilder;
20
    /**
21
     * @var BuilderInterface
22
     */
23
    private BuilderInterface $serviceProviderBuilder;
24
    /**
25
     * @var BuilderInterface
26
     */
27
    private BuilderInterface $inflectorBuilder;
28
29
    /**
30
     * ContainerBuilder constructor.
31
     *
32
     * @param BuilderInterface $definitionBuilder
33
     * @param BuilderInterface $serviceProviderBuilder
34
     * @param BuilderInterface $inflectorBuilder
35
     */
36 3
    public function __construct(
37
        BuilderInterface $definitionBuilder,
38
        BuilderInterface $serviceProviderBuilder,
39
        BuilderInterface $inflectorBuilder
40
    ) {
41 3
        $this->definitionBuilder = $definitionBuilder;
42 3
        $this->serviceProviderBuilder = $serviceProviderBuilder;
43 3
        $this->inflectorBuilder = $inflectorBuilder;
44 3
    }
45
46
    /**
47
     * @return ContainerBuilder
48
     */
49 1
    public static function make(): self
50
    {
51 1
        return new static(
52 1
            new DefinitionBuilder(new DefinitionAggregate()),
53 1
            new ServiceProviderBuilder(new ServiceProviderAggregate()),
54 1
            new InflectorBuilder(new InflectorAggregate())
55
        );
56
    }
57
58
    /**
59
     * @param array $config
60
     *
61
     * @return Container
62
     */
63 1
    public function build(array $config): Container
64
    {
65 1
        $container = new Container(
66 1
            $this->definitionBuilder->build($config['definitions'] ?? []),
67 1
            $this->serviceProviderBuilder->build($config['serviceProviders'] ?? []),
68 1
            $this->inflectorBuilder->build($config['inflectors'] ?? [])
69
        );
70
71 1
        return $this->loadDelegates($container, $config['delegates'] ?? []);
72
    }
73
74
    /**
75
     * @param Container $container
76
     * @param array     $delegates
77
     *
78
     * @return Container
79
     */
80 2
    public function loadDelegates(Container $container, array $delegates): Container
81
    {
82 2
        foreach ($delegates as $delegate) {
83
            /**
84
             * @var ContainerInterface $instance
85
             */
86 1
            $instance = $delegate instanceof ContainerInterface ? $delegate : new $delegate();
87 1
            $container->delegate($instance);
88
        }
89
90 2
        return $container;
91
    }
92
}
93