Completed
Push — master ( 639c65...497f34 )
by Maxim
06:00
created

ContainerAdapter::get()   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 WebComplete\core\utils\container;
4
5
class ContainerAdapter implements ContainerInterface
6
{
7
    private $container;
8
9
    /**
10
     * @param object $container
11
     */
12
    public function __construct($container = null)
13
    {
14
        $this->container = $container;
15
    }
16
17
    /**
18
     * @param object $container
19
     */
20
    public function setContainer($container)
21
    {
22
        $this->container = $container;
23
    }
24
25
    /**
26
     * @param string $id
27
     * @return mixed
28
     * @throws ContainerException
29
     */
30
    public function get($id)
31
    {
32
        return $this->call('get', $id);
33
    }
34
35
    /**
36
     * @param string $id
37
     * @return mixed
38
     * @throws ContainerException
39
     */
40
    public function has($id)
41
    {
42
        return $this->call('has', $id);
43
    }
44
45
    /**
46
     * @param $id
47
     * @return mixed
48
     * @throws ContainerException
49
     */
50
    public function make($id)
51
    {
52
        return $this->call('make', $id);
53
    }
54
55
    /**
56
     * @param $method
57
     * @param $id
58
     *
59
     * @return mixed
60
     * @throws ContainerException
61
     */
62
    protected function call($method, $id)
63
    {
64
        if (!$this->container) {
65
            throw new ContainerException('Container not injected');
66
        }
67
        if (\method_exists($this->container, $method)) {
68
            return $this->container->$method($id);
69
        }
70
        throw new ContainerException('Container method not found: ' . $method);
71
    }
72
}
73