Completed
Push — master ( f53209...60476a )
by
03:09
created

createInterfaceNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

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