Completed
Push — develop ( dd2af7...2c61cc )
by Baptiste
09:23 queued 01:14
created

Container::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose;
5
6
use Innmind\Compose\{
7
    Definition\Name,
8
    Exception\NotFound
9
};
10
use Innmind\Immutable\Map;
11
use Psr\Container\ContainerInterface;
12
13
final class Container implements ContainerInterface
14
{
15
    private $definitions;
16
    private $instances;
17
18 6
    public function __construct(Definitions $definitions)
19
    {
20 6
        $this->definitions = $definitions;
21 6
        $this->instances = new Map('string', 'object');
22 6
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function get($id): object
28
    {
29 4
        if (!$this->has($id)) {
30 3
            throw new NotFound($id);
31
        }
32
33 1
        return $this->build(new Name($id));
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 5
    public function has($id): bool
40
    {
41 5
        $name = new Name($id);
42
43 5
        if (!$this->definitions->has($name)) {
44 2
            return false;
45
        }
46
47
        $definition = $this
48 4
            ->definitions
49 4
            ->get($name);
50
51 4
        if (!$definition->exposed()) {
52 2
            return false;
53
        }
54
55 3
        return $definition->isExposedAs($name);
56
    }
57
58 1
    private function build(Name $name): object
59
    {
60 1
        if ($this->instances->contains((string) $name)) {
61 1
            return $this->instances->get((string) $name);
62
        }
63
64 1
        $instance = $this->definitions->build($name);
65 1
        $this->instances = $this->instances->put((string) $name, $instance);
66
67 1
        return $instance;
68
    }
69
}
70