Completed
Push — master ( 5a8de8...64e59d )
by
03:07
created

createTranslationFormsNode()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 30
Ratio 100 %

Importance

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