Completed
Push — master ( ad3d78...d4271d )
by Riikka
03:13
created

ContainerBuilder::getMethodIdentifier()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 6
rs 9.0777
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Container;
4
5
use Simply\Container\Entry\CallableEntry;
6
use Simply\Container\Entry\MixedEntry;
7
use Simply\Container\Entry\ProviderEntry;
8
use Simply\Container\Exception\ContainerException;
9
10
/**
11
 * Class that provides convenience functionality for setting up containers.
12
 *
13
 * @author Riikka Kalliomäki <[email protected]>
14
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
15
 * @license http://opensource.org/licenses/mit-license.php MIT License
16
 */
17
class ContainerBuilder
18
{
19
    /** @var Container The container that is being built */
20
    private $container;
21
22
    /**
23
     * ContainerBuilder constructor.
24
     */
25 2
    public function __construct()
26
    {
27 2
        $this->container = new Container();
28 2
    }
29
30
    /**
31
     * Returns the container that is being built.
32
     * @return Container The container that is being built
33
     */
34 2
    public function getContainer(): Container
35
    {
36 2
        return $this->container;
37
    }
38
39
    /**
40
     * Registers an array with identifier value pairs to the container as mixed entries.
41
     * @param array $configuration The configuration array to register
42
     * @throws ContainerException If trying to register values that already exist
43
     */
44 1
    public function registerConfiguration(array $configuration): void
45
    {
46 1
        foreach ($configuration as $identifier => $value) {
47 1
            $this->container->addEntry($identifier, new MixedEntry($value));
48
        }
49 1
    }
50
51
    /**
52
     * Registers the given provider as a callable and applicable methods as provider methods.
53
     * @param EntryProvider $provider The entry provider to register
54
     * @throws ContainerException If the provider tries to provide something that has already been registered
55
     */
56 1
    public function registerProvider(EntryProvider $provider): void
57
    {
58 1
        $class = \get_class($provider);
59
60 1
        $this->container->addEntry($class, new CallableEntry([\get_class($provider), 'initialize']));
61
62 1
        foreach ($provider->getMethods() as $identifier => $method) {
63 1
            $this->container->addEntry($identifier, new ProviderEntry([$provider, $method]));
64
        }
65 1
    }
66
}
67