Passed
Push — master ( 61ebc4...c33ad8 )
by Pavel
17:39
created

processEntityConfiguration()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.439
cc 5
eloc 23
nc 8
nop 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\DependencyInjection;
4
5
use Doctrine\ORM\EntityRepository;
6
use ScayTrase\Api\Cruds\Controller\CreateController;
7
use ScayTrase\Api\Cruds\Controller\DeleteController;
8
use ScayTrase\Api\Cruds\Controller\ReadController;
9
use ScayTrase\Api\Cruds\Controller\SearchController;
10
use ScayTrase\Api\Cruds\Controller\UpdateController;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Reference;
14
15
final class CrudsEntitiesConfigurator
16
{
17
    /** @var  ContainerBuilder */
18
    private $container;
19
20
    /**
21
     * CrudsEntitiesConfigurator constructor.
22
     *
23
     * @param ContainerBuilder $container
24
     */
25
    public function __construct(ContainerBuilder $container)
26
    {
27
        $this->container = $container;
28
    }
29
30
    public function processEntityConfiguration($name, $config)
31
    {
32
        $class      = $config['class'];
33
        $actions    = $config['actions'];
34
        $prefix     = $config['prefix'];
35
        $repository = $config['repository'];
36
37
        if (null === $repository) {
38
            $repositoryDefinition = new Definition(EntityRepository::class);
39
            $repositoryDefinition->setFactory([new Reference('doctrine.orm.entity_manager'), 'getRepository']);
40
            $repositoryDefinition->setArguments([$class]);
41
        } else {
42
            $repositoryDefinition = new Reference($this->filterReference($repository));
43
        }
44
45
        foreach ($actions as $action => $actionConfig) {
46
            $actionConfig['name']       = $name;
47
            $actionConfig['repository'] = $repositoryDefinition;
48
            $actionConfig['path']       = $prefix.$actionConfig['path'];
49
            $function                   = new \ReflectionMethod($this, 'register'.ucfirst($action).'Action');
50
            $args                       = [];
51
52
            foreach ($function->getParameters() as $parameter) {
53
                if (array_key_exists($parameter->getName(), $actionConfig)) {
1 ignored issue
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
54
                    $args[] = $actionConfig[$parameter->getName()];
1 ignored issue
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
                } else {
56
                    $args[] = $parameter->getDefaultValue();
57
                }
58
            }
59
            $function->invokeArgs($this, $args);
60
        }
61
    }
62
63 View Code Duplication
    public function registerCreateAction($name, $repository, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $definition = new Definition(CreateController::class);
66
        $definition->setArguments(
67
            [
68
                $repository,
69
                new Reference('event_dispatcher'),
70
            ]
71
        );
72
73
        $controllerId = $this->normalize('cruds_api_'.$name.'_create_controller');
74
        $this->container->setDefinition($controllerId, $definition);
75
76
        $this->getLoaderDefinition()->addMethodCall(
77
            'addRoute',
78
            [
79
                $this->normalize('cruds_api_'.$name.'_create'),
80
                $path,
81
                '@'.$controllerId.':'.CreateController::ACTION,
82
                ['POST'],
83
            ]
84
        );
85
    }
86
87 View Code Duplication
    public function registerReadAction($name, $path, $repository)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $definition = new Definition(ReadController::class);
90
        $definition->setArguments(
91
            [
92
                $repository,
93
                new Reference('event_dispatcher'),
94
            ]
95
        );
96
97
        $controllerId = $this->normalize('cruds_api_'.$name.'_read_controller');
98
        $this->container->setDefinition($controllerId, $definition);
99
100
        $this->getLoaderDefinition()->addMethodCall(
101
            'addRoute',
102
            [
103
                $this->normalize('cruds_api_'.$name.'_read'),
104
                $path,
105
                '@'.$controllerId.':'.ReadController::ACTION,
106
                ['GET'],
107
            ]
108
        );
109
    }
110
111 View Code Duplication
    public function registerUpdateAction($name, $path, $repository)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $definition = new Definition(UpdateController::class);
114
        $definition->setArguments(
115
            [
116
                $repository,
117
                new Reference('event_dispatcher'),
118
            ]
119
        );
120
121
        $controllerId = $this->normalize('cruds_api_'.$name.'_update_controller');
122
        $this->container->setDefinition($controllerId, $definition);
123
124
        $this->getLoaderDefinition()->addMethodCall(
125
            'addRoute',
126
            [
127
                $this->normalize('cruds_api_'.$name.'_update'),
128
                $path,
129
                '@'.$controllerId.':'.UpdateController::ACTION,
130
                ['POST'],
131
            ]
132
        );
133
    }
134
135 View Code Duplication
    public function registerDeleteAction($name, $path, $repository)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $definition = new Definition(DeleteController::class);
138
        $definition->setArguments(
139
            [
140
                $repository,
141
                new Reference('event_dispatcher'),
142
            ]
143
        );
144
145
        $controllerId = $this->normalize('cruds_api_'.$name.'_delete_controller');
146
        $this->container->setDefinition($controllerId, $definition);
147
148
        $this->getLoaderDefinition()->addMethodCall(
149
            'addRoute',
150
            [
151
                $this->normalize('cruds_api_'.$name.'_delete'),
152
                $path,
153
                '@'.$controllerId.':'.DeleteController::ACTION,
154
                ['POST'],
155
            ]
156
        );
157
    }
158
159
    public function registerSearchAction($name, $path, $repository, array $filters = [])
160
    {
161
        $filterArray = [];
162
        foreach ($filters as $reference) {
163
            $filterArray[] = new Reference($this->filterReference($reference));
164
        }
165
166
        $definition = new Definition(SearchController::class);
167
        $definition->setArguments(
168
            [
169
                $repository,
170
                $filterArray,
171
                new Reference('event_dispatcher'),
172
            ]
173
        );
174
175
        $controllerId = $this->normalize('cruds_api_'.$name.'_search_controller');
176
        $this->container->setDefinition($controllerId, $definition);
177
178
        $this->getLoaderDefinition()->addMethodCall(
179
            'addRoute',
180
            [
181
                $this->normalize('cruds_api_'.$name.'_search'),
182
                $path,
183
                '@'.$controllerId.':'.SearchController::ACTION,
184
                ['GET', 'POST'],
185
            ]
186
        );
187
    }
188
189
    private function getLoaderDefinition()
190
    {
191
        return $this->container->getDefinition('cruds.api.router_loader');
192
    }
193
194
    private function normalize($name)
195
    {
196
        return str_replace('-', '_', $name);
197
    }
198
199
    /**
200
     * @param string $reference
201
     *
202
     * @return string
203
     */
204
    private function filterReference($reference)
205
    {
206
        return ltrim($reference, '@');
207
    }
208
}
209