Completed
Push — master ( 07e238...0f15f1 )
by Pavel
09:14
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

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