Failed Conditions
Pull Request — release/0.2.0 (#35)
by Yo
02:16
created

Behat3SymfonyExtension::load()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

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