EnvironmentProcessor::initialize()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
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