Configuration   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 76
c 1
b 0
f 0
dl 0
loc 93
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 87 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
/**
11
 * @author Joris van de Sande <[email protected]>
12
 *
13
 * @final
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    private const ROOT_NAME = 'fh_multi_site';
18
19
    private const ALLOWED_RESOLVERS = ['hostname_identified', 'prefixed_path_identified', 'service'];
20
21
    public function getConfigTreeBuilder(): TreeBuilder
22
    {
23
        $treeBuilder = new TreeBuilder(self::ROOT_NAME);
24
        $rootNode = $treeBuilder->getRootNode();
25
26
        $rootNode
27
            ->children()
28
                ->scalarNode('repository')
29
                    ->isRequired()
30
                ->end()
31
                ->arrayNode('resolver')
32
                    ->canBeEnabled()
33
                    ->beforeNormalization()
34
                        ->ifTrue(static function (array $config) {
35
                            if (!$config['enabled']) {
36
                                return false;
37
                            }
38
39
                            return 'hostname_identified' === $config['type'] && !isset($config['host_mapping']);
40
                        })
41
                        ->thenInvalid('A "host_mapping" is required for the hostname_identified resolver')
42
                    ->end()
43
                    ->beforeNormalization()
44
                        ->ifTrue(static function (array $config) {
45
                            if (!$config['enabled']) {
46
                                return false;
47
                            }
48
49
                            return 'prefixed_path_identified' === $config['type'] && !isset($config['identifiers']);
50
                        })
51
                        ->thenInvalid('"identifiers" are required for the prefixed_path_identified resolver')
52
                    ->end()
53
                    ->beforeNormalization()
54
                        ->ifTrue(static function (array $config) {
55
                            if (!$config['enabled']) {
56
                                return false;
57
                            }
58
59
                            return 'service' === $config['type'] && !isset($config['service_id']);
60
                        })
61
                            ->thenInvalid('A "service_id" is required for the service resolver')
62
                    ->end()
63
                    ->children()
64
                        ->scalarNode('type')
65
                            ->cannotBeEmpty()
66
                            ->defaultValue('hostname_identified')
67
                            ->beforeNormalization()
68
                                ->ifNotInArray(self::ALLOWED_RESOLVERS)
69
                                    ->thenInvalid('Resolver type must be one of: '.implode(', ', self::ALLOWED_RESOLVERS))
70
                            ->end()
71
                        ->end()
72
                        ->scalarNode('service_id')->end()
73
                        ->arrayNode('host_mapping')
74
                            ->useAttributeAsKey('identifier')
75
                            ->prototype('array')
76
                                ->children()
77
                                    ->scalarNode('identifier')->cannotBeEmpty()->end()
78
                                    ->arrayNode('hostnames')
79
                                        ->requiresAtLeastOneElement()
80
                                        ->beforeNormalization()
81
                                            ->castToArray()
82
                                        ->end()
83
                                        ->prototype('scalar')
84
                                        ->end()
85
                                    ->end()
86
                                    ->arrayNode('locales')
87
                                        ->requiresAtLeastOneElement()
88
                                        ->beforeNormalization()
89
                                            ->castToArray()
90
                                        ->end()
91
                                        ->prototype('scalar')
92
                                        ->end()
93
                                    ->end()
94
                                ->end()
95
                            ->end()
96
                        ->end()
97
                        ->scalarNode('route_parameter')->defaultValue('_site')->cannotBeEmpty()->end()
98
                        ->scalarNode('default_identifier')->end()
99
                        ->arrayNode('identifiers')
100
                            ->prototype('scalar')->end()
101
                        ->end()
102
                    ->end()
103
104
                ->end()
105
            ->end();
106
107
        return $treeBuilder;
108
    }
109
}
110