Completed
Push — master ( 28dfad...f53209 )
by
03:00
created

createProviderNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
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
                    ->append($this->createProviderNode(isset($defaults['provider']) ? $defaults['provider'] : null))
89
                    ->append($this->createInterfaceNode(isset($defaults['interface']) ? $defaults['interface'] : null))
90
                    ->append($this->createFormsNode(isset($defaults['form']) ? $defaults['form'] : null))
91
                    ->end()
92
                ->end()
93
            ->end()
94
        ;
95
96
        return $node;
97
    }
98
99
    /**
100
     * @param string $default
101
     *
102
     * @return ScalarNodeDefinition
103
     */
104
    protected function createDriverNode($default = null)
105
    {
106
        $builder = new TreeBuilder();
107
        $node = $builder->root('driver', 'enum');
108
109
        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...
110
            $node->defaultValue($default);
111
        }
112
113
        $node
114
            ->values(array(
115
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
116
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
117
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM,
118
            ))
119
            ->cannotBeEmpty()
120
            ->info(sprintf(
121
                'Database driver (%s, %s or %s)',
122
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
123
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
124
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM
125
            ))
126
            ->end()
127
        ;
128
129
        return $node;
130
    }
131
132
    protected function createOptionsNode(ArrayNodeDefinition $node, $default = 'default')
133
    {
134
        $node
135
            ->children()
136
                ->variableNode('options')->defaultValue($default)->end()
137
            ->end()
138
        ;
139
    }
140
141
    protected function createTranslationNode(ArrayNodeDefinition $node, array $defaults)
142
    {
143
        $translationNode = $node
144
            ->children()
145
                ->arrayNode('translation')
146
                    ->addDefaultsIfNotSet()
147
        ;
148
149
        if ($defaults['options']) {
150
            $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...
151
                ->variableNode('options')
152
                ->defaultValue($defaults['options'])
153
                ->end()
154
            ;
155
        }
156
157
        if (!isset($defaults['validation_groups'])) {
158
            $defaults['validation_groups'] = array(
159
                'default' => array()
160
            );
161
        }
162
163
        $this->addValidationGroupsSection($translationNode, $defaults['validation_groups']);
164
        $this->addClassesSection($translationNode, $defaults['classes']);
165
166
        $translationNode
167
            ->end()
168
            ->end()
169
        ;
170
    }
171
172
    /**
173
     * @param string $default
174
     *
175
     * @return ScalarNodeDefinition
176
     */
177 View Code Duplication
    protected function createInterfaceNode($default = null)
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...
178
    {
179
        $builder = new TreeBuilder();
180
        $node = $builder->root('interface', 'scalar');
181
182
        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...
183
            $node->defaultValue($default);
184
        }
185
186
        $node
187
            ->cannotBeEmpty()
188
            ->info('Name of model interface')
189
            ->end()
190
        ;
191
192
        return $node;
193
    }
194
195
    /**
196
     * @param string $default
197
     *
198
     * @return ScalarNodeDefinition
199
     */
200 View Code Duplication
    protected function createProviderNode($default = null)
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...
201
    {
202
        $builder = new TreeBuilder();
203
        $node = $builder->root('provider', 'scalar');
204
205
        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...
206
            $node->defaultValue($default);
207
        }
208
209
        $node
210
            ->cannotBeEmpty()
211
            ->info('Name of model provider')
212
            ->end()
213
        ;
214
215
        return $node;
216
    }
217
218
    /**
219
     * @param ArrayNodeDefinition $node
220
     * @param array               $validationGroups
221
     */
222
    protected function addValidationGroupsSection(ArrayNodeDefinition $node, array $validationGroups = array())
223
    {
224
        $child = $node
225
            ->children()
226
                ->arrayNode('validation_groups')
227
                ->addDefaultsIfNotSet()
228
                ->children()
229
        ;
230
231
        foreach ($validationGroups as $name => $groups) {
232
            $child
233
                ->arrayNode($name)
234
                ->prototype('scalar')->end()
235
                ->defaultValue($groups)
236
            ->end();
237
        }
238
239
        $child
240
            ->end()
241
            ->end()
242
            ->end()
243
        ;
244
    }
245
246
    /**
247
     * @param array|string $classes
248
     *
249
     * @return ArrayNodeDefinition
250
     */
251 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...
252
    {
253
        $builder = new TreeBuilder();
254
        $node = $builder->root('form');
255
256
        if (is_string($classes)) {
257
            $classes = array(self::DEFAULT_KEY => $classes);
258
        }
259
260
        if (!isset($classes['choice'])) {
261
            $classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType';
262
        }
263
264
        $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...
265
            ->defaultValue($classes)
266
                ->useAttributeAsKey('name')
267
                ->prototype('scalar')
268
            ->end()
269
            ->beforeNormalization()
270
            ->ifString()
271
                ->then(function ($v) {
272
                    return array(
273
                        AbstractResourceConfiguration::DEFAULT_KEY => $v,
274
                    );
275
                })
276
            ->end()
277
        ;
278
279
        return $node;
280
    }
281
282
    /**
283
     * @param array|string $classes
284
     *
285
     * @return ArrayNodeDefinition
286
     */
287 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...
288
    {
289
        $builder = new TreeBuilder();
290
        $node = $builder->root('form');
291
292
        if (is_string($classes)) {
293
            $classes = array(self::DEFAULT_KEY => $classes);
294
        }
295
296
        if (!isset($classes['choice'])) {
297
            $classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType';
298
        }
299
300
        $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...
301
            ->defaultValue($classes)
302
                ->useAttributeAsKey('name')
303
                ->prototype('scalar')
304
            ->end()
305
            ->beforeNormalization()
306
            ->ifString()
307
                ->then(function ($v) {
308
                    return array(
309
                        AbstractResourceConfiguration::DEFAULT_KEY => $v,
310
                    );
311
                })
312
            ->end()
313
        ;
314
315
        return $node;
316
    }
317
318
    protected function setDefaults(ArrayNodeDefinition $node, array $configs = array())
319
    {
320
        $configs = array_replace_recursive(array(
321
            'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
322
            'resources' => array(),
323
        ), $configs);
324
325
        $node->append($this->createDriverNode($configs['driver']));
326
        $node->append($this->createResourcesSection($configs['resources']));
327
    }
328
}
329