Failed Conditions
Branch feature/init (0e876c)
by Yo
04:41 queued 01:07
created

BehatUtilsExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
wmc 9
lcom 0
cbo 6
ccs 0
cts 49
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigKey() 0 4 1
A initialize() 0 3 1
A configure() 0 9 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
    /**
16
     * {@inheritdoc}
17
     */
18
    public function getConfigKey()
19
    {
20
        return 'behat_utils';
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function initialize(ExtensionManager $extensionManager)
27
    {
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function configure(ArrayNodeDefinition $builder)
34
    {
35
        $builder->append(
36
            (new LoggerConfiguration())->getConfigTreeBuilder()
37
        );
38
        $builder->append(
39
            (new StepLoggerConfiguration())->getConfigTreeBuilder()
40
        );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function load(ContainerBuilder $container, array $config)
47
    {
48
        $this->bindConfigToContainer($container, $config);
49
50
        $loader = new XmlFileLoader(
51
            $container,
52
            new FileLocator(__DIR__.'/../Resources/config')
53
        );
54
55
        $loader->load('logger.xml');
56
        $loader->load('initializer.xml');
57
58
        if (true === $config['step_logger']['enabled']) {
59
            $loader->load('behat_step_logger.xml');
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function process(ContainerBuilder $container)
67
    {
68
    }
69
70
    /**
71
     * @param ContainerBuilder $container
72
     * @param array            $config
73
     * @param string           $baseId
74
     */
75
    protected function bindConfigToContainer(
76
        ContainerBuilder $container,
77
        array $config,
78
        $baseId = 'behat_utils_extension'
79
    ) {
80
        foreach ($config as $configKey => $configValue) {
81
            if (is_array($configValue)) {
82
                $this->bindConfigToContainer(
83
                    $container,
84
                    $configValue,
85
                    sprintf('%s.%s', $baseId, $configKey)
86
                );
87
            } else {
88
                $container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue);
89
            }
90
        }
91
    }
92
}
93