Completed
Push — master ( 3371e5...425c01 )
by Baptiste
11s
created

Behapi::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 9.5555
c 0
b 0
f 0
cc 1
eloc 54
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
namespace Behapi;
3
4
use Behat\Testwork\ServiceContainer\Extension;
5
use Behat\Testwork\ServiceContainer\ExtensionManager;
6
use Behat\Testwork\Cli\ServiceContainer\CliExtension;
7
8
use Behat\Behat\HelperContainer\ServiceContainer\HelperContainerExtension;
9
10
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
11
12
use Symfony\Component\DependencyInjection\Reference;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
15
use Behapi\Debug;
16
use Behapi\HttpHistory;
17
18
/**
19
 * Extension which feeds the dependencies of behapi's features
20
 *
21
 * @author Baptiste Clavié <[email protected]>
22
 */
23
final class Behapi implements Extension
24
{
25
    const DEBUG_INTROSPECTION_TAG = 'behapi.debug.introspection';
26
27
    /** {@inheritDoc} */
28
    public function getConfigKey()
29
    {
30
        return 'behapi';
31
    }
32
33
    /** {@inheritDoc} */
34
    public function configure(ArrayNodeDefinition $builder)
35
    {
36
        $builder
37
            ->children()
38
                ->scalarNode('base_url')
39
                    ->isRequired()
40
                    ->cannotBeEmpty()
41
                ->end()
42
43
                ->arrayNode('debug')
1 ignored issue
show
Bug introduced by
The method arrayNode() 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

43
                ->/** @scrutinizer ignore-call */ arrayNode('debug')
Loading history...
44
                    ->canBeDisabled()
45
                    ->children()
46
                        ->scalarNode('formatter')
47
                            ->defaultValue('pretty')
48
                            ->info('Not used anymore, only here for BC')
49
                        ->end()
50
51
                        ->arrayNode('introspection')
52
                            ->info('Debug Introspection configuration')
53
                            ->addDefaultsIfNotSet()
54
                            ->children()
55
                                ->arrayNode('var_dumper')
56
                                    ->addDefaultsIfNotSet()
57
                                    ->children()
58
                                        ->arrayNode('json')
59
                                            ->addDefaultsIfNotSet()
60
                                            ->children()
61
                                                ->arrayNode('types')
62
                                                    ->info('Types to be used in the json var-dumper adapter')
63
                                                    ->defaultValue(['application/json'])
64
                                                    ->prototype('scalar')->end()
65
                                                ->end()
66
                                            ->end()
67
                                        ->end()
68
                                    ->end()
69
                                ->end()
70
                            ->end()
71
                        ->end()
72
73
                        ->arrayNode('headers')
74
                            ->info('Headers to print in Debug Http introspection adapters')
75
                            ->addDefaultsIfNotSet()
76
                            ->children()
77
                                ->arrayNode('request')
78
                                    ->info('Request headers to print in Debug Http introspection adapters')
79
                                    ->defaultValue(['Content-Type'])
80
                                    ->prototype('scalar')->end()
81
                                ->end()
82
83
                                ->arrayNode('response')
84
                                    ->info('Response headers to print in Debug Http introspection adapters')
85
                                    ->defaultValue(['Content-Type'])
86
                                    ->prototype('scalar')->end()
87
                                ->end()
88
                            ->end()
89
                        ->end()
90
                    ->end()
91
                ->end()
92
            ->end()
93
        ->end();
94
95
    }
96
97
    /** {@inheritDoc} */
98
    public function initialize(ExtensionManager $extensionManager)
99
    {
100
    }
101
102
    /** {@inheritDoc} */
103
    public function load(ContainerBuilder $container, array $config)
104
    {
105
        $container->register(HttpHistory\History::class, HttpHistory\History::class)
106
            ->setPublic(false)
107
        ;
108
109
        $container->register(HttpHistory\Listener::class, HttpHistory\Listener::class)
110
            ->addArgument(new Reference(HttpHistory\History::class))
111
112
            ->setPublic(false)
113
            ->addTag('event_dispatcher.subscriber')
114
        ;
115
116
        $this->loadDebugServices($container, $config['debug']);
117
        $this->loadContainer($container, $config);
118
    }
119
120
    /** {@inheritDoc} */
121
    public function process(ContainerBuilder $container)
122
    {
123
        $dumpers = [];
124
125
        foreach ($container->findTaggedServiceIds(self::DEBUG_INTROSPECTION_TAG) as $id => $tags) {
126
            foreach ($tags as $attributes) {
127
                $priority = $attributes['priority'] ?? 0;
128
                $dumpers[$priority][] = new Reference($id);
129
            }
130
        }
131
132
        krsort($dumpers);
133
134
        $container->getDefinition(Debug\Listener::class)
135
            ->addArgument(array_merge(...$dumpers));
136
    }
137
138
    private function loadContainer(ContainerBuilder $container, array $config): void
139
    {
140
        $definition = $container->register(Container::class, Container::class);
141
142
        $definition
143
            ->addArgument(new Reference(HttpHistory\History::class))
144
            ->addArgument($config['base_url'])
145
        ;
146
147
        $definition->setPublic(true);
148
        $definition->setShared(false);
149
150
        $definition->addTag(HelperContainerExtension::HELPER_CONTAINER_TAG);
151
    }
152
153
    private function loadDebugServices(ContainerBuilder $container, array $config): void
154
    {
155
        if (!$config['enabled']) {
156
            return;
157
        }
158
159
        $container->register(Debug\Status::class, Debug\Status::class)
160
            ->setPublic(false)
161
        ;
162
163
        $container->register(Debug\CliController::class, Debug\CliController::class)
164
            ->addArgument(new Reference(Debug\Status::class))
165
166
            ->setPublic(false)
167
            ->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 10])
168
        ;
169
170
        $container->register(Debug\Listener::class, Debug\Listener::class)
171
            ->addArgument(new Reference(Debug\Status::class))
172
            ->addArgument(new Reference(HttpHistory\History::class))
173
174
            ->setPublic(false)
175
            ->addTag('event_dispatcher.subscriber')
176
        ;
177
178
        $adapters = [
179
            Debug\Introspection\Request\EchoerAdapter::class => [-100, [$config['headers']['request']]],
180
            Debug\Introspection\Response\EchoerAdapter::class => [-100, [$config['headers']['response']]],
181
182
            Debug\Introspection\Request\VarDumperAdapter::class => [-80, [$config['headers']['request']]],
183
            Debug\Introspection\Response\VarDumperAdapter::class => [-80, [$config['headers']['response']]],
184
185
            Debug\Introspection\Request\VarDumper\JsonAdapter::class => [-75, [$config['headers']['request'], $config['introspection']['var_dumper']['json']['types']]],
186
            Debug\Introspection\Response\VarDumper\JsonAdapter::class => [-75, [$config['headers']['response'], $config['introspection']['var_dumper']['json']['types']]],
187
        ];
188
189
        foreach ($adapters as $adapter => [$priority, $args]) {
190
            $def = $container->register($adapter, $adapter)
191
                ->addTag(self::DEBUG_INTROSPECTION_TAG, ['priority' => $priority])
192
            ;
193
194
            foreach ($args as $arg) {
195
                $def->addArgument($arg);
196
            }
197
        }
198
    }
199
}
200