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:
Complex classes like FOSElasticaExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FOSElasticaExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class FOSElasticaExtension extends Extension |
||
27 | { |
||
28 | /** |
||
29 | * Definition of elastica clients as configured by this extension. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $clients = []; |
||
34 | |||
35 | /** |
||
36 | * An array of indexes as configured by the extension. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $indexConfigs = []; |
||
41 | |||
42 | /** |
||
43 | * An array of index templates as configured by the extension. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $indexTemplateConfigs = []; |
||
48 | |||
49 | /** |
||
50 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
51 | * here. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $loadedDrivers = []; |
||
56 | |||
57 | 21 | public function load(array $configs, ContainerBuilder $container) |
|
58 | { |
||
59 | 21 | $configuration = $this->getConfiguration($configs, $container); |
|
60 | 21 | $config = $this->processConfiguration($configuration, $configs); |
|
61 | |||
62 | 21 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
63 | |||
64 | 21 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
65 | // No Clients or indexes are defined |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | 21 | foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener', 'commands'] as $basename) { |
|
70 | 21 | $loader->load(sprintf('%s.xml', $basename)); |
|
71 | } |
||
72 | |||
73 | 21 | if (empty($config['default_client'])) { |
|
74 | 21 | $keys = array_keys($config['clients']); |
|
75 | 21 | $config['default_client'] = reset($keys); |
|
76 | } |
||
77 | |||
78 | 21 | if (empty($config['default_index'])) { |
|
79 | 21 | $keys = array_keys($config['indexes']); |
|
80 | 21 | $config['default_index'] = reset($keys); |
|
81 | } |
||
82 | |||
83 | 21 | if (isset($config['serializer'])) { |
|
84 | 1 | $loader->load('serializer.xml'); |
|
85 | |||
86 | 1 | $this->loadSerializer($config['serializer'], $container); |
|
87 | } |
||
88 | |||
89 | 21 | $this->loadClients($config['clients'], $container); |
|
90 | 21 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
91 | 21 | $container->getAlias('fos_elastica.client')->setPublic(true); |
|
92 | 21 | $container->setAlias(ElasticaClient::class, new Alias('fos_elastica.client', false)); |
|
93 | 21 | $container->setAlias(Client::class, 'fos_elastica.client'); |
|
94 | 21 | $container->getAlias(Client::class)->setPublic(false); |
|
95 | |||
96 | 21 | $this->loadIndexes($config['indexes'], $container); |
|
97 | 21 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
98 | 21 | $container->getAlias('fos_elastica.index')->setPublic(true); |
|
99 | 21 | $container->setParameter('fos_elastica.default_index', $config['default_index']); |
|
100 | |||
101 | 21 | if ($usedIndexNames = \array_intersect_key($config['indexes'], $config['index_templates'])) { |
|
102 | throw new \DomainException(\sprintf('Index names "%s" are already in use and can not be used for index templates names', \implode('","', \array_keys($usedIndexNames)))); |
||
103 | } |
||
104 | 21 | $this->loadIndexTemplates($config['index_templates'], $container); |
|
105 | |||
106 | 21 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
107 | $container |
||
108 | 21 | ->getDefinition('fos_elastica.config_source.template_container') |
|
109 | 21 | ->replaceArgument(0, $this->indexTemplateConfigs); |
|
110 | |||
111 | 21 | $this->loadIndexManager($container); |
|
112 | 21 | $this->loadIndexTemplateManager($container); |
|
113 | |||
114 | 21 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
115 | 21 | } |
|
116 | |||
117 | /** |
||
118 | * @return Configuration |
||
119 | */ |
||
120 | 21 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
121 | { |
||
122 | 21 | return new Configuration($container->getParameter('kernel.debug')); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Loads the configured clients. |
||
127 | * |
||
128 | * @param array $clients An array of clients configurations |
||
129 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | 21 | private function loadClients(array $clients, ContainerBuilder $container) |
|
134 | { |
||
135 | 21 | foreach ($clients as $name => $clientConfig) { |
|
136 | 21 | $clientId = sprintf('fos_elastica.client.%s', $name); |
|
137 | |||
138 | 21 | $clientDef = new ChildDefinition('fos_elastica.client_prototype'); |
|
139 | 21 | $clientDef->replaceArgument(0, $clientConfig); |
|
140 | 21 | $clientDef->replaceArgument(1, null); |
|
141 | |||
142 | 21 | $logger = $clientConfig['connections'][0]['logger']; |
|
143 | 21 | if (false !== $logger) { |
|
144 | 21 | $clientDef->addMethodCall('setLogger', [new Reference($logger)]); |
|
145 | } |
||
146 | |||
147 | 21 | $clientDef->addTag('fos_elastica.client'); |
|
148 | |||
149 | 21 | $container->setDefinition($clientId, $clientDef); |
|
150 | |||
151 | 21 | $this->clients[$name] = [ |
|
152 | 21 | 'id' => $clientId, |
|
153 | 21 | 'reference' => new Reference($clientId), |
|
154 | ]; |
||
155 | } |
||
156 | 21 | } |
|
157 | |||
158 | /** |
||
159 | * Loads the configured indexes. |
||
160 | * |
||
161 | * @param array $indexes An array of indexes configurations |
||
162 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
163 | * |
||
164 | * @throws \InvalidArgumentException |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 21 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
169 | { |
||
170 | 21 | $indexableCallbacks = []; |
|
171 | |||
172 | 21 | foreach ($indexes as $name => $index) { |
|
173 | 21 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
174 | 21 | $indexName = $index['index_name'] ?? $name; |
|
175 | |||
176 | 21 | $indexDef = new ChildDefinition('fos_elastica.index_prototype'); |
|
177 | 21 | $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndex']); |
|
178 | 21 | $indexDef->replaceArgument(0, $indexName); |
|
179 | 21 | $indexDef->addTag('fos_elastica.index', [ |
|
180 | 21 | 'name' => $name, |
|
181 | ]); |
||
182 | |||
183 | 21 | View Code Duplication | if (isset($index['client'])) { |
184 | 1 | $client = $this->getClient($index['client']); |
|
185 | |||
186 | 1 | $indexDef->setFactory([$client, 'getIndex']); |
|
187 | } |
||
188 | |||
189 | 21 | $container->setDefinition($indexId, $indexDef); |
|
190 | 21 | $reference = new Reference($indexId); |
|
191 | |||
192 | 21 | $this->indexConfigs[$name] = [ |
|
193 | 21 | 'elasticsearch_name' => $indexName, |
|
194 | 21 | 'reference' => $reference, |
|
195 | 21 | 'model' => $index['persistence']['model'] ?? null, |
|
196 | 21 | 'name' => $name, |
|
197 | 21 | 'settings' => $index['settings'], |
|
198 | 21 | 'index_prototype' => $index['index_prototype'] ?? [], |
|
199 | 21 | 'use_alias' => $index['use_alias'], |
|
200 | ]; |
||
201 | |||
202 | 21 | if ($index['finder']) { |
|
203 | $this->loadIndexFinder($container, $name, $reference); |
||
204 | } |
||
205 | |||
206 | 21 | $this->loadIndexConfig((array) $index, $this->indexConfigs[$name]); |
|
207 | |||
208 | 21 | if (isset($index['indexable_callback'])) { |
|
209 | 4 | $indexableCallbacks[$name] = $this->buildCallback($index['indexable_callback'], $name); |
|
210 | } |
||
211 | |||
212 | 21 | $this->loadIndexSerializerIntegration($index['serializer'] ?? [], $container, $reference); |
|
213 | |||
214 | 21 | if (isset($index['persistence'])) { |
|
215 | 15 | $this->loadIndexPersistenceIntegration($index['persistence'], $container, $reference, $name); |
|
216 | } |
||
217 | } |
||
218 | |||
219 | 21 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
220 | 21 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
221 | 21 | } |
|
222 | |||
223 | /** |
||
224 | * Loads the configured indexes. |
||
225 | * |
||
226 | * @param array $indexTemplates An array of indexes configurations |
||
227 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
228 | * |
||
229 | * @throws \InvalidArgumentException |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | 21 | private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $container) |
|
234 | { |
||
235 | 21 | foreach ($indexTemplates as $name => $indexTemplate) { |
|
236 | 6 | $indexId = sprintf('fos_elastica.index_template.%s', $name); |
|
237 | 6 | $indexTemplateName = $indexTemplate['template_name'] ?? $name; |
|
238 | |||
239 | 6 | $indexDef = new ChildDefinition('fos_elastica.index_template_prototype'); |
|
240 | 6 | $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndexTemplate']); |
|
241 | 6 | $indexDef->replaceArgument(0, $indexTemplateName); |
|
242 | 6 | $indexDef->addTag('fos_elastica.index_template', [ |
|
243 | 6 | 'name' => $name, |
|
244 | ]); |
||
245 | |||
246 | 6 | View Code Duplication | if (isset($indexTemplate['client'])) { |
247 | 6 | $client = $this->getClient($indexTemplate['client']); |
|
248 | 6 | $indexDef->setFactory([$client, 'getIndexTemplate']); |
|
249 | } |
||
250 | |||
251 | 6 | $container->setDefinition($indexId, $indexDef); |
|
252 | 6 | $reference = new Reference($indexId); |
|
253 | |||
254 | 6 | $this->indexTemplateConfigs[$name] = [ |
|
255 | 6 | 'elasticsearch_name' => $indexTemplateName, |
|
256 | 6 | 'reference' => $reference, |
|
257 | 6 | 'name' => $name, |
|
258 | 6 | 'settings' => $indexTemplate['settings'], |
|
259 | 6 | 'template' => $indexTemplate['template'], |
|
260 | ]; |
||
261 | |||
262 | 6 | $this->loadIndexConfig((array) $indexTemplate, $this->indexTemplateConfigs[$name]); |
|
263 | } |
||
264 | 21 | } |
|
265 | |||
266 | /** |
||
267 | * Loads the configured index finders. |
||
268 | * |
||
269 | * @param string $name The index name |
||
270 | * @param Reference $index Reference to the related index |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | private function loadIndexFinder(ContainerBuilder $container, string $name, Reference $index): void |
||
275 | { |
||
276 | $finderId = sprintf('fos_elastica.finder.%s', $name); |
||
277 | $finderDef = new ChildDefinition('fos_elastica.finder'); |
||
278 | $finderDef->replaceArgument(0, $index); |
||
279 | $finderDef->replaceArgument(1, new Reference(sprintf('fos_elastica.elastica_to_model_transformer.%s', $name))); |
||
280 | |||
281 | $container->setDefinition($finderId, $finderDef); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Loads the configured $index. |
||
286 | */ |
||
287 | 21 | private function loadIndexConfig(array $index, array &$indexConfig): void |
|
288 | { |
||
289 | 21 | $indexConfig = array_merge($indexConfig, [ |
|
290 | 21 | 'mapping' => [], // An array containing anything that gets sent directly to ElasticSearch |
|
291 | 'config' => [], |
||
292 | ]); |
||
293 | |||
294 | foreach ([ |
||
295 | 21 | 'dynamic_templates', |
|
296 | 'properties', |
||
297 | '_id', |
||
298 | '_routing', |
||
299 | '_source', |
||
300 | ] as $field) { |
||
301 | 21 | if (isset($index[$field])) { |
|
302 | 21 | $indexConfig['mapping'][$field] = $index[$field]; |
|
303 | } |
||
304 | } |
||
305 | |||
306 | foreach ([ |
||
307 | 21 | 'analyzer', |
|
308 | 'search_analyzer', |
||
309 | 'dynamic', |
||
310 | 'date_detection', |
||
311 | 'dynamic_date_formats', |
||
312 | 'numeric_detection', |
||
313 | ] as $field) { |
||
314 | 21 | $indexConfig['config'][$field] = array_key_exists($field, $index) ? $index[$field] : null; |
|
315 | } |
||
316 | 21 | } |
|
317 | |||
318 | 4 | private function buildCallback($indexCallback, $indexName) |
|
343 | |||
344 | 4 | private function transformServiceReference($classOrService) |
|
348 | |||
349 | 21 | private function loadIndexSerializerIntegration(array $config, ContainerBuilder $container, Reference $indexRef): void |
|
350 | { |
||
351 | 21 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
352 | 1 | $indexSerializerId = sprintf('%s.serializer.callback', $indexRef); |
|
353 | 1 | $indexSerializerDef = new ChildDefinition('fos_elastica.serializer_callback_prototype'); |
|
354 | |||
355 | 1 | if (isset($config['groups'])) { |
|
370 | |||
371 | /** |
||
372 | * Loads the optional provider and finder for a type. |
||
373 | */ |
||
374 | 15 | private function loadIndexPersistenceIntegration(array $config, ContainerBuilder $container, Reference $indexRef, string $indexName): void |
|
394 | |||
395 | /** |
||
396 | * Creates and loads an ElasticaToModelTransformer. |
||
397 | */ |
||
398 | 15 | private function loadElasticaToModelTransformer(array $persistenceConfig, ContainerBuilder $container, string $indexName): string |
|
420 | |||
421 | /** |
||
422 | * Creates and loads a ModelToElasticaTransformer for an index. |
||
423 | */ |
||
424 | 15 | private function loadModelToElasticaTransformer(array $config, ContainerBuilder $container, string $indexName): string |
|
444 | |||
445 | /** |
||
446 | * Creates and loads an object persister for a index. |
||
447 | */ |
||
448 | 15 | private function loadObjectPersister(array $config, Reference $indexRef, ContainerBuilder $container, string $indexName, string $transformerId): string |
|
485 | |||
486 | /** |
||
487 | * Loads a pager provider for a index. |
||
488 | */ |
||
489 | 15 | private function loadIndexPagerProvider(array $indexConfig, ContainerBuilder $container, string $indexName): string |
|
522 | |||
523 | /** |
||
524 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
525 | */ |
||
526 | 14 | private function loadIndexListener(array $indexConfig, ContainerBuilder $container, string $objectPersisterId, string $indexName): string |
|
586 | |||
587 | /** |
||
588 | * Map Elastica to Doctrine events for the current driver. |
||
589 | */ |
||
590 | 14 | private function getDoctrineEvents(array $indexConfig) |
|
622 | |||
623 | /** |
||
624 | * Loads a Type specific Finder. |
||
625 | */ |
||
626 | 15 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, string $elasticaToModelId, Reference $indexRef, string $indexName): string |
|
652 | |||
653 | /** |
||
654 | * Loads the index manager. |
||
655 | **/ |
||
656 | 21 | View Code Duplication | private function loadIndexManager(ContainerBuilder $container): void |
665 | |||
666 | /** |
||
667 | * Load index template manager. |
||
668 | */ |
||
669 | 21 | View Code Duplication | private function loadIndexTemplateManager(ContainerBuilder $container): void |
678 | |||
679 | /** |
||
680 | * Makes sure a specific driver has been loaded. |
||
681 | */ |
||
682 | 15 | private function loadDriver(ContainerBuilder $container, string $driver): void |
|
692 | |||
693 | /** |
||
694 | * Loads and configures the serializer prototype. |
||
695 | */ |
||
696 | 1 | private function loadSerializer(array $config, ContainerBuilder $container): void |
|
707 | |||
708 | /** |
||
709 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
710 | */ |
||
711 | 21 | private function createDefaultManagerAlias(string $defaultManager, ContainerBuilder $container): void |
|
730 | |||
731 | /** |
||
732 | * Returns a reference to a client given its configured name. |
||
733 | * |
||
734 | * @throws \InvalidArgumentException |
||
735 | */ |
||
736 | 7 | private function getClient(string $clientName): Reference |
|
744 | } |
||
745 |