Passed
Push — feature/improve ( ac8030...d3c170 )
by Yo
02:14
created

KernelSubExtension::configure()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 0
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 38
nc 1
nop 1
crap 6
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension;
3
4
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
5
use Behat\Testwork\ServiceContainer\ExtensionManager;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Yoanm\Behat3SymfonyExtension\ServiceContainer\AbstractExtension;
9
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Driver\Behat3SymfonyDriverFactory;
10
11
class KernelSubExtension extends AbstractExtension
12
{
13
    /**
14
     * @inheritDoc
15
     */
16 3
    public function getConfigKey()
17
    {
18 3
        return 'kernel';
19
    }
20
21
    // @codeCoverageIgnoreStart
22
    // Not possible to cover this because ExtensionManager is a final class
23
    /**
24
     * @inheritDoc
25
     */
26
    public function initialize(ExtensionManager $extensionManager)
27
    {
28
        $minExtension = $extensionManager->getExtension('mink');
29
30
        if ($minExtension) {
31
            $minExtension->registerDriverFactory(new Behat3SymfonyDriverFactory());
32
        }
33
    }
34
    // @codeCoverageIgnoreEnd
35
36
    // @codeCoverageIgnoreStart
37
    // Will be covered by FT
38
    /**
39
     * @inheritDoc
40
     */
41
    public function configure(ArrayNodeDefinition $builder)
42
    {
43
        $castToBool = function ($value) {
44
            $filtered = filter_var(
45
                $value,
46
                FILTER_VALIDATE_BOOLEAN,
47
                FILTER_NULL_ON_FAILURE
48
            );
49
50
            return (null === $filtered) ? (bool) $value : $filtered;
51
        };
52
        $builder
53
            ->addDefaultsIfNotSet()
54
            ->children()
55
                ->scalarNode('bootstrap')
56
                    ->defaultValue('app/autoload.php')
57
                ->end()
58
                ->scalarNode('path')
59
                    ->defaultValue('app/AppKernel.php')
60
                ->end()
61
                ->scalarNode('class')
62
                    ->defaultValue('AppKernel')
63
                ->end()
64
                ->scalarNode('env')
65
                    ->defaultValue('test')
66
                ->end()
67
                ->booleanNode('debug')
68
                    ->beforeNormalization()
69
                    ->always()
70
                        ->then($castToBool)
71
                    ->end()
72
                    ->defaultTrue()
73
                ->end()
74
                ->booleanNode('reboot')
75
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
76
                    ->beforeNormalization()
77
                        ->always()
78
                        ->then($castToBool)
79
                    ->end()
80
                    ->defaultTrue()
81
                ->end()
82
            ->end();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function load(ContainerBuilder $container, array $config)
89
    {
90
        $kernelConfig = $config[$this->getConfigKey()];
91
        $container->setParameter(
92
            $this->buildContainerId('kernel.reboot'),
93
            $kernelConfig['reboot']
94
        );
95
        $container->setParameter(
96
            $this->buildContainerId('kernel.bootstrap'),
97
            $kernelConfig['bootstrap']
98
        );
99
        $this->createService(
100
            $container,
101
            'kernel',
102
            $kernelConfig['class'],
103
            [
104
                $kernelConfig['env'],
105
                $kernelConfig['debug'],
106
            ]
107
        );
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function process(ContainerBuilder $container)
114
    {
115
        $bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap'));
116
        if ($bootstrapPath) {
117
            $bootstrap = sprintf(
118
                '%s/%s',
119
                $container->getParameter('paths.base'),
120
                $bootstrapPath
121
            );
122
            if (file_exists($bootstrap)) {
123
                require_once($bootstrap);
124
            } else {
125
                throw new ProcessingException('Could not find bootstrap file !');
126
            }
127
        }
128
    }
129
}
130