Failed Conditions
Push — feature/extension ( 6c86ef...6608eb )
by Yo
03:49
created

KernelSubExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 2
crap 1
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 3
    public function getConfigKey()
14
    {
15 3
        return 'kernel';
16
    }
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function configure(ArrayNodeDefinition $builder)
22
    {
23
        /**
24
         * @codeCoverageIgnoreStart
25
         * Will be a pain to cover this manually
26
         */
27
        $castToBool = function ($value) {
28
            $filtered = filter_var(
29
                $value,
30
                FILTER_VALIDATE_BOOLEAN,
31
                FILTER_NULL_ON_FAILURE
32
            );
33
34
            return (null === $filtered) ? (bool) $value : $filtered;
35
        };
36
        $builder
37
            ->addDefaultsIfNotSet()
38
            ->children()
39
                ->scalarNode('bootstrap')
40
                    ->defaultValue('app/autoload.php')
41
                ->end()
42
                ->scalarNode('path')
43
                    ->defaultValue('app/AppKernel.php')
44
                ->end()
45
                ->scalarNode('class')
46
                    ->defaultValue('AppKernel')
47
                ->end()
48
                ->scalarNode('env')
49
                    ->defaultValue('test')
50
                ->end()
51
                ->booleanNode('debug')
52
                    ->beforeNormalization()
53
                    ->always()
54
                        ->then($castToBool)
55
                    ->end()
56
                    ->defaultTrue()
57
                ->end()
58
                ->booleanNode('reboot')
59
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
60
                    ->beforeNormalization()
61
                        ->always()
62
                        ->then($castToBool)
63
                    ->end()
64
                    ->defaultTrue()
65
                ->end()
66
            ->end();
67
        // @codeCoverageIgnoreEnd
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function load(ContainerBuilder $container, array $config)
74
    {
75 2
        $kernelConfig = $config[$this->getConfigKey()];
76 2
        $container->setParameter(
77 2
            $this->getContainerParamOrServiceId('kernel.reboot'),
78 2
            $kernelConfig['reboot']
79 2
        );
80 2
        $this->createService(
81 2
            $container,
82 2
            'kernel',
83 2
            $kernelConfig['class'],
84
            array(
85 2
                $kernelConfig['env'],
86 2
                $kernelConfig['debug'],
87
            )
88 2
        );
89 2
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 3
    public function process(ContainerBuilder $container)
95
    {
96 3
        $bootstrapPath = $container->getParameter($this->getContainerParamOrServiceId('kernel.bootstrap'));
97 3
        if ($bootstrapPath) {
98 2
            $bootstrap = sprintf(
99 2
                '%s/%s',
100 2
                $container->getParameter('paths.base'),
101
                $bootstrapPath
102 2
            );
103 2
            if (file_exists($bootstrap)) {
104 1
                require_once($bootstrap);
105 1
            } else {
106 1
                throw new ProcessingException('Could not find bootstrap file !');
107
            }
108 1
        }
109 2
    }
110
}
111