EnvironmentFactory::constructEnvironment()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 4
nop 2
crap 6
1
<?php
2
3
namespace Assimtech\Tempo\Factory;
4
5
use Assimtech\Tempo\Environment;
6
7
/**
8
 * Constructs Environments from configuration
9
 */
10
class EnvironmentFactory
11
{
12
    /**
13
     * @param array $config
14
     * @param \Assimtech\Tempo\Node\NodeInterface[] $nodes
15
     * @return \Assimtech\Tempo\Environment[]
16
     */
17 1
    public function create(array $config, array $nodes)
18
    {
19 1
        $environments = array();
20
21 1
        foreach ($config as $environmentConfig) {
22 1
            $environments[] = $this->constructEnvironment(
23 1
                $environmentConfig,
24
                $nodes
25 1
            );
26 1
        }
27
28 1
        return $environments;
29
    }
30
31 1
    protected function constructEnvironment($environmentConfig, $nodes)
32
    {
33
        // Replace node names with node instances
34 1
        if (isset($environmentConfig['nodes'])) {
35 1
            foreach ($environmentConfig['nodes'] as $i => $nodeName) {
36 1
                $environmentConfig['nodes'][$i] = $nodes[$nodeName];
37 1
            }
38 1
        }
39
40
        // Replace node names with node instances
41 1
        if (isset($environmentConfig['roles'])) {
42 1
            foreach ($environmentConfig['roles'] as $role => $nodeNames) {
43 1
                foreach ($nodeNames as $i => $nodeName) {
44 1
                    $environmentConfig['roles'][$role][$i] = $nodes[$nodeName];
45 1
                }
46 1
            }
47 1
        }
48
49 1
        return new Environment($environmentConfig);
50
    }
51
}
52