Completed
Push — master ( 71f819...ff9a7d )
by
52:13 queued 12s
created

AbstractResourceConfiguration::createOptionsNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
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
48
        return $node;
49
    }
50
51
    /**
52
     * @param ArrayNodeDefinition $node
53
     * @param array               $defaults
54
     *
55
     * @return ArrayNodeDefinition
56
     */
57
    protected function addClassesSection(ArrayNodeDefinition $node, array $defaults = array())
58
    {
59
        $node
60
            ->children()
61
                ->arrayNode('classes')
62
                ->addDefaultsIfNotSet()
63
                ->children()
64
                    ->scalarNode('model')
65
                        ->cannotBeEmpty()
66
                        ->defaultValue(isset($defaults['model']) ? $defaults['model'] : null)
67
                    ->end()
68
69
                    ->scalarNode('factory')
70
                        ->cannotBeEmpty()
71
                        ->defaultValue(isset($defaults['factory']) ? $defaults['factory'] : 'DoS\ResourceBundle\Factory\Factory')
72
                    ->end()
73
74
                    ->scalarNode('controller')
75
                        ->defaultValue(isset($defaults['controller']) ? $defaults['controller'] : 'DoS\ResourceBundle\Controller\ResourceController')
76
                    ->end()
77
78
                    ->scalarNode('repository')
79
                        ->cannotBeEmpty()
80
                        ->defaultValue(isset($defaults['repository']) ? $defaults['repository'] : 'DoS\ResourceBundle\Doctrine\ORM\EntityRepository')
81
                    ->end()
82
83
                    ->append($this->createInterfaceNode(isset($defaults['interface']) ? $defaults['interface'] : null))
84
                    ->append($this->createFormsNode(isset($defaults['form']) ? $defaults['form'] : null))
85
                    ->end()
86
                ->end()
87
            ->end()
88
        ;
89
90
        return $node;
91
    }
92
93
    /**
94
     * @param string $default
95
     *
96
     * @return ScalarNodeDefinition
97
     */
98
    protected function createDriverNode($default = null)
99
    {
100
        $builder = new TreeBuilder();
101
        $node = $builder->root('driver', 'enum');
102
103
        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...
104
            $node->defaultValue($default);
105
        }
106
107
        $node
108
            ->values(array(
109
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
110
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
111
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM,
112
            ))
113
            ->cannotBeEmpty()
114
            ->info(sprintf(
115
                'Database driver (%s, %s or %s)',
116
                SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
117
                SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
118
                SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM
119
            ))
120
            ->end()
121
        ;
122
123
        return $node;
124
    }
125
126
    protected function createOptionsNode(ArrayNodeDefinition $node, $default = 'default')
0 ignored issues
show
Unused Code introduced by
The parameter $default is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        $node
129
            ->children()
130
                ->variableNode('options')->end()
131
            ->end()
132
        ;
133
    }
134
135
    /**
136
     * @param string $default
137
     *
138
     * @return ScalarNodeDefinition
139
     */
140
    protected function createInterfaceNode($default = null)
141
    {
142
        $builder = new TreeBuilder();
143
        $node = $builder->root('interface', 'scalar');
144
145
        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...
146
            $node->defaultValue($default);
147
        }
148
149
        $node
150
            ->cannotBeEmpty()
151
            ->info('Name of model interface')
152
            ->end()
153
        ;
154
155
        return $node;
156
    }
157
158
    /**
159
     * @param ArrayNodeDefinition $node
160
     * @param array               $validationGroups
161
     */
162
    protected function addValidationGroupsSection(ArrayNodeDefinition $node, array $validationGroups = array())
163
    {
164
        $child = $node
165
            ->children()
166
                ->arrayNode('validation_groups')
167
                ->addDefaultsIfNotSet()
168
                ->children()
169
        ;
170
171
        foreach ($validationGroups as $name => $groups) {
172
            $child
173
                ->arrayNode($name)
174
                ->prototype('scalar')->end()
175
                ->defaultValue($groups)
176
            ->end();
177
        }
178
179
        $child
180
            ->end()
181
            ->end()
182
            ->end()
183
        ;
184
    }
185
186
    /**
187
     * @param array|string $classes
188
     *
189
     * @return ArrayNodeDefinition
190
     */
191
    protected function createFormsNode($classes)
192
    {
193
        $builder = new TreeBuilder();
194
        $node = $builder->root('form');
195
196
        if (is_string($classes)) {
197
            $classes = array(self::DEFAULT_KEY => $classes);
198
        }
199
200
        if (!isset($classes['choice'])) {
201
            $classes['choice'] = 'DoS\ResourceBundle\Form\Type\ResourceChoiceType';
202
        }
203
204
        $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...
205
            ->defaultValue($classes)
206
                ->useAttributeAsKey('name')
207
                ->prototype('scalar')
208
            ->end()
209
            ->beforeNormalization()
210
            ->ifString()
211
                ->then(function ($v) {
212
                    return array(
213
                        AbstractResourceConfiguration::DEFAULT_KEY => $v,
214
                    );
215
                })
216
            ->end()
217
        ;
218
219
        return $node;
220
    }
221
222
    protected function setDefaults(ArrayNodeDefinition $node, array $configs = array())
223
    {
224
        $configs = array_replace_recursive(array(
225
            'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
226
            'resources' => array(),
227
        ), $configs);
228
229
        $node->append($this->createDriverNode($configs['driver']));
230
        $node->append($this->createResourcesSection($configs['resources']));
231
    }
232
}
233