Passed
Push — master ( 797714...39cab3 )
by Rougin
03:07
created

ZapheusKernel::defaults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Zapheus\Bridge\Symfony;
4
5
use Symfony\Component\Config\Loader\LoaderInterface;
6
use Symfony\Component\HttpKernel\Kernel;
7
use Zapheus\Provider\ConfigurationInterface;
8
9
/**
10
 * Zapheus Kernel
11
 *
12
 * @package Zapheus
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class ZapheusKernel extends Kernel
16
{
17
    /**
18
     * @var \Zapheus\Provider\ConfigurationInterface
19
     */
20
    protected $configuration;
21
22
    /**
23
     * @var \Symfony\Component\HttpKernel\Bundle\BundleInterface[]
24
     */
25
    protected $items = array();
26
27
    /**
28
     * Initializes the kernel instance.
29
     *
30
     * @param \Symfony\Component\HttpKernel\Bundle\BundleInterface[] $bundles
31
     * @param \Zapheus\Provider\ConfigurationInterface               $configuration
32
     */
33 3
    public function __construct(array $bundles, ConfigurationInterface $configuration)
34
    {
35 3
        $this->configuration = $configuration;
36
37 3
        $parameters = $configuration->get('symfony', array(), true);
38
39 3
        $parameters = $this->defaults((array) $parameters);
40
41 3
        $this->debug = (boolean) $parameters['kernel.debug'];
42
43 3
        $this->environment = $parameters['kernel.environment'];
44
45 3
        $this->rootDir = $parameters['kernel.root_dir'];
46
47 3
        $this->name = $parameters['kernel.name'];
48
49 3
        $this->debug && $this->startTime = microtime(true);
50
51 3
        $this->items = $bundles;
52 3
    }
53
54
    /**
55
     * Returns an array of bundles to register.
56
     *
57
     * @return \Symfony\Component\HttpKernel\Bundle\BundleInterface[]
58
     */
59 3
    public function registerBundles()
60
    {
61 3
        return (array) $this->items;
62
    }
63
64
    /**
65
     * Loads the container configuration.
66
     *
67
     * @param  \Symfony\Component\Config\Loader\LoaderInterface $loader
68
     * @return void
69
     */
70 3
    public function registerContainerConfiguration(LoaderInterface $loader)
71
    {
72 3
        $configuration = $this->configuration;
73
74 3
        $loader->load(function ($container) use ($configuration) {
75 3
            $items = $configuration->get('symfony', array(), true);
76
77 3
            foreach ((array) $items as $key => $value) {
78 3
                $exists = $container->hasParameter($key) === true;
79
80 3
                $exists || $container->setParameter($key, $value);
81 3
            }
82 3
        });
83 3
    }
84
85
    /**
86
     * Returns an array of default kernel parameters.
87
     *
88
     * @param  array $config
89
     * @return array
90
     */
91 3
    protected function defaults(array $config)
92
    {
93 3
        $items = array('kernel.debug' => true);
94
95 3
        $items['kernel.environment'] = 'dev';
96
97 3
        $items['kernel.root_dir'] = $this->getRootDir();
98
99 3
        $items['kernel.name'] = $this->getName();
100
101 3
        foreach ((array) $items as $key => $value) {
102 3
            $exists = isset($config[$key]);
103
104 3
            $exists || $config[$key] = $value;
105 3
        }
106
107 3
        return $config;
108
    }
109
}
110