Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
18 | final class CrudsEntitiesConfigurator |
||
19 | { |
||
20 | /** @var ContainerBuilder */ |
||
21 | private $container; |
||
22 | |||
23 | /** |
||
24 | * CrudsEntitiesConfigurator constructor. |
||
25 | * |
||
26 | * @param ContainerBuilder $container |
||
27 | */ |
||
28 | 2 | public function __construct(ContainerBuilder $container) |
|
29 | { |
||
30 | 2 | $this->container = $container; |
|
31 | 2 | } |
|
32 | |||
33 | 2 | public function processEntityConfiguration($name, $config) |
|
34 | { |
||
35 | 2 | $class = $config['class']; |
|
36 | 2 | $actions = $config['actions']; |
|
37 | 2 | $prefix = $config['prefix']; |
|
38 | 2 | $repository = $config['repository']; |
|
39 | |||
40 | 2 | if (null === $repository) { |
|
41 | 2 | $repositoryDefinition = new Definition(EntityRepository::class); |
|
42 | 2 | $repositoryDefinition->setFactory([new Reference('doctrine.orm.entity_manager'), 'getRepository']); |
|
43 | 2 | $repositoryDefinition->setArguments([$class]); |
|
44 | 2 | } else { |
|
45 | $repositoryDefinition = new Reference($this->filterReference($repository)); |
||
46 | } |
||
47 | |||
48 | //todo: try to use any ObjectManager as a dependency |
||
49 | 2 | $manager = new Definition(ObjectManager::class); |
|
50 | 2 | $manager->setFactory([new Reference('doctrine'), 'getManagerForClass']); |
|
51 | 2 | $manager->setArguments([$class]); |
|
52 | |||
53 | 2 | foreach ($actions as $action => $actionConfig) { |
|
54 | 2 | $actionConfig['name'] = $name; |
|
55 | 2 | $actionConfig['class'] = $class; |
|
56 | 2 | $actionConfig['repository'] = $repositoryDefinition; |
|
57 | 2 | $actionConfig['path'] = $prefix . $actionConfig['path']; |
|
58 | 2 | $actionConfig['manager'] = $manager; |
|
59 | 2 | $function = new \ReflectionMethod($this, 'register' . ucfirst($action) . 'Action'); |
|
60 | 2 | $args = []; |
|
61 | |||
62 | 2 | foreach ($function->getParameters() as $parameter) { |
|
63 | 2 | if (array_key_exists($parameter->getName(), $actionConfig)) { |
|
1 ignored issue
–
show
|
|||
64 | 2 | $args[] = $actionConfig[$parameter->getName()]; |
|
1 ignored issue
–
show
|
|||
65 | 2 | } else { |
|
66 | $args[] = $parameter->getDefaultValue(); |
||
67 | } |
||
68 | 2 | } |
|
69 | 2 | $function->invokeArgs($this, $args); |
|
70 | 2 | } |
|
71 | 2 | } |
|
72 | |||
73 | 2 | public function registerCreateAction($name, $class, $factory, $processor, $path, $manager) |
|
74 | { |
||
75 | 2 | if (null === $factory) { |
|
76 | 2 | $factory = new DefinitionDecorator('cruds.factory.reflection'); |
|
77 | 2 | $factory->setArguments([$class, []]); |
|
78 | 2 | } else { |
|
79 | $factory = new Reference($this->filterReference($factory)); |
||
80 | } |
||
81 | |||
82 | 2 | View Code Duplication | if (null === $processor) { |
83 | 2 | $processor = new Reference('cruds.processor.property_access'); |
|
84 | 2 | } else { |
|
85 | $processor = new Reference($this->filterReference($processor)); |
||
86 | } |
||
87 | |||
88 | 2 | $definition = new Definition(CreateController::class); |
|
89 | 2 | $definition->setArguments( |
|
90 | [ |
||
91 | 2 | $processor, |
|
92 | 2 | $manager, |
|
93 | 2 | $factory, |
|
94 | 2 | new Reference('event_dispatcher'), |
|
95 | ] |
||
96 | 2 | ); |
|
97 | 2 | $definition->setPublic(true); |
|
98 | |||
99 | 2 | $controllerId = $this->normalize('cruds.api.generated.' . $name . '.create_controller'); |
|
100 | 2 | $this->container->setDefinition($controllerId, $definition); |
|
101 | |||
102 | 2 | $this->getLoaderDefinition()->addMethodCall( |
|
103 | 2 | 'addRoute', |
|
104 | [ |
||
105 | 2 | $this->normalize('cruds_api_' . $name . '_create'), |
|
106 | 2 | $path, |
|
107 | 2 | $controllerId . ':' . CreateController::ACTION, |
|
108 | 2 | ['POST'], |
|
109 | ] |
||
110 | 2 | ); |
|
111 | 2 | } |
|
112 | |||
113 | 2 | View Code Duplication | public function registerReadAction($name, $path, $repository) |
136 | |||
137 | 2 | public function registerUpdateAction($name, $path, $repository, $processor, $manager) |
|
138 | { |
||
139 | 2 | View Code Duplication | if (null === $processor) { |
140 | 2 | $processor = new Reference('cruds.processor.property_access'); |
|
141 | 2 | } else { |
|
142 | $processor = new Reference($this->filterReference($processor)); |
||
143 | } |
||
144 | |||
145 | 2 | $definition = new Definition(UpdateController::class); |
|
146 | 2 | $definition->setArguments( |
|
147 | [ |
||
148 | 2 | $repository, |
|
149 | 2 | $processor, |
|
150 | 2 | $manager, |
|
151 | 2 | new Reference('event_dispatcher'), |
|
152 | ] |
||
153 | 2 | ); |
|
154 | |||
155 | 2 | $controllerId = $this->normalize('cruds_api_' . $name . '_update_controller'); |
|
156 | 2 | $this->container->setDefinition($controllerId, $definition); |
|
157 | |||
158 | 2 | $this->getLoaderDefinition()->addMethodCall( |
|
159 | 2 | 'addRoute', |
|
160 | [ |
||
161 | 2 | $this->normalize('cruds_api_' . $name . '_update'), |
|
162 | 2 | $path, |
|
163 | 2 | $controllerId . ':' . UpdateController::ACTION, |
|
164 | 2 | ['POST'], |
|
165 | ] |
||
166 | 2 | ); |
|
167 | 2 | } |
|
168 | |||
169 | 2 | View Code Duplication | public function registerDeleteAction($name, $path, $repository, $manager) |
170 | { |
||
171 | 2 | $definition = new Definition(DeleteController::class); |
|
172 | 2 | $definition->setArguments( |
|
173 | [ |
||
174 | 2 | $repository, |
|
175 | 2 | $manager, |
|
176 | 2 | new Reference('event_dispatcher'), |
|
177 | ] |
||
178 | 2 | ); |
|
179 | |||
180 | 2 | $controllerId = $this->normalize('cruds_api_' . $name . '_delete_controller'); |
|
181 | 2 | $this->container->setDefinition($controllerId, $definition); |
|
182 | |||
183 | 2 | $this->getLoaderDefinition()->addMethodCall( |
|
184 | 2 | 'addRoute', |
|
185 | [ |
||
186 | 2 | $this->normalize('cruds_api_' . $name . '_delete'), |
|
187 | 2 | $path, |
|
188 | 2 | $controllerId . ':' . DeleteController::ACTION, |
|
189 | 2 | ['POST'], |
|
190 | ] |
||
191 | 2 | ); |
|
192 | 2 | } |
|
193 | |||
194 | 2 | public function registerSearchAction($name, $path, $class, $repository, array $criteria = []) |
|
195 | { |
||
196 | 2 | $filterArray = []; |
|
197 | 2 | foreach ($criteria as $filter => $reference) { |
|
198 | 2 | $filterArray[$filter] = new Reference($this->filterReference($reference)); |
|
199 | 2 | } |
|
200 | |||
201 | 2 | $definition = new Definition(SearchController::class); |
|
202 | 2 | $definition->setArguments( |
|
203 | [ |
||
204 | 2 | $class, |
|
205 | 2 | $repository, |
|
206 | 2 | $filterArray, |
|
207 | 2 | new Reference('event_dispatcher'), |
|
208 | ] |
||
209 | 2 | ); |
|
210 | |||
211 | 2 | $controllerId = $this->normalize('cruds_api_' . $name . '_search_controller'); |
|
212 | 2 | $this->container->setDefinition($controllerId, $definition); |
|
213 | |||
214 | 2 | $this->getLoaderDefinition()->addMethodCall( |
|
215 | 2 | 'addRoute', |
|
216 | [ |
||
217 | 2 | $this->normalize('cruds_api_' . $name . '_search'), |
|
218 | 2 | $path, |
|
219 | 2 | $controllerId . ':' . SearchController::ACTION, |
|
220 | 2 | ['GET', 'POST'], |
|
221 | ] |
||
222 | 2 | ); |
|
223 | 2 | } |
|
224 | |||
225 | 2 | private function getLoaderDefinition() |
|
226 | { |
||
227 | 2 | return $this->container->getDefinition('cruds.api.router_loader'); |
|
228 | } |
||
229 | |||
230 | 2 | private function normalize($name) |
|
234 | |||
235 | /** |
||
236 | * @param string $reference |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 2 | private function filterReference($reference) |
|
241 | { |
||
244 | } |
||
245 |