Container   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 15
c 4
b 0
f 0
dl 0
loc 39
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocator() 0 3 1
A withConfig() 0 32 6
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