Passed
Push — feature/extension ( 80a1d3...dba08b )
by Yo
02:20
created

KernelSubExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.54%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 98
ccs 62
cts 67
cp 0.9254
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigKey() 0 4 1
A load() 0 17 1
A process() 0 16 3
B configure() 0 43 2
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension;
3
4
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
class KernelSubExtension extends AbstractSubExtension
9
{
10
    /**
11
     * @inheritDoc
12
     */
13 5
    public function getConfigKey()
14
    {
15 5
        return 'kernel';
16
    }
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function configure(ArrayNodeDefinition $builder)
22
    {
23 1
        $castToBool = function ($value) {
24
            $filtered = filter_var(
25
                $value,
26
                FILTER_VALIDATE_BOOLEAN,
27
                FILTER_NULL_ON_FAILURE
28
            );
29
30
            return (null === $filtered) ? (bool) $value : $filtered;
31 1
        };
32
        $builder
33 1
            ->addDefaultsIfNotSet()
34 1
            ->children()
35 1
                ->scalarNode('bootstrap')
36 1
                    ->defaultValue('app/autoload.php')
37 1
                ->end()
38 1
                ->scalarNode('path')
39 1
                    ->defaultValue('app/AppKernel.php')
40 1
                ->end()
41 1
                ->scalarNode('class')
42 1
                    ->defaultValue('AppKernel')
43 1
                ->end()
44 1
                ->scalarNode('env')
45 1
                    ->defaultValue('test')
46 1
                ->end()
47 1
                ->booleanNode('debug')
48 1
                    ->beforeNormalization()
49 1
                    ->always()
50 1
                        ->then($castToBool)
51 1
                    ->end()
52 1
                    ->defaultTrue()
53 1
                ->end()
54 1
                ->booleanNode('reboot')
55 1
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
56 1
                    ->beforeNormalization()
57 1
                        ->always()
58 1
                        ->then($castToBool)
59 1
                    ->end()
60 1
                    ->defaultTrue()
61 1
                ->end()
62 1
            ->end();
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 3
    public function load(ContainerBuilder $container, array $config)
69
    {
70 3
        $kernelConfig = $config[$this->getConfigKey()];
71 3
        $container->setParameter(
72 3
            $this->getContainerParamOrServiceId('kernel.reboot'),
73 3
            $kernelConfig['reboot']
74 3
        );
75 3
        $this->createService(
76 3
            $container,
77 3
            'kernel',
78 3
            $kernelConfig['class'],
79
            array(
80 3
                $kernelConfig['env'],
81 3
                $kernelConfig['debug'],
82
            )
83 3
        );
84 3
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 4
    public function process(ContainerBuilder $container)
90
    {
91 4
        $bootstrapPath = $container->getParameter($this->getContainerParamOrServiceId('kernel.bootstrap'));
92 4
        if ($bootstrapPath) {
93 2
            $bootstrap = sprintf(
94 2
                '%s/%s',
95 2
                $container->getParameter('paths.base'),
96
                $bootstrapPath
97 2
            );
98 2
            if (file_exists($bootstrap)) {
99 1
                require_once($bootstrap);
100 1
            } else {
101 1
                throw new ProcessingException('Could not find bootstrap file !');
102
            }
103 1
        }
104 3
    }
105
}
106