Completed
Push — master ( 66b974...89c512 )
by Ross
13s queued 10s
created

src/DependencyInjection/Configuration.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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