Behat3SymfonyExtension   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 11
dl 0
loc 149
ccs 77
cts 77
cp 1
rs 10
c 0
b 0
f 0

9 Methods

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