Completed
Pull Request — master (#175)
by Arnaud
06:52
created

Configuration::getApplicationNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 49
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 53
ccs 36
cts 36
cp 1
crap 1
rs 9.1127

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 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
     * @param string $name
44
     *
45
     * @return NodeDefinition
46 8
     */
47 8
    protected function createRootNode(string $name): NodeDefinition
48 8
    {
49 8
        if (method_exists(TreeBuilder::class, 'getRootNode')) {
50 8
            $treeBuilder = new TreeBuilder($name);
51 8
52 8
            return $treeBuilder->getRootNode();
53 8
        } else {
54
            $treeBuilder = new TreeBuilder();
55 8
56 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

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