Passed
Push — master ( 3bfccc...670b84 )
by Koldo
02:23
created

ContainerConfig::marshalDelegators()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 47
ccs 0
cts 31
cp 0
rs 8.5066
c 0
b 0
f 0
cc 7
nc 17
nop 2
crap 56

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ContainerConfig::lazyLoadConditionals() 0 11 4
A ContainerConfig::lazyLoadFactories() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Container\Config;
6
7
use Antidot\Container\ContainerDelegatorFactory;
8
use ArrayObject;
9
use Aura\Di\Container;
10
use Aura\Di\ContainerConfigInterface;
11
12
use function dump;
13
use function is_array;
14
15
/**
16
 * Configuration for the Aura.Di container.
17
 * This class provides functionality for the following service types:
18
 * - Aliases
19
 * - Delegators
20
 * - Factories
21
 * - Invokable classes
22
 * - Services (known instances)
23
 * - conditionals
24
 */
25
final class ContainerConfig implements ContainerConfigInterface
26
{
27
    private $config;
28
29 10
    public function __construct(array $config)
30
    {
31 10
        $this->config = $config;
32 10
    }
33
34 9
    public function define(Container $container)
35
    {
36 9
        $container->set('config', new ArrayObject($this->config, ArrayObject::ARRAY_AS_PROPS));
37 9
        if (empty($this->config['dependencies'])) {
38 1
            return null;
39
        }
40 8
        $dependencies = $this->config['dependencies'];
41 8
        if (isset($dependencies['delegators'])) {
42
            $dependencies = (new MarshalDelegatorsConfig())($container, $dependencies);
43
        }
44 8
        $this->lazyLoadServices($container, $dependencies);
45 8
        $this->lazyLoadFactories($container, $dependencies);
46 8
        $this->lazyLoadConditionals($container, $dependencies);
47 8
        $this->lazyLoadInvokables($container, $dependencies);
48 8
        $this->lazyLoadAliases($container, $dependencies);
49 8
    }
50
51 1
    public function modify(Container $container)
52
    {
53 1
    }
54
55 3
    private function lazyLoad(Container $container, array $dependencies, string $type): void
56
    {
57 3
        foreach ($dependencies[$type] as $service => $class) {
58 3
            $container->set($service, $container->lazyNew($class));
59 3
            $container->types[$service] = $container->lazyGet($service);
60
        }
61 3
    }
62
63 8
    private function lazyLoadFactories(Container $container, array $dependencies): void
64
    {
65 8
        if (empty($dependencies['factories'])) {
66 6
            return;
67
        }
68 2
        foreach ($dependencies['factories'] as $service => $factory) {
69 2
            if (is_array($factory)) {
70 1
                $container->set($factory[0], $container->lazyNew($factory[0]));
71 1
                $container->set($service, $container->lazyGetCall(
72 1
                    $factory[0],
73 1
                    '__invoke',
74 1
                    $container,
75 1
                    $factory[1]
76
                ));
77
            } else {
78 1
                $container->set($factory, $container->lazyNew($factory));
79 1
                $container->set($service, $container->lazyGetCall($factory, '__invoke', $container));
80
            }
81 2
            $container->types[$service] = $container->lazyGet($service);
82
        }
83 2
    }
84
85 8
    private function lazyLoadConditionals(Container $container, array $dependencies): void
86
    {
87 8
        if (empty($dependencies['conditionals'])) {
88 5
            return;
89
        }
90
91 3
        foreach ($dependencies['conditionals'] as $id => $conditional) {
92 3
            $params = $this->setConditionalArguments($container, $id, $conditional);
93 3
            if (!$container->has($id)) {
94 1
                $container->set($id, $container->lazyNew($conditional['class'], $params));
95 3
                $container->types[$id] = $container->lazyGet($id);
96
            }
97
        }
98 3
    }
99
100 8
    private function lazyLoadInvokables(Container $container, array $dependencies): void
101
    {
102 8
        if (empty($dependencies['invokables'])) {
103 7
            return;
104
        }
105
106 1
        $this->lazyLoad($container, $dependencies, 'invokables');
107 1
    }
108
109 8
    private function lazyLoadAliases(Container $container, array $dependencies): void
110
    {
111 8
        if (empty($dependencies['aliases'])) {
112 7
            return;
113
        }
114
115 1
        $this->lazyLoad($container, $dependencies, 'aliases');
116 1
    }
117
118 8
    private function lazyLoadServices(Container $container, array $dependencies): void
119
    {
120 8
        if (empty($dependencies['services'])) {
121 7
            return;
122
        }
123
124 1
        $this->lazyLoad($container, $dependencies, 'services');
125 1
    }
126
127 3
    private function setConditionalArguments(Container $container, string $id, array $conditional): array
128
    {
129 3
        $params = [];
130 3
        foreach ($conditional['arguments'] as $type => $implementation) {
131 3
            if (is_array($implementation)) {
132 1
                $params[$type] = $implementation;
133 1
                $container->params[$id][$type] = $params[$type];
134 1
                $container->set($id, $container->lazyNew($conditional['class'], $params));
135 1
                $container->types[$id] = $container->lazyGet($id);
136 1
                continue;
137
            }
138
139 2
            if (!$container->has($implementation)) {
140 1
                $container->set($implementation, $container->lazyNew($implementation));
141 1
                $container->types[$implementation] = $container->lazyGet($implementation);
142
            }
143 2
            $params[$type] = $container->lazyGet($implementation);
144 2
            $container->params[$id][$type] = $params[$type];
145
        }
146
147 3
        return $params;
148
    }
149
}
150