ContainerBuilder::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Container package.
5
 *
6
 * Copyright (c) Miloš Đurić <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Laganica\Di;
13
14
use ArrayObject;
15
use Laganica\Di\Definition\DefinitionFactory;
16
use Laganica\Di\Exception\ContainerException;
17
use Laganica\Di\Resolver\ResolverFactory;
18
19
/**
20
 * Class ContainerBuilder
21
 *
22
 * @package Laganica\Di
23
 */
24
class ContainerBuilder
25
{
26
    /**
27
     * @var ArrayObject
28
     */
29
    private $definitions;
30
31
    /**
32
     * @var bool
33
     */
34
    private $useAutowiring;
35
36
    /**
37
     * @var bool
38
     */
39
    private $useAnnotations;
40
41 22
    public function __construct()
42
    {
43 22
        $this->init();
44 22
    }
45
46
    /**
47
     * @param bool $useAutowiring
48
     */
49 22
    public function useAutowiring(bool $useAutowiring): void
50
    {
51 22
        $this->useAutowiring = $useAutowiring;
52 22
    }
53
54
    /**
55
     * @return bool
56
     */
57 21
    private function hasAutowiringEnabled(): bool
58
    {
59 21
        return $this->useAutowiring;
60
    }
61
62
    /**
63
     * @param bool $useAnnotations
64
     */
65 22
    public function useAnnotations(bool $useAnnotations): void
66
    {
67 22
        $this->useAnnotations = $useAnnotations;
68 22
    }
69
70
    /**
71
     * @return bool
72
     */
73 21
    private function hasAnnotationsEnabled(): bool
74
    {
75 21
        return $this->useAnnotations;
76
    }
77
78
    /**
79
     * @param array $definitions
80
     *
81
     * @throws ContainerException
82
     */
83 15
    public function addDefinitions(array $definitions): void
84
    {
85 15
        foreach ($definitions as $id => $definition) {
86 15
            if ($this->definitions->offsetExists($id)) {
87 1
                throw new ContainerException("More than one definition is found for entry or class $id");
88
            }
89
90 15
            $this->definitions->offsetSet($id, $definition);
91
        }
92 15
    }
93
94
    /**
95
     * @return ArrayObject
96
     */
97 21
    private function getDefinitions(): ArrayObject
98
    {
99 21
        return $this->definitions;
100
    }
101
102
    /**
103
     * @return Container
104
     */
105 21
    public function build(): Container
106
    {
107 21
        $container = new Container(new DefinitionFactory(), new ResolverFactory());
108 21
        $container->useAutowiring($this->hasAutowiringEnabled());
109 21
        $container->useAnnotations($this->hasAnnotationsEnabled());
110 21
        $container->setDefinitions($this->getDefinitions());
111
112 21
        $this->reset();
113
114 21
        return $container;
115
    }
116
117
    /**
118
     * @return void
119
     */
120 22
    private function init(): void
121
    {
122 22
        $this->useAutowiring(true);
123 22
        $this->useAnnotations(false);
124 22
        $this->definitions = new ArrayObject();
125 22
    }
126
127
    /**
128
     * @return void
129
     */
130 21
    private function reset(): void
131
    {
132 21
        $this->init();
133 21
    }
134
}
135