Passed
Push — master ( 40ae7f...ed7868 )
by Marcel
03:53
created

Container.php$0 ➔ resolved()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UMA\DIC;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
10
class Container implements ContainerInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $container;
16
17
    /**
18
     * @param array $entries Array of string => mixed.
19
     */
20 6
    public function __construct(array $entries = [])
21
    {
22 6
        $this->container = $entries;
23 6
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 5
    public function get($id)
29
    {
30 5
        if (!$this->resolved($id)) {
31 1
            $this->container[$id] = \call_user_func($this->container[$id], $this);
32
        }
33
34 4
        return $this->container[$id];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function has($id)
41
    {
42 6
        return \array_key_exists($id, $this->container);
43
    }
44
45
    /**
46
     * @param string $id
47
     * @param mixed  $entry
48
     */
49 3
    public function set(string $id, $entry): void
50
    {
51 3
        $this->container[$id] = $entry;
52 3
    }
53
54 1
    public function register(ServiceProvider $provider): void
55
    {
56 1
        $provider->provide($this);
57 1
    }
58
59
    /**
60
     * Returns whether a given service has already been resolved
61
     * into its final value, or is still a callable.
62
     *
63
     * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
64
     */
65 6
    public function resolved(string $id): bool
66
    {
67 6
        if (!$this->has($id)) {
68
            throw new class extends \LogicException implements NotFoundExceptionInterface {};
69
        }
70
71 4
        return !$this->container[$id] instanceof \Closure;
72
    }
73
}
74