Completed
Pull Request — master (#15)
by Pavel
19:37
created

Configuration::configureCreateAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 23
cts 23
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\DependencyInjection;
4
5
use ScayTrase\Api\Cruds\EntityFactoryInterface;
6
use ScayTrase\Api\Cruds\EntityProcessorInterface;
7
use ScayTrase\Api\Cruds\PropertyAccessProcessor;
8
use ScayTrase\Api\Cruds\ReflectionConstructorFactory;
9
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
10
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
11
use Symfony\Component\Config\Definition\ConfigurationInterface;
12
13
final class Configuration implements ConfigurationInterface
14
{
15
    /** {@inheritdoc} */
16 21
    public function getConfigTreeBuilder()
17
    {
18 21
        $builder = new TreeBuilder();
19 21
        $root    = $builder->root('cruds');
20
21 21
        $entities = $root->children()->arrayNode('entities');
22 21
        $entities->useAttributeAsKey('name');
23 21
        $entities->normalizeKeys(false);
24
        /** @var ArrayNodeDefinition $entitiesProto */
25 21
        $entitiesProto = $entities->prototype('array');
26
27 21
        $events = $root->children()->arrayNode('listeners');
28 21
        $events->addDefaultsIfNotSet();
29
        $events
30 21
            ->children()
31 21
            ->booleanNode('param_converter')
32 21
            ->defaultTrue()
33 21
            ->info('Configure param converting for symfony request');
34
        $events
35 21
            ->children()
36 21
            ->booleanNode('response_normalizer')
37 21
            ->defaultTrue()
38 21
            ->info('Configure normalizer view event listener');
39
        $events
40 21
            ->children()
41 21
            ->booleanNode('response_serializer')
42 21
            ->defaultTrue()
43 21
            ->info('Configure serializer view event listener');
44
45 21
        $this->configureEntityProto($entitiesProto);
46
47 21
        return $builder;
48
    }
49
50 21
    private function configureEntityProto(ArrayNodeDefinition $parent)
51
    {
52 21
        $parent->canBeDisabled();
53
54
        $parent
55 21
            ->children()
56 21
            ->scalarNode('class')
57 21
            ->isRequired()
58 21
            ->info('Doctrine class')
59 21
            ->example('MyBundle:MyEntity');
60
61
        $parent
62 21
            ->children()
63 21
            ->scalarNode('prefix')
64 21
            ->defaultNull()
65 21
            ->info('Route prefix. Defaults to entity key if not set')
66 21
            ->example('/my-entity');
67
68
        $parent
69 21
            ->children()
70 21
            ->scalarNode('mount')
71 21
            ->defaultValue('api')
72 21
            ->cannotBeEmpty()
73 21
            ->info(
74
                'Route mount. You can create different entries '.
75
                'with different mounts. You can use this value when loading routes'
76 21
            )
77 21
            ->example('my-mount-name');
78
79
        $parent
80 21
            ->children()
81 21
            ->scalarNode('repository')
82 21
            ->defaultNull()
83 21
            ->info(
84 21
                'Object repository. service reference,'.PHP_EOL.
85
                'default to factory-acquired doctrine repository for class'
86 21
            )
87 21
            ->example('@my_entity.repository');
88
89
        $parent
90 21
            ->children()
91 21
            ->scalarNode('manager')
92 21
            ->defaultNull()
93 21
            ->info(
94 21
                'Object manager. service reference,'.PHP_EOL.
95
                'default to factory-acquired doctrine manager for class'
96 21
            )
97 21
            ->example('@my_entity.repository');
98
99
100
        $actions = $parent
101 21
            ->children()
102 21
            ->arrayNode('actions');
103
104
        $actions
105 21
            ->beforeNormalization()
106 21
            ->ifArray()
107 21
            ->then(
108 21
                function (array $v) {
109 21
                    if (array_keys($v) !== range(0, count($v) - 1)) {
110
                        return $v;
111
                    }
112
113 21
                    $result = [];
114 21
                    foreach ($v as $key) {
115 21
                        $result[$key] = ['enabled' => true];
116 21
                    }
117
118 21
                    return $result;
119
                }
120 21
            )
121 21
            ->end()
122 21
            ->info('Action configuration');
123
124 21
        $this->configureCreateAction($actions);
125 21
        $this->configureReadAction($actions);
126 21
        $this->configureUpdateAction($actions);
127 21
        $this->configureDeleteAction($actions);
128 21
        $this->configureSearchAction($actions);
129 21
    }
130
131 21
    private function configureCreateAction(ArrayNodeDefinition $parent)
132
    {
133
        $create = $parent
134 21
            ->children()
135 21
            ->arrayNode('create');
136
137
        $create
138 21
            ->children()
139 21
            ->variableNode('factory')
140 21
            ->defaultNull()
141 21
            ->example('@my_entity.factory')
142 21
            ->info(
143 21
                'Service ID implementing '.PHP_EOL.
144 21
                EntityFactoryInterface::class.PHP_EOL.
145 21
                'Defaults to '.ReflectionConstructorFactory::class
146 21
            );
147
148
        $create
149 21
            ->children()
150 21
            ->variableNode('processor')
151 21
            ->defaultNull()
152 21
            ->example('@my_entity.factory')
153 21
            ->info(
154 21
                'Service ID implementing '.PHP_EOL.
155 21
                EntityFactoryInterface::class.PHP_EOL.
156 21
                'Defaults to '.ReflectionConstructorFactory::class
157 21
            );
158
159 21
        $this->configureActionNode($create, 'create');
160 21
    }
161
162 21
    private function configureSearchAction(ArrayNodeDefinition $parent)
163
    {
164
        $search = $parent
165 21
            ->children()
166 21
            ->arrayNode('search');
167 21
        $this->configureActionNode($search, 'search');
168
169
        $search
170 21
            ->children()
171 21
            ->scalarNode('count_path')
172 21
            ->info('Path for count action (will be prefixed with entity prefix)')
173 21
            ->defaultValue('/count');
174
175 21
        $criteria = $search->children()->variableNode('criteria');
176 21
        $criteria->info(
177 21
            'Criteria modifiers. Array will be treated as nested criteria,'.PHP_EOL.
178
            'allowing configuring several modifiers by key:value'
179 21
        );
180 21
        $criteria->defaultValue('cruds.criteria.entity');
181 21
        $criteria->example('my.criteria.modifier');
182 21
        $criteria->cannotBeEmpty();
183 21
    }
184
185 21
    private function configureReadAction(ArrayNodeDefinition $parent)
186
    {
187
        $read = $parent
188 21
            ->children()
189 21
            ->arrayNode('read');
190 21
        $this->configureActionNode($read, 'get');
191 21
    }
192
193 21
    private function configureUpdateAction(ArrayNodeDefinition $parent)
194
    {
195
        $update = $parent
196 21
            ->children()
197 21
            ->arrayNode('update');
198
199
        $update
200 21
            ->children()
201 21
            ->variableNode('processor')
202 21
            ->defaultNull()
203 21
            ->example('@my_entity.processor')
204 21
            ->info(
205 21
                'Service ID implementing '.PHP_EOL.
206 21
                EntityProcessorInterface::class.PHP_EOL.
207 21
                'Defaults to '.PropertyAccessProcessor::class
208 21
            );
209
210 21
        $this->configureActionNode($update, 'update');
211 21
    }
212
213 21
    private function configureDeleteAction(ArrayNodeDefinition $parent)
214
    {
215
        $delete = $parent
216 21
            ->children()
217 21
            ->arrayNode('delete');
218 21
        $this->configureActionNode($delete, 'delete');
219 21
    }
220
221 21
    private function configureActionNode(ArrayNodeDefinition $parent, $action)
222
    {
223 21
        $parent->canBeEnabled();
224
225
        $parent
226 21
            ->children()
227 21
            ->scalarNode('path')
228 21
            ->info('Action path (will be prefixed with entity prefix)')
229 21
            ->defaultValue('/'.$action);
230 21
    }
231
}
232