Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 70
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 70
cts 70
cp 1
rs 7.2848
c 0
b 0
f 0
cc 7
nc 2
nop 0
crap 7

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
2
3
namespace League\Tactician\Bundle\DependencyInjection;
4
5
use League\Tactician\Bundle\DependencyInjection\Compiler\BusBuilder\BusBuildersFromConfig;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
9
10
/**
11
 * This is the class that validates and merges configuration from your app/config files.
12
 *
13
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * Create a rootnode tree for configuration that can be injected into the DI container.
19
     *
20
     * @return TreeBuilder
21
     */
22 111
    public function getConfigTreeBuilder()
23
    {
24 111
        $treeBuilder = new TreeBuilder('tactician');
25
26 111
        if (\method_exists($treeBuilder, 'getRootNode')) {
27 74
            $rootNode = $treeBuilder->getRootNode();
28
        } else {
29
            // BC layer for symfony/config 4.1 and older
30 37
            $rootNode = $treeBuilder->root('tactician');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
        }
32
33
        $rootNode
34 111
            ->children()
35 111
                ->arrayNode('commandbus')
36 111
                    ->defaultValue(['default' => ['middleware' => ['tactician.middleware.command_handler']]])
37 111
                    ->requiresAtLeastOneElement()
38 111
                    ->useAttributeAsKey('name')
39 111
                    ->prototype('array')
40 111
                        ->children()
41 111
                            ->arrayNode('middleware')
42 111
                                ->requiresAtLeastOneElement()
43 111
                                ->useAttributeAsKey('name')
44 111
                                ->prototype('scalar')->end()
45 111
                                ->validate()
46 111
                                    ->ifTrue(function ($config) {
47 90
                                        $isPresent = in_array('tactician.middleware.command_handler', $config);
48 90
                                        $isLast = end($config) == 'tactician.middleware.command_handler';
49
50 90
                                        return $isPresent && !$isLast;
51 111
                                    })
52 111
                                    ->thenInvalid(
53
                                        '"tactician.middleware.command_handler" should be the last middleware loaded '.
54 111
                                        'when it is used.'
55
                                    )
56 111
                                ->end()
57 111
                            ->end()
58 111
                            ->scalarNode('method_inflector')->end()
59 111
                        ->end()
60 111
                    ->end()
61 111
                ->end()
62 111
                ->scalarNode('default_bus')
63 111
                    ->defaultValue(BusBuildersFromConfig::DEFAULT_BUS_ID)
64 111
                    ->cannotBeEmpty()
65 111
                ->end()
66 111
                ->scalarNode('method_inflector')
67 111
                    ->defaultValue(BusBuildersFromConfig::DEFAULT_METHOD_INFLECTOR)
68 111
                    ->cannotBeEmpty()
69 111
                ->end()
70 111
                ->arrayNode('security')
71 111
                    ->defaultValue([])
72 111
                    ->useAttributeAsKey('name')
73 111
                    ->prototype('array')
74 111
                        ->prototype('scalar')->end()
75 111
                    ->end()
76 111
                ->end()
77 111
                ->scalarNode('logger_formatter')
78 111
                    ->defaultValue('tactician.logger.class_properties_formatter')
79 111
                    ->cannotBeEmpty()
80 111
                ->end()
81 111
            ->end()
82 111
            ->validate()
83 111
                ->ifTrue(function ($config) {
84 102
                    return is_array($config) &&
85 102
                        array_key_exists('default_bus', $config) &&
86 102
                        array_key_exists('commandbus', $config)
87
                    ;
88 111
                })
89 111
                    ->then(function ($config) {
90 102
                        $busNames = [];
91 102
                        foreach ($config['commandbus'] as $busName => $busConfig) {
92 102
                            $busNames[] = $busName;
93
                        }
94
95 102
                        if (!in_array($config['default_bus'], $busNames)) {
96 6
                            throw new InvalidConfigurationException(
97 6
                                sprintf(
98 6
                                    'The default_bus "%s" was not defined as a command bus. Valid option(s): %s',
99 6
                                    $config['default_bus'],
100 6
                                    implode(', ', $busNames)
101
                                )
102
                            );
103
                        }
104
105 96
                        return $config;
106 111
                    })
107 111
            ->end()
108
        ;
109
110 111
        return $treeBuilder;
111
    }
112
}
113