getOutputTypeInitializer()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bex\Behat\StepTimeLoggerExtension\ServiceContainer;
4
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
12
/**
13
 * This class is the entry point of the step time logger extension
14
 *
15
 * @license http://opensource.org/licenses/MIT The MIT License
16
 */
17
class StepTimeLoggerExtension implements Extension
18
{
19
    const CONFIG_KEY = 'steptimelogger';
20
21
     /**
22
     * {@inheritdoc}
23
     */
24
    public function getConfigKey()
25
    {
26
        return self::CONFIG_KEY;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process(ContainerBuilder $container)
33
    {
34
        // nothing to do here
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function initialize(ExtensionManager $extensionManager)
41
    {
42
        // nothing to do here
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function configure(ArrayNodeDefinition $builder)
49
    {
50
        $builder
51
            ->children()
52
                ->booleanNode('enabled_always')
53
                    ->defaultFalse()
54
                ->end()
55
                ->scalarNode('output_directory')
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                ->/** @scrutinizer ignore-call */ scalarNode('output_directory')
Loading history...
56
                    ->defaultValue(sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::CONFIG_KEY)
57
                ->end()
58
                ->arrayNode('output')
59
                    ->defaultValue(['console'])
60
                    ->beforeNormalization()
61
                        ->always($this->getOutputTypeInitializer())
62
                    ->end()
63
                    ->validate()
64
                        ->always($this->getOutputTypeValidator())
65
                    ->end()
66
                    ->prototype('scalar')->end()
67
                ->end()
68
            ->end();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function load(ContainerBuilder $container, array $config)
75
    {
76
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/config'));
77
        $loader->load('services.xml');
78
79
        $extensionConfig = new Config($container, $config);
80
        $container->set('bex.step_time_logger_extension.config', $extensionConfig);
81
    }
82
83
    /**
84
     * @return \Closure
85
     */
86
    private function getOutputTypeInitializer()
87
    {
88
        return function ($value) {
89
            $value = empty($value) ? ['console'] : $value;
90
            return is_array($value) ? $value : [$value];
91
        };
92
    }
93
94
    /**
95
     * @return \Closure
96
     */
97
    private function getOutputTypeValidator()
98
    {
99
        return function ($value) {
100
            $allowed = ['console', 'csv'];
101
            $invalid = array_diff($value, $allowed);
102
            
103
            if (!empty($invalid)) {
104
                $message = 'Invalid output types: %s. Allowed types: %s';
105
                throw new \InvalidArgumentException(sprintf($message, join(',', $invalid), join(',', $allowed)));
106
            }
107
108
            return $value;
109
        };
110
    }
111
}