Passed
Pull Request — master (#175)
by Arnaud
03:44
created

Configuration::createRootNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace LAG\AdminBundle\DependencyInjection;
4
5
use LAG\AdminBundle\Controller\AdminAction;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    /**
14
     * @return TreeBuilder
15
     */
16 8
    public function getConfigTreeBuilder()
17
    {
18 8
        $treeBuilder = new TreeBuilder('lag_admin');
19 8
20
        if (method_exists(TreeBuilder::class, 'getRootNode')) {
21
            $rootNode = $treeBuilder->getRootNode();
22 8
        } else {
23
            $rootNode = $treeBuilder->root('lag_admin');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

23
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('lag_admin');

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

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

Loading history...
24 8
        }
25
26 8
        $rootNode
27
            ->children()
28 8
                // application configuration
29 8
                ->append($this->getApplicationNode())
30 8
                // admins configuration
31
                ->append($this->getAdminsConfigurationNode())
32 8
                // menus configurations
33
                ->append($this->getMenuConfiguration())
34
            ->end()
35
        ->end();
36
37
        return $treeBuilder;
38 8
    }
39
40 8
    /**
41 8
     * This method preserves the compatibility with Symfony 3.4.
42
     */
43
    protected function createRootNode(string $name): NodeDefinition
44
    {
45
        if (method_exists(TreeBuilder::class, 'getRootNode')) {
46 8
            $treeBuilder = new TreeBuilder($name);
47 8
48 8
            return $treeBuilder->getRootNode();
49 8
        } else {
50 8
            $treeBuilder = new TreeBuilder();
51 8
52 8
            return $treeBuilder->root($name);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

52
            return /** @scrutinizer ignore-deprecated */ $treeBuilder->root($name);

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

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

Loading history...
53 8
        }
54
    }
55 8
56 8
    /**
57 8
     * @return ArrayNodeDefinition|NodeDefinition
58
     */
59 8
    protected function getAdminsConfigurationNode()
60
    {
61
        $node = $this->createRootNode('admins');
62
63
        $node
64
            // useAttributeAsKey() method will preserve keys when multiple configurations files are used and then avoid
65 8
            // admin not found by configuration override
66
            ->useAttributeAsKey('name')
67 8
            ->prototype('array')
68 8
                ->children()
69
                    ->scalarNode('entity')->end()
70
                    ->scalarNode('data_provider')->end()
71 8
                    ->scalarNode('form')->end()
72 8
                    ->scalarNode('max_per_page')->end()
73 8
                    ->scalarNode('controller')->defaultValue(AdminAction::class)->end()
74 8
                    // actions configurations
75 8
                    ->variableNode('actions')->end()
76 8
                ->end()
77 8
            ->end();
78 8
79 8
        return $node;
80 8
    }
81 8
82 8
    /**
83 8
     * @return ArrayNodeDefinition|NodeDefinition
84 8
     */
85 8
    protected function getApplicationNode()
86 8
    {
87 8
        $node = $this->createRootNode('application');
88 8
89 8
        $node
90 8
            ->children()
91 8
                ->scalarNode('title')->end()
92 8
                ->scalarNode('description')->end()
93 8
                ->scalarNode('resources_path')->defaultValue('%kernel.project_dir%/config/admin/resources')->end()
94 8
                ->scalarNode('base_template')->end()
95 8
                ->scalarNode('menu_template')->end()
96 8
                ->scalarNode('list_template')->end()
97 8
                ->scalarNode('block_template')
98 8
                    ->defaultValue('LAGAdminBundle:Form:fields.html.twig')
99 8
                ->end()
100 8
                ->scalarNode('date_format')->defaultValue('Y-m-d')->end()
101 8
                ->scalarNode('bootstrap')->end()
102 8
                ->scalarNode('max_per_page')->end()
103 8
                ->scalarNode('routing_name_pattern')->end()
104 8
                ->scalarNode('routing_url_pattern')->end()
105 8
                ->arrayNode('translation')
106 8
                    ->children()
107 8
                        ->booleanNode('enabled')->defaultTrue()->end()
108 8
                        ->scalarNode('pattern')->defaultValue('admin.{admin}.{key}')->end()
109 8
                        ->scalarNode('catalog')->defaultValue('messages')->end()
110 8
                    ->end()
111 8
                ->end()
112 8
                ->arrayNode('fields_mapping')
113 8
                    ->prototype('scalar')
114 8
                    ->end()
115 8
                ->end()
116 8
                ->booleanNode('enable_extra_configuration')->defaultTrue()->end()
117
                ->booleanNode('enable_security')->end()
118 8
                ->booleanNode('enable_menus')->defaultTrue()->end()
119
                ->booleanNode('enable_homepage')->end()
120
                ->scalarNode('locale')->end()
121
                ->scalarNode('homepage_template')->end()
122
                ->scalarNode('homepage_route')->end()
123
                ->scalarNode('routing_url_pattern')->end()
124 8
                ->scalarNode('routing_name_pattern')->end()
125
                ->scalarNode('bootstrap')->end()
126 8
                ->scalarNode('pager')->end()
127 8
                ->scalarNode('string_length')->end()
128
                ->scalarNode('string_length_truncate')->end()
129
                ->scalarNode('max_per_page')->end()
130 8
                ->scalarNode('admin_class')->end()
131 8
                ->scalarNode('action_class')->end()
132 8
                ->scalarNode('permissions')->end()
133
                ->scalarNode('page_parameter')->end()
134 8
            ->end()
135
        ->end();
136
137
        return $node;
138
    }
139
140
    /**
141
     * @return ArrayNodeDefinition|NodeDefinition
142
     */
143
    protected function getMenuConfiguration()
144
    {
145
        $node = $this->createRootNode('menus');
146
147
        $node
148
            ->prototype('variable')
149
            ->end()
150
        ->end();
151
152
        return $node;
153
    }
154
}
155