Container::withConfig()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 24
nop 1
dl 0
loc 32
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Container;
6
7
use Gacela\Container\Container as GacelaContainer;
8
use Gacela\Framework\Config\Config;
9
10
/**
11
 * This is a decorator class to simplify the usage of the decoupled Container
12
 */
13
final class Container extends GacelaContainer implements ContainerInterface
14
{
15
    public static function withConfig(Config $config): self
16
    {
17
        $container = new self(
18
            $config->getFactory()->createGacelaFileConfig()->getBindings(),
19
            $config->getSetupGacela()->getServicesToExtend(),
20
        );
21
22
        // Register factory services
23
        foreach ($config->getSetupGacela()->getFactories() as $id => $factory) {
24
            $container->set($id, $container->factory($factory));
25
        }
26
27
        // Register protected services
28
        foreach ($config->getSetupGacela()->getProtectedServices() as $id => $service) {
29
            $container->set($id, $container->protect($service));
30
        }
31
32
        // Register aliases
33
        foreach ($config->getSetupGacela()->getAliases() as $alias => $id) {
34
            $container->alias($alias, $id);
35
        }
36
37
        // Register contextual bindings
38
        foreach ($config->getSetupGacela()->getContextualBindings() as $concrete => $needs) {
39
            foreach ($needs as $abstract => $implementation) {
40
                /** @var class-string $concrete */
41
                /** @var class-string $abstract */
42
                $container->when($concrete)->needs($abstract)->give($implementation);
43
            }
44
        }
45
46
        return $container;
47
    }
48
49
    public function getLocator(): LocatorInterface
50
    {
51
        return Locator::getInstance($this);
52
    }
53
}
54