AbstractResourceConfiguration::addClassesSection()   C
last analyzed

Complexity

Conditions 8
Paths 1

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 2 Features 2
Metric Value
c 8
b 2
f 2
dl 0
loc 41
rs 5.3846
cc 8
eloc 32
nc 1
nop 2
1
<?php
2
3
namespace DoS\ResourceBundle\DependencyInjection;
4
5
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
abstract class AbstractResourceConfiguration implements ConfigurationInterface
12
{
13
    const DEFAULT_KEY = 'default';
14
15
    /**
16
     * @param array $resources
17
     *
18
     * @return ArrayNodeDefinition
19
     */
20
    protected function createResourcesSection(array $resources = array())
21
    {
22
        $builder = new TreeBuilder();
23
        $node = $builder->root('resources');
24
        $node->addDefaultsIfNotSet();
25
        $resourceNodes = $node->children();
26
27
        foreach ($resources as $resource => $defaults) {
28
            $resourceNode = $resourceNodes
29
                ->arrayNode($resource)
30
                ->addDefaultsIfNotSet()
31
            ;
32
33
            $this->addClassesSection($resourceNode, $defaults['classes']);
34
35
            if (isset($defaults['options'])) {
36
                $this->createOptionsNode($resourceNode, $defaults['options']);
37
            }
38
39
            if (!isset($defaults['validation_groups'])) {
40
                $defaults['validation_groups'] = array(
41
                    'default' => array()
42
                );
43
            }
44
45
            $this->addValidationGroupsSection($resourceNode, $defaults['validation_groups']);
46
47
            if (isset($defaults['translation'])) {
48
                $this->createTranslationNode($resourceNode, $defaults['translation']);
49
            }
50
        }
51
52
        return $node;
53
    }
54
55
56
    /**
57
     * @param ArrayNodeDefinition $node
58
     * @param array               $defaults
59
     *
60
     * @return ArrayNodeDefinition
61
     */
62
    protected function addClassesSection(ArrayNodeDefinition $node, array $defaults = array())
63
    {
64
        $node
65
            ->children()
66
                ->arrayNode('classes')
67
                ->addDefaultsIfNotSet()
68
                ->children()
69
                    ->scalarNode('model')
70
                        ->cannotBeEmpty()
71
                        ->defaultValue(isset($defaults['model']) ? $defaults['model'] : null)
72
                    ->end()
73
74
                    ->scalarNode('factory')
75
                        ->cannotBeEmpty()
76
                        ->defaultValue(isset($defaults['factory']) ? $defaults['factory'] : 'DoS\ResourceBundle\Factory\Factory')
77
                    ->end()
78
79
                    ->scalarNode('controller')
80
                        ->defaultValue(isset($defaults['controller']) ? $defaults['controller'] : 'DoS\ResourceBundle\Controller\ResourceController')
81
                    ->end()
82
83
                    ->scalarNode('repository')
84
                        ->cannotBeEmpty()
85
                        ->defaultValue(isset($defaults['repository']) ? $defaults['repository'] : 'DoS\ResourceBundle\Doctrine\ORM\EntityRepository')
86
                    ->end()
87
88
                    ->scalarNode('interface')
89
                        ->cannotBeEmpty()
90
                        ->isRequired()
91
                        ->defaultValue(isset($defaults['interface']) ? $defaults['interface'] : null)
92
                    ->end()
93
94
                    ->append($this->createProviderNode(isset($defaults['provider']) ? $defaults['provider'] : null))
95
                    ->append($this->createFormsNode(isset($defaults['form']) ? $defaults['form'] : null))
96
                    ->end()
97
                ->end()
98
            ->end()
99
        ;
100
101
        return $node;
102
    }
103
104
    /**
105
     * @param string $default
106
     *
107
     * @return ScalarNodeDefinition
108
     */
109
    protected function createDriverNode($default = null)
110
    {
111
        $builder = new TreeBuilder();
112
        $node = $builder->root('driver', 'enum');
113
114
        if ($default) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $default of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
115
            $node->defaultValue($default);
116
        }
117
118
        $node
119
            ->values(array(
120
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
121
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
122
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM,
123
            ))
124
            ->cannotBeEmpty()
125
            ->info(sprintf(
126
                'Database driver (%s, %s or %s)',
127
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
128
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
129
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM
130
            ))
131
            ->end()
132
        ;
133
134
        return $node;
135
    }
136
137
    protected function createOptionsNode(ArrayNodeDefinition $node, $default = 'default')
138
    {
139
        $node
140
            ->children()
141
                ->variableNode('options')->defaultValue($default)->end()
142
            ->end()
143
        ;
144
    }
145
146
    protected function createTranslationNode(ArrayNodeDefinition $node, array $defaults)
147
    {
148
        $translationNode = $node
149
            ->children()
150
                ->arrayNode('translation')
151
                    ->addDefaultsIfNotSet()
152
        ;
153
154
        if (isset($defaults['options'])) {
155
            $translationNode
0 ignored issues
show
Bug introduced by
The method variableNode() does not seem to exist on object<Symfony\Component...er\ArrayNodeDefinition>.

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...
156
                ->variableNode('options')
157
                ->defaultValue($defaults['options'])
158
                ->end()
159
            ;
160
        }
161
162
        if (!isset($defaults['validation_groups'])) {
163
            $defaults['validation_groups'] = array(
164
                'default' => array()
165
            );
166
        }
167
168
        $this->addValidationGroupsSection($translationNode, $defaults['validation_groups']);
169
        $this->addClassesSection($translationNode, $defaults['classes']);
170
171
        $translationNode
172
            ->end()
173
            ->end()
174
        ;
175
    }
176
177
    /**
178
     * @param string $default
179
     *
180
     * @return ScalarNodeDefinition
181
     */
182
    protected function createProviderNode($default = null)
183
    {
184
        $builder = new TreeBuilder();
185
        $node = $builder->root('provider', 'scalar');
186
187
        if ($default) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $default of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
188
            $node->defaultValue($default);
189
        }
190
191
        $node
192
            ->cannotBeEmpty()
193
            ->info('Name of model provider')
194
            ->end()
195
        ;
196
197
        return $node;
198
    }
199
200
    /**
201
     * @param ArrayNodeDefinition $node
202
     * @param array               $validationGroups
203
     */
204
    protected function addValidationGroupsSection(ArrayNodeDefinition $node, array $validationGroups = array())
205
    {
206
        $child = $node
207
            ->children()
208
                ->arrayNode('validation_groups')
209
                ->addDefaultsIfNotSet()
210
                ->children()
211
        ;
212
213
        foreach ($validationGroups as $name => $groups) {
214
            $child
215
                ->arrayNode($name)
216
                ->prototype('scalar')->end()
217
                ->defaultValue($groups)
218
            ->end();
219
        }
220
221
        $child
222
            ->end()
223
            ->end()
224
            ->end()
225
        ;
226
    }
227
228
    /**
229
     * @param array|string $classes
230
     *
231
     * @return ArrayNodeDefinition
232
     */
233 View Code Duplication
    protected function createFormsNode($classes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        $builder = new TreeBuilder();
236
        $node = $builder->root('form');
237
238
        if (is_string($classes)) {
239
            $classes = array(self::DEFAULT_KEY => $classes);
240
        }
241
242
        if (!isset($classes['choice'])) {
243
            $classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType';
244
        }
245
246
        $node
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
247
            ->defaultValue($classes)
248
                ->useAttributeAsKey('name')
249
                ->prototype('scalar')
250
            ->end()
251
            ->beforeNormalization()
252
            ->ifString()
253
                ->then(function ($v) {
254
                    return array(
255
                        AbstractResourceConfiguration::DEFAULT_KEY => $v,
256
                    );
257
                })
258
            ->end()
259
        ;
260
261
        return $node;
262
    }
263
264
    /**
265
     * @param array|string $classes
266
     *
267
     * @return ArrayNodeDefinition
268
     */
269 View Code Duplication
    protected function createTranslationFormsNode($classes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271
        $builder = new TreeBuilder();
272
        $node = $builder->root('form');
273
274
        if (is_string($classes)) {
275
            $classes = array(self::DEFAULT_KEY => $classes);
276
        }
277
278
        if (!isset($classes['choice'])) {
279
            $classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType';
280
        }
281
282
        $node
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
283
            ->defaultValue($classes)
284
                ->useAttributeAsKey('name')
285
                ->prototype('scalar')
286
            ->end()
287
            ->beforeNormalization()
288
            ->ifString()
289
                ->then(function ($v) {
290
                    return array(
291
                        AbstractResourceConfiguration::DEFAULT_KEY => $v,
292
                    );
293
                })
294
            ->end()
295
        ;
296
297
        return $node;
298
    }
299
300
    protected function setDefaults(ArrayNodeDefinition $node, array $configs = array())
301
    {
302
        $configs = array_replace_recursive(array(
303
            'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
304
            'resources' => array(),
305
        ), $configs);
306
307
        $node->append($this->createDriverNode($configs['driver']));
308
        $node->append($this->createResourcesSection($configs['resources']));
309
    }
310
}
311