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

KernelSubExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 46.48%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 117
ccs 33
cts 71
cp 0.4648
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigKey() 0 4 1
A initialize() 0 8 2
B configure() 0 43 2
A load() 0 21 1
A process() 0 16 3
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
    /**
37
     * @inheritDoc
38
     */
39
    public function configure(ArrayNodeDefinition $builder)
40
    {
41
        $castToBool = function ($value) {
42
            $filtered = filter_var(
43
                $value,
44
                FILTER_VALIDATE_BOOLEAN,
45
                FILTER_NULL_ON_FAILURE
46
            );
47
48
            return (null === $filtered) ? (bool) $value : $filtered;
49
        };
50
        $builder
51
            ->addDefaultsIfNotSet()
52
            ->children()
53
                ->scalarNode('bootstrap')
54
                    ->defaultValue('app/autoload.php')
55
                ->end()
56
                ->scalarNode('path')
57
                    ->defaultValue('app/AppKernel.php')
58
                ->end()
59
                ->scalarNode('class')
60
                    ->defaultValue('AppKernel')
61
                ->end()
62
                ->scalarNode('env')
63
                    ->defaultValue('test')
64
                ->end()
65
                ->booleanNode('debug')
66
                    ->beforeNormalization()
67
                    ->always()
68
                        ->then($castToBool)
69
                    ->end()
70
                    ->defaultTrue()
71
                ->end()
72
                ->booleanNode('reboot')
73
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
74
                    ->beforeNormalization()
75
                        ->always()
76
                        ->then($castToBool)
77
                    ->end()
78
                    ->defaultTrue()
79
                ->end()
80
            ->end();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 2
    public function load(ContainerBuilder $container, array $config)
87
    {
88 2
        $kernelConfig = $config[$this->getConfigKey()];
89 2
        $container->setParameter(
90 2
            $this->buildContainerId('kernel.reboot'),
91 2
            $kernelConfig['reboot']
92 2
        );
93 2
        $container->setParameter(
94 2
            $this->buildContainerId('kernel.bootstrap'),
95 2
            $kernelConfig['bootstrap']
96 2
        );
97 2
        $this->createService(
98 2
            $container,
99 2
            'kernel',
100 2
            $kernelConfig['class'],
101
            [
102 2
                $kernelConfig['env'],
103 2
                $kernelConfig['debug'],
104
            ]
105 2
        );
106 2
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 3
    public function process(ContainerBuilder $container)
112
    {
113 3
        $bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap'));
114 3
        if ($bootstrapPath) {
115 2
            $bootstrap = sprintf(
116 2
                '%s/%s',
117 2
                $container->getParameter('paths.base'),
118
                $bootstrapPath
119 2
            );
120 2
            if (file_exists($bootstrap)) {
121 1
                require_once($bootstrap);
122 1
            } else {
123 1
                throw new ProcessingException('Could not find bootstrap file !');
124
            }
125 1
        }
126 2
    }
127
}
128