Completed
Push — master ( 76d715...eae939 )
by Yo
02:10
created

Behat3SymfonyExtension::load()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
11
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\KernelConfiguration;
12
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\LoggerConfiguration;
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
42
    /**
43 1
     * (Will be covered by Functional tests)
44
     * {@inheritdoc}
45
     */
46
    public function configure(ArrayNodeDefinition $builder)
47
    {
48
        $castToBool = function ($value) {
49
            $filtered = filter_var(
50
                $value,
51
                FILTER_VALIDATE_BOOLEAN,
52
                FILTER_NULL_ON_FAILURE
53
            );
54
55
            return (null === $filtered) ? (bool) $value : $filtered;
56
        };
57
        $builder->children()
58
            ->booleanNode('debug_mode')
59
                ->beforeNormalization()
60
                ->always()
61
                    ->then($castToBool)
62
                ->end()
63
                ->defaultFalse()
64
            ->end()
65
            ->end();
66
        $builder->append((new KernelConfiguration())->getConfigTreeBuilder());
67
        $builder->append((new LoggerConfiguration())->getConfigTreeBuilder());
68
    }
69
    // @codeCoverageIgnoreEnd
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4
    public function load(ContainerBuilder $container, array $config)
75
    {
76 4
        $this->bindConfigToContainer($container, $config);
77
78 4
        $loader = new XmlFileLoader(
79 4
            $container,
80 4
            new FileLocator(__DIR__.'/../Resources/config')
81 4
        );
82
83 4
        $loader->load('client.xml');
84 4
        $loader->load('kernel.xml');
85 4
        $loader->load('initializer.xml');
86 4
        $loader->load('logger.xml');
87 4
        if (true === $config['kernel']['reboot']) {
88 2
            $loader->load('kernel_auto_reboot.xml');
89 2
        }
90 4
        if (true === $config['kernel']['debug']) {
91 2
            $loader->load('kernel_debug_mode.xml');
92 2
        }
93 4
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 3
    public function process(ContainerBuilder $container)
99
    {
100 3
        $bootstrapPath = $container->getParameter('behat3_symfony_extension.kernel.bootstrap');
101 3
        if ($bootstrapPath) {
102 2
            require_once($this->normalizePath($container, $bootstrapPath));
103 2
        }
104
105
        // load kernel
106 3
        $container->getDefinition(self::KERNEL_SERVICE_ID)
107 3
            ->setFile(
108 3
                $this->normalizePath(
109 3
                    $container,
110 3
                    $container->getParameter('behat3_symfony_extension.kernel.path')
111 3
                )
112 3
            );
113 3
    }
114
115
    /**
116
     * @param ContainerBuilder $container
117
     * @param string           $path
118
     *
119
     * @return string
120
     */
121 3
    protected function normalizePath(ContainerBuilder $container, $path)
122
    {
123 3
        $basePath = $container->getParameter('paths.base');
124 3
        $pathUnderBasePath = sprintf('%s/%s', $basePath, $path);
125 3
        if (file_exists($pathUnderBasePath)) {
126 2
            $path = $pathUnderBasePath;
127 2
        }
128
129 3
        return $path;
130
    }
131
132
    /**
133
     * @param ContainerBuilder $container
134
     * @param array            $config
135
     * @param string           $baseId
136
     */
137 4
    protected function bindConfigToContainer(
138
        ContainerBuilder $container,
139
        array $config,
140
        $baseId = 'behat3_symfony_extension'
141
    ) {
142 4
        foreach ($config as $configKey => $configValue) {
143 4
            if (is_array($configValue)) {
144 4
                $this->bindConfigToContainer(
145 4
                    $container,
146 4
                    $configValue,
147 4
                    sprintf('%s.%s', $baseId, $configKey)
148 4
                );
149 4
            } else {
150 4
                $container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue);
151
            }
152 4
        }
153 4
    }
154
}
155