Completed
Push — master ( 0f15f1...06dce9 )
by Pavel
09:44
created

Configuration::configureReadAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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