Container   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 92
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 3
A has() 0 4 1
A set() 0 8 1
A createInstance() 0 6 1
A hasEntry() 0 4 2
1
<?php
2
3
namespace Kambo\Deinj;
4
5
use Psr\Container\ContainerInterface;
6
use Kambo\Deinj\Exception\NotFoundException;
7
8
/**
9
 * Simple dependency injection container.
10
 *
11
 * @author  Bohuslav Simek <[email protected]>
12
 * @license MIT
13
 */
14
final class Container implements ContainerInterface
15
{
16
    private $instances = [];
17
    private $items = [];
18
19
    /**
20
     * Finds an entry of the container by its identifier and returns it.
21
     *
22
     * @param string $id Identifier of the entry to look for.
23
     *
24
     * @throws NotFoundException  No entry was found for **this** identifier.
25
     *
26
     * @return mixed Entry.
27
     */
28 3
    public function get($id)
29
    {
30 3
        if (!$this->hasEntry($id)) {
31 1
            throw new NotFoundException('Entry '.$id.' does not exists.');
32
        }
33
34 2
        if (!isset($this->instances[$id])) {
35 2
            $this->instances[$id] = $this->createInstance($id);
36
        }
37
38 2
        return $this->instances[$id];
39
    }
40
41
    /**
42
     * Check if the container can return an entry for the given identifier.
43
     *
44
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
45
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
46
     *
47
     * @param string $id Identifier of the entry to look for.
48
     *
49
     * @return bool Returns true if the container can return an entry for the given identifier.
50
     *              Returns false otherwise.
51
     */
52 2
    public function has($id)
53
    {
54 2
        return $this->hasEntry($id);
55
    }
56
57
    /**
58
     * Set entry into the container.
59
     *
60
     * @param string $id    Identifier of the entry to look for.
61
     * @param mixed  $entry Entry which should be returned
62
     *
63
     * @return self Instance of the self for the fluent interface.
64
     */
65 3
    public function set($id, $entry) : Container
66
    {
67 3
        unset($this->instances[$id]);
68
69 3
        $this->items[$id] = $entry;
70
71 3
        return $this;
72
    }
73
74
    // ------------ PRIVATE METHODS
75
76
    /**
77
     * Create instance of the class
78
     *
79
     * @param string $id Identifier of the entry to look for.
80
     *
81
     * @return mixed Instance of the entry
82
     */
83 2
    private function createInstance(string $id)
84
    {
85 2
        $factory = $this->items[$id];
86
87 2
        return $factory($this);
88
    }
89
90
    /**
91
     * Check if the container can return an entry for the given identifier.
92
     *
93
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
94
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
95
     *
96
     * @param string $id Identifier of the entry to look for.
97
     *
98
     * @return bool Returns true if the container can return an entry for the given identifier.
99
     *              Returns false otherwise.
100
     */
101 5
    private function hasEntry(string $id) : bool
102
    {
103 5
        return isset($this->items[$id]) ? true : false;
104
    }
105
}
106