Passed
Push — master ( d115e1...fbfc99 )
by Mr
03:40
created

App   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A set() 0 10 2
A getContainer() 0 3 1
A setContainer() 0 4 1
A __construct() 0 3 1
A get() 0 4 1
1
<?php
2
3
namespace DrMVC;
4
5
//use Psr\Container\ContainerInterface;
6
//use Psr\Container\NotFoundExceptionInterface;
7
8
use DrMVC\Config\ConfigInterface;
9
10
class App implements AppInterface
11
{
12
    private $_containers = [];
13
14
    public function __construct(ConfigInterface $config)
15
    {
16
        $this->setContainer('config', $config);
17
    }
18
19
    /**
20
     * @param   string $name
21
     * @return  mixed
22
     */
23
    private function getContainer(string $name)
24
    {
25
        return $this->_containers[$name];
26
    }
27
28
    /**
29
     * @param   string $name
30
     * @param   object $object
31
     * @return  AppInterface
32
     */
33
    private function setContainer(string $name, $object): AppInterface
34
    {
35
        $this->_containers[$name] = $object;
36
        return $this;
37
    }
38
39
    /**
40
     * PSR-11 set container
41
     *
42
     * @param   string $container
43
     * @param   string $object
44
     * @param   ConfigInterface $config
45
     * @return  AppInterface
46
     */
47
    public function set(string $container, string $object, ConfigInterface $config): AppInterface
48
    {
49
        $objectConfig = $config->get($object);
50
51
        if (null !== $objectConfig) {
52
            $class = '\\DrMVC\\' . $object;
53
            $this->setContainer($container, new $class($config));
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * Get container by name
61
     *
62
     * @param   string $name
63
     * @return  mixed
64
     */
65
    public function get($name)
66
    {
67
        // TODO: NotFoundExceptionInterface
68
        return $this->has($name) ?? $this->getContainer($name);
69
    }
70
71
    /**
72
     * Container is exist
73
     *
74
     * @param   string $name
75
     * @return  bool
76
     */
77
    public function has($name): bool
78
    {
79
        // TODO: NotFoundExceptionInterface
80
        return isset($this->_containers[$name]);
81
    }
82
}
83