Failed Conditions
Pull Request — master (#1)
by Yo
04:42
created

BehatUtilsExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 6
dl 0
loc 79
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigKey() 0 4 1
A initialize() 0 3 1
A configure() 0 5 1
A load() 0 16 2
A process() 0 3 1
A bindConfigToContainer() 0 17 3
1
<?php
2
namespace Yoanm\BehatUtilsExtension\ServiceContainer;
3
4
use Behat\Testwork\ServiceContainer\Extension;
5
use Behat\Testwork\ServiceContainer\ExtensionManager;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
use Yoanm\BehatUtilsExtension\ServiceContainer\Configuration\LoggerConfiguration;
11
use Yoanm\BehatUtilsExtension\ServiceContainer\Configuration\StepLoggerConfiguration;
12
13
class BehatUtilsExtension implements Extension
14
{
15
    const EXTENSION_CONFIG_KEY = 'behat_utils';
16
    const CONTAINER_KEY_BASE = 'behat_utils_extension';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 1
    public function getConfigKey()
22
    {
23 1
        return self::EXTENSION_CONFIG_KEY;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function initialize(ExtensionManager $extensionManager)
30
    {
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function configure(ArrayNodeDefinition $builder)
37
    {
38 1
        $builder->append((new LoggerConfiguration())->getConfigNode());
39 1
        $builder->append((new StepLoggerConfiguration())->getConfigNode());
40 1
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 4
    public function load(ContainerBuilder $container, array $config)
46
    {
47 4
        $this->bindConfigToContainer($container, $config);
48
49 4
        $loader = new XmlFileLoader(
50 4
            $container,
51 4
            new FileLocator(__DIR__.'/../Resources/config')
52 4
        );
53
54 4
        $loader->load('logger.xml');
55 4
        $loader->load('initializer.xml');
56
57 4
        if (true === $config['step_logger']['enabled']) {
58 2
            $loader->load('behat_step_logger.xml');
59 2
        }
60 4
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function process(ContainerBuilder $container)
66
    {
67 1
    }
68
69
    /**
70
     * @param ContainerBuilder $container
71
     * @param array            $config
72
     * @param string           $baseId
73
     */
74 4
    protected function bindConfigToContainer(
75
        ContainerBuilder $container,
76
        array $config,
77
        $baseId = self::CONTAINER_KEY_BASE
78
    ) {
79 4
        foreach ($config as $configKey => $configValue) {
80 4
            if (is_array($configValue)) {
81 4
                $this->bindConfigToContainer(
82 4
                    $container,
83 4
                    $configValue,
84 4
                    sprintf('%s.%s', $baseId, $configKey)
85 4
                );
86 4
            } else {
87 4
                $container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue);
88
            }
89 4
        }
90 4
    }
91
}
92