initWithConfigurationAndContainer()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 3
nop 2
1
<?php
2
/**
3
 * Veto.
4
 * PHP Microframework.
5
 *
6
 * @author Damien Walsh <[email protected]>
7
 * @copyright Damien Walsh 2013-2014
8
 * @version 0.1
9
 * @package veto
10
 */
11
namespace Veto\Layer;
12
13
use Veto\Configuration\Hive;
14
use Veto\DependencyInjection\Container;
15
use Veto\Configuration\Exception\ConfigurationException;
16
17
/**
18
 * LayerChain Builder
19
 *
20
 * Builds a LayerChain from configuration.
21
 */
22
class LayerChainBuilder
23
{
24
    /**
25
     * Construct a LayerChain from application configuration.
26
     *
27
     * @param Hive $config
28
     * @param Container $container
29
     * @throws ConfigurationException
30
     * @return LayerChain
31
     */
32
    public static function initWithConfigurationAndContainer(Hive $config, Container $container)
33
    {
34
        $layerChain = new LayerChain;
35
36
        // Register layers from configuration
37
        if (isset($config['layers']) && is_array($config['layers'])) {
38
            foreach ($config['layers'] as $layerName => $layer) {
39
40
                if (!array_key_exists('service', $layer)) {
41
                    throw ConfigurationException::missingSubkey('layer', 'service');
42
                }
43
44
                $newLayer = $container->get($layer['service']);
45
                $priority = array_key_exists('priority', $layer) ? intval($layer['priority']) : 0;
46
                $layerChain->addLayer($newLayer, $priority);
47
            }
48
        }
49
50
        return $layerChain;
51
    }
52
}
53