Failed Conditions
Pull Request — master (#33)
by Yo
04:47 queued 02:20
created

Behat3SymfonyExtension::load()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 21
cts 21
cp 1
rs 8.6845
cc 4
eloc 16
nc 8
nop 2
crap 4
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer;
3
4
use Behat\MinkExtension\ServiceContainer\MinkExtension;
5
use Behat\Testwork\ServiceContainer\Extension;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Monolog\Logger;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
12
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\KernelConfiguration;
13
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\LoggerConfiguration;
14
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory;
15
16
class Behat3SymfonyExtension implements Extension
17
{
18
    const TEST_CLIENT_SERVICE_ID = 'behat3_symfony_extension.test.client';
19
    const KERNEL_SERVICE_ID = 'behat3_symfony_extension.kernel';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function getConfigKey()
25
    {
26 1
        return 'behat3_symfony';
27
    }
28
29
    // @codeCoverageIgnoreStart
30
    /**
31
     * (Not possible to cover this because ExtensionManager is a final class)
32
     *
33
     * {@inheritdoc}
34
     */
35
    public function initialize(ExtensionManager $extensionManager)
36
    {
37
        $minExtension = $extensionManager->getExtension('mink');
38
        if ($minExtension instanceof MinkExtension) {
39
            $minExtension->registerDriverFactory(new Behat3SymfonyFactory());
40
        }
41
    }
42
43
    /**
44
     * (Will be covered by Functional tests)
45
     * {@inheritdoc}
46
     */
47
    public function configure(ArrayNodeDefinition $builder)
48
    {
49
        $castToBool = function ($value) {
50
            $filtered = filter_var(
51
                $value,
52
                FILTER_VALIDATE_BOOLEAN,
53
                FILTER_NULL_ON_FAILURE
54
            );
55
56
            return (null === $filtered) ? (bool) $value : $filtered;
57
        };
58
        $builder->children()
59
            ->booleanNode('debug_mode')
60
                ->beforeNormalization()
61
                ->always()
62
                    ->then($castToBool)
63
                ->end()
64
                ->defaultFalse()
65
            ->end()
66
            ->end();
67
        $builder->append((new KernelConfiguration())->getConfigTreeBuilder());
68
        $builder->append((new LoggerConfiguration())->getConfigTreeBuilder());
69
    }
70
    // @codeCoverageIgnoreEnd
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4
    public function load(ContainerBuilder $container, array $config)
76
    {
77 4
        $config = $this->normalizeConfig($config);
78 4
        $this->bindConfigToContainer($container, $config);
79
80 4
        $loader = new XmlFileLoader(
81 4
            $container,
82 4
            new FileLocator(__DIR__.'/../Resources/config')
83 4
        );
84
85 4
        $loader->load('client.xml');
86 4
        $loader->load('kernel.xml');
87 4
        $loader->load('initializer.xml');
88 4
        $loader->load('logger.xml');
89 4
        if (true === $config['kernel']['reboot']) {
90 2
            $loader->load('kernel_auto_reboot.xml');
91 2
        }
92 4
        if (true === $config['kernel']['debug']) {
93 2
            $loader->load('kernel_debug_mode.xml');
94 2
        }
95 4
        if (true === $config['debug_mode']) {
96 2
            $loader->load('extension_debug_mode.xml');
97 2
        }
98 4
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function process(ContainerBuilder $container)
104
    {
105 3
        $bootstrapPath = $container->getParameter('behat3_symfony_extension.kernel.bootstrap');
106 3
        if ($bootstrapPath) {
107 2
            require_once($this->normalizePath($container, $bootstrapPath));
108 2
        }
109
110
        // load kernel
111 3
        $container->getDefinition(self::KERNEL_SERVICE_ID)
112 3
            ->setFile(
113 3
                $this->normalizePath(
114 3
                    $container,
115 3
                    $container->getParameter('behat3_symfony_extension.kernel.path')
116 3
                )
117 3
            );
118 3
    }
119
120
    /**
121
     * @param ContainerBuilder $container
122
     * @param string           $path
123
     *
124
     * @return string
125
     */
126 3
    protected function normalizePath(ContainerBuilder $container, $path)
127
    {
128 3
        $basePath = $container->getParameter('paths.base');
129 3
        $pathUnderBasePath = sprintf('%s/%s', $basePath, $path);
130 3
        if (file_exists($pathUnderBasePath)) {
131 2
            $path = $pathUnderBasePath;
132 2
        }
133
134 3
        return $path;
135
    }
136
137
    /**
138
     * @param ContainerBuilder $container
139
     * @param array            $config
140
     * @param string           $baseId
141
     */
142 4
    protected function bindConfigToContainer(
143
        ContainerBuilder $container,
144
        array $config,
145
        $baseId = 'behat3_symfony_extension'
146
    ) {
147 4
        foreach ($config as $configKey => $configValue) {
148 4
            if (is_array($configValue)) {
149 4
                $this->bindConfigToContainer(
150 4
                    $container,
151 4
                    $configValue,
152 4
                    sprintf('%s.%s', $baseId, $configKey)
153 4
                );
154 4
            } else {
155 4
                $container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue);
156
            }
157 4
        }
158 4
    }
159
160
    /**
161
     * @param array $config
162
     * @return array
163
     */
164 4
    protected function normalizeConfig(array $config)
165
    {
166 4
        if (true === $config['debug_mode']) {
167 2
            $config['kernel']['debug'] = true;
168 2
            $config['logger']['level'] = Logger::DEBUG;
169 2
        }
170
171 4
        return $config;
172
    }
173
}
174