Completed
Push — master ( fbfc99...da50d4 )
by Mr
02:29
created

Containers::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DrMVC\Framework;
4
5
use DrMVC\Config\ConfigInterface;
6
7
class Containers implements ContainersInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $_containers = [];
13
14
    /**
15
     * @param   string $name
16
     * @return  mixed
17
     */
18
    private function getContainer(string $name)
19
    {
20
        return $this->_containers[$name];
21
    }
22
23
    /**
24
     * @param   string $name
25
     * @param   object $object
26
     * @return  ContainersInterface
27
     */
28
    private function setContainer(string $name, $object): ContainersInterface
29
    {
30
        $this->_containers[$name] = $object;
31
        return $this;
32
    }
33
34
    /**
35
     * PSR-11 set container
36
     *
37
     * @param   string $container
38
     * @param   string|object $object
39
     * @param   ConfigInterface $config
40
     * @return  ContainersInterface
41
     */
42
    public function set(string $container, $object, ConfigInterface $config = null): ContainersInterface
43
    {
44
        if (\is_object($object)) {
45
            $this->setContainer($container, $object);
46
        } else {
47
            $class = '\\DrMVC\\' . $object;
48
            $this->setContainer($container, new $class($config));
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * Get container by name
56
     *
57
     * @param   string $name
58
     * @return  mixed
59
     */
60
    public function get($name)
61
    {
62
        // TODO: NotFoundExceptionInterface
63
        return $this->has($name) ? $this->getContainer($name) : null;
64
    }
65
66
    /**
67
     * Container is exist
68
     *
69
     * @param   string $name
70
     * @return  bool
71
     */
72
    public function has($name): bool
73
    {
74
        // TODO: NotFoundExceptionInterface
75
        return isset($this->_containers[$name]);
76
    }
77
}
78