EnvironmentProcessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 23 4
A assertUnregisteredEnvironment() 0 9 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Kernel package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Kernel\Processor;
12
13
use Borobudur\Kernel\EnvironmentInterface;
14
use Borobudur\Kernel\Exception\RuntimeException;
15
use Borobudur\Kernel\KernelInterface;
16
17
/**
18
 * @author      Iqbal Maulana <[email protected]>
19
 * @created     8/17/15
20
 */
21
class EnvironmentProcessor implements ProcessorInterface
22
{
23
    /**
24
     * @var EnvironmentInterface[]
25
     */
26
    private $environments;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function initialize(KernelInterface $kernel)
32
    {
33
        $env = $kernel->getEnvironmentManager()->getEnv();
34
        if (empty($this->environments)) {
35
            foreach ($kernel->registerEnvironments() as $environment) {
36
                $name = $environment->getName();
37
                $this->assertUnregisteredEnvironment($name);
38
                $this->environments[$name] = $environment;
39
            }
40
        }
41
42
        if (!isset($this->environments[$env])) {
43
            throw new RuntimeException(sprintf(
44
                'Environment "%s" is not registered.',
45
                $env
46
            ));
47
        }
48
49
        $env = $this->environments[$env];
50
        $env->build($kernel);
51
52
        $kernel->getEnvironmentManager()->setEnvInstance($env);
53
    }
54
55
    /**
56
     * Assert that environment not registered.
57
     *
58
     * @param string $env
59
     */
60
    private function assertUnregisteredEnvironment($env)
61
    {
62
        if (isset($this->environments[$env])) {
63
            throw new RuntimeException(sprintf(
64
                'Environment "%s" already registered.',
65
                $env
66
            ));
67
        }
68
    }
69
}
70