Container   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 83
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A set() 0 5 1
A configureFromFile() 0 6 2
A get() 0 12 3
A listServiceIds() 0 4 1
A getServiceFromFactory() 0 6 2
A has() 0 4 1
A setDelegateContainer() 0 4 1
1
<?php
2
namespace UltraLite\Container;
3
4
use Psr\Container\ContainerInterface;
5
use Psr\Container\NotFoundExceptionInterface;
6
use UltraLite\Container\Exception\DiServiceNotFound;
7
8
class Container implements ContainerInterface
9
{
10
    /** @var \Closure[] */
11
    private $serviceFactories = [];
12
13
    /** @var array */
14
    private $services = [];
15
16
    /** @var ContainerInterface */
17
    private $delegateContainer;
18
19
    /**
20
     * @param \Closure[] $serviceFactories
21
     */
22
    public function __construct(array $serviceFactories = [])
23
    {
24
        foreach ($serviceFactories as $serviceId => $serviceFactory) {
25
            $this->set($serviceId, $serviceFactory);
26
        }
27
    }
28
29
    public function set(string $serviceId, \Closure $serviceFactory)
30
    {
31
        $this->serviceFactories[$serviceId] = $serviceFactory;
32
        unset($this->services[$serviceId]);
33
    }
34
35
    public function configureFromFile(string $path)
36
    {
37
        foreach (require $path as $serviceId => $serviceFactory) {
38
            $this->set($serviceId, $serviceFactory);
39
        }
40
    }
41
42
    /**
43
     * @throws NotFoundExceptionInterface
44
     *
45
     * @return mixed
46
     */
47
    public function get(string $serviceId)
48
    {
49
        if (!$this->has($serviceId)) {
50
            throw DiServiceNotFound::createFromServiceId($serviceId);
51
        }
52
53
        if (!isset($this->services[$serviceId])) {
54
            $this->services[$serviceId] = $this->getServiceFromFactory($serviceId);
55
        }
56
57
        return $this->services[$serviceId];
58
    }
59
60
    /**
61
     * @return string[]
62
     */
63
    public function listServiceIds() : array
64
    {
65
        return array_keys($this->serviceFactories);
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    private function getServiceFromFactory(string $serviceId)
72
    {
73
        $serviceFactory = $this->serviceFactories[$serviceId];
74
        $containerToUseForDependencies = $this->delegateContainer ?: $this;
75
        return $serviceFactory($containerToUseForDependencies);
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function has(string $serviceId)
82
    {
83
        return isset($this->serviceFactories[$serviceId]);
84
    }
85
86
    public function setDelegateContainer(ContainerInterface $delegateContainer)
87
    {
88
        $this->delegateContainer = $delegateContainer;
89
    }
90
}
91