Completed
Push — master ( c00bbf...06e52a )
by Pavel
17:08 queued 22s
created

Configuration::configureSearchAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

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