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 |
||
14 | class FOSElasticaExtension extends Extension |
||
15 | { |
||
16 | /** |
||
17 | * Definition of elastica clients as configured by this extension. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $clients = array(); |
||
22 | |||
23 | /** |
||
24 | * An array of indexes as configured by the extension. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $indexConfigs = array(); |
||
29 | |||
30 | /** |
||
31 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
32 | * here. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $loadedDrivers = array(); |
||
37 | |||
38 | 15 | public function load(array $configs, ContainerBuilder $container) |
|
82 | |||
83 | /** |
||
84 | * @param array $config |
||
85 | * @param ContainerBuilder $container |
||
86 | * |
||
87 | * @return Configuration |
||
88 | */ |
||
89 | 15 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
93 | |||
94 | /** |
||
95 | * Loads the configured clients. |
||
96 | * |
||
97 | * @param array $clients An array of clients configurations |
||
98 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 3 | private function loadClients(array $clients, ContainerBuilder $container) |
|
103 | { |
||
104 | 3 | foreach ($clients as $name => $clientConfig) { |
|
105 | 3 | $clientId = sprintf('fos_elastica.client.%s', $name); |
|
106 | |||
107 | 3 | $clientDef = new DefinitionDecorator('fos_elastica.client_prototype'); |
|
108 | 3 | $clientDef->replaceArgument(0, $clientConfig); |
|
109 | |||
110 | 3 | $logger = $clientConfig['connections'][0]['logger']; |
|
111 | 3 | if (false !== $logger) { |
|
112 | 3 | $clientDef->addMethodCall('setLogger', array(new Reference($logger))); |
|
113 | } |
||
114 | |||
115 | 3 | $clientDef->addTag('fos_elastica.client'); |
|
116 | |||
117 | 3 | $container->setDefinition($clientId, $clientDef); |
|
118 | |||
119 | 3 | $this->clients[$name] = array( |
|
120 | 3 | 'id' => $clientId, |
|
121 | 3 | 'reference' => new Reference($clientId), |
|
122 | ); |
||
123 | } |
||
124 | 3 | } |
|
125 | |||
126 | /** |
||
127 | * Loads the configured indexes. |
||
128 | * |
||
129 | * @param array $indexes An array of indexes configurations |
||
130 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
131 | * |
||
132 | * @throws \InvalidArgumentException |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 3 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
137 | { |
||
138 | 3 | $indexableCallbacks = array(); |
|
139 | |||
140 | 3 | foreach ($indexes as $name => $index) { |
|
141 | 3 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
142 | 3 | $indexName = isset($index['index_name']) ? $index['index_name'] : $name; |
|
143 | |||
144 | 3 | $indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); |
|
145 | 3 | $indexDef->setFactory(array(new Reference('fos_elastica.client'), 'getIndex')); |
|
146 | 3 | $indexDef->replaceArgument(0, $indexName); |
|
147 | 3 | $indexDef->addTag('fos_elastica.index', array( |
|
148 | 3 | 'name' => $name, |
|
149 | )); |
||
150 | |||
151 | 3 | if (isset($index['client'])) { |
|
152 | $client = $this->getClient($index['client']); |
||
153 | |||
154 | $indexDef->setFactory(array($client, 'getIndex')); |
||
155 | } |
||
156 | |||
157 | 3 | $container->setDefinition($indexId, $indexDef); |
|
158 | 3 | $reference = new Reference($indexId); |
|
159 | |||
160 | 3 | $this->indexConfigs[$name] = array( |
|
161 | 3 | 'elasticsearch_name' => $indexName, |
|
162 | 3 | 'reference' => $reference, |
|
163 | 3 | 'name' => $name, |
|
164 | 3 | 'settings' => $index['settings'], |
|
165 | 3 | 'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : array(), |
|
166 | 3 | 'use_alias' => $index['use_alias'], |
|
167 | ); |
||
168 | |||
169 | 3 | if ($index['finder']) { |
|
170 | $this->loadIndexFinder($container, $name, $reference); |
||
171 | } |
||
172 | |||
173 | 3 | $this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name], $indexableCallbacks); |
|
174 | } |
||
175 | |||
176 | 3 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
177 | 3 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
178 | 3 | } |
|
179 | |||
180 | /** |
||
181 | * Loads the configured index finders. |
||
182 | * |
||
183 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
184 | * @param string $name The index name |
||
185 | * @param Reference $index Reference to the related index |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
205 | |||
206 | /** |
||
207 | * Loads the configured types. |
||
208 | * |
||
209 | * @param array $types |
||
210 | * @param ContainerBuilder $container |
||
211 | * @param array $indexConfig |
||
212 | * @param array $indexableCallbacks |
||
213 | */ |
||
214 | 3 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
215 | { |
||
216 | 3 | foreach ($types as $name => $type) { |
|
217 | 3 | $indexName = $indexConfig['name']; |
|
218 | |||
219 | 3 | $typeId = sprintf('%s.%s', $indexConfig['reference'], $name); |
|
220 | 3 | $typeDef = new DefinitionDecorator('fos_elastica.type_prototype'); |
|
221 | 3 | $typeDef->setFactory(array($indexConfig['reference'], 'getType')); |
|
222 | 3 | $typeDef->replaceArgument(0, $name); |
|
223 | |||
224 | 3 | $container->setDefinition($typeId, $typeDef); |
|
225 | |||
226 | $typeConfig = array( |
||
227 | 3 | 'name' => $name, |
|
228 | 'mapping' => array(), // An array containing anything that gets sent directly to ElasticSearch |
||
229 | 'config' => array(), |
||
230 | ); |
||
231 | |||
232 | foreach (array( |
||
233 | 3 | 'dynamic_templates', |
|
234 | 'properties', |
||
235 | '_all', |
||
236 | '_boost', |
||
237 | '_id', |
||
238 | '_parent', |
||
239 | '_routing', |
||
240 | '_source', |
||
241 | '_timestamp', |
||
242 | '_ttl', |
||
243 | ) as $field) { |
||
244 | 3 | if (isset($type[$field])) { |
|
245 | 3 | $typeConfig['mapping'][$field] = $type[$field]; |
|
246 | } |
||
247 | } |
||
248 | |||
249 | foreach (array( |
||
250 | 3 | 'persistence', |
|
251 | 'serializer', |
||
252 | 'analyzer', |
||
253 | 'search_analyzer', |
||
254 | 'dynamic', |
||
255 | 'date_detection', |
||
256 | 'dynamic_date_formats', |
||
257 | 'numeric_detection', |
||
258 | ) as $field) { |
||
259 | 3 | $typeConfig['config'][$field] = array_key_exists($field, $type) ? |
|
260 | 3 | $type[$field] : |
|
261 | 3 | null; |
|
262 | } |
||
263 | |||
264 | 3 | $this->indexConfigs[$indexName]['types'][$name] = $typeConfig; |
|
265 | |||
266 | 3 | if (isset($type['persistence'])) { |
|
267 | 3 | $this->loadTypePersistenceIntegration($type['persistence'], $container, new Reference($typeId), $indexName, $name); |
|
268 | |||
269 | 3 | $typeConfig['persistence'] = $type['persistence']; |
|
270 | } |
||
271 | |||
272 | 3 | if (isset($type['_parent'])) { |
|
273 | // _parent mapping cannot contain `property` and `identifier`, so removing them after building `persistence` |
||
274 | unset($indexConfig['types'][$name]['mapping']['_parent']['property'], $indexConfig['types'][$name]['mapping']['_parent']['identifier']); |
||
275 | } |
||
276 | |||
277 | 3 | if (isset($type['indexable_callback'])) { |
|
278 | $indexableCallbacks[sprintf('%s/%s', $indexName, $name)] = $type['indexable_callback']; |
||
279 | } |
||
280 | |||
281 | 3 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
282 | 3 | $typeSerializerId = sprintf('%s.serializer.callback', $typeId); |
|
283 | 3 | $typeSerializerDef = new DefinitionDecorator('fos_elastica.serializer_callback_prototype'); |
|
284 | |||
285 | 3 | if (isset($type['serializer']['groups'])) { |
|
286 | 3 | $typeSerializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); |
|
287 | } |
||
288 | |||
289 | 3 | if (isset($type['serializer']['serialize_null'])) { |
|
290 | 3 | $typeSerializerDef->addMethodCall('setSerializeNull', array($type['serializer']['serialize_null'])); |
|
291 | } |
||
292 | |||
293 | 3 | if (isset($type['serializer']['version'])) { |
|
294 | 3 | $typeSerializerDef->addMethodCall('setVersion', array($type['serializer']['version'])); |
|
295 | } |
||
296 | |||
297 | 3 | $typeDef->addMethodCall('setSerializer', array(array(new Reference($typeSerializerId), 'serialize'))); |
|
298 | 3 | $container->setDefinition($typeSerializerId, $typeSerializerDef); |
|
299 | } |
||
300 | } |
||
301 | 3 | } |
|
302 | |||
303 | /** |
||
304 | * Loads the optional provider and finder for a type. |
||
305 | * |
||
306 | * @param array $typeConfig |
||
307 | * @param ContainerBuilder $container |
||
308 | * @param Reference $typeRef |
||
309 | * @param string $indexName |
||
310 | * @param string $typeName |
||
311 | */ |
||
312 | 3 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
313 | { |
||
314 | 3 | if (isset($typeConfig['driver'])) { |
|
315 | 3 | $this->loadDriver($container, $typeConfig['driver']); |
|
316 | } |
||
317 | |||
318 | 3 | $elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($typeConfig, $container, $indexName, $typeName); |
|
319 | 3 | $modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($typeConfig, $container, $indexName, $typeName); |
|
320 | 3 | $objectPersisterId = $this->loadObjectPersister($typeConfig, $typeRef, $container, $indexName, $typeName, $modelToElasticaTransformerId); |
|
321 | |||
322 | 3 | if (isset($typeConfig['provider'])) { |
|
323 | 3 | $this->loadTypeProvider($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
324 | } |
||
325 | 3 | if (isset($typeConfig['finder'])) { |
|
326 | 3 | $this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName); |
|
327 | } |
||
328 | 3 | if (isset($typeConfig['listener'])) { |
|
329 | 3 | $this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
330 | } |
||
331 | 3 | } |
|
332 | |||
333 | /** |
||
334 | * Creates and loads an ElasticaToModelTransformer. |
||
335 | * |
||
336 | * @param array $typeConfig |
||
337 | * @param ContainerBuilder $container |
||
338 | * @param string $indexName |
||
339 | * @param string $typeName |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 3 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
344 | { |
||
345 | 3 | if (isset($typeConfig['elastica_to_model_transformer']['service'])) { |
|
346 | return $typeConfig['elastica_to_model_transformer']['service']; |
||
347 | } |
||
348 | |||
349 | /* Note: transformer services may conflict with "prototype.driver", if |
||
350 | * the index and type names were "prototype" and a driver, respectively. |
||
351 | */ |
||
352 | 3 | $abstractId = sprintf('fos_elastica.elastica_to_model_transformer.prototype.%s', $typeConfig['driver']); |
|
353 | 3 | $serviceId = sprintf('fos_elastica.elastica_to_model_transformer.%s.%s', $indexName, $typeName); |
|
354 | 3 | $serviceDef = new DefinitionDecorator($abstractId); |
|
355 | 3 | $serviceDef->addTag('fos_elastica.elastica_to_model_transformer', array('type' => $typeName, 'index' => $indexName)); |
|
356 | |||
357 | // Doctrine has a mandatory service as first argument |
||
358 | 3 | $argPos = ('propel' === $typeConfig['driver']) ? 0 : 1; |
|
359 | |||
360 | 3 | $serviceDef->replaceArgument($argPos, $typeConfig['model']); |
|
361 | 3 | $serviceDef->replaceArgument($argPos + 1, array_merge($typeConfig['elastica_to_model_transformer'], array( |
|
362 | 3 | 'identifier' => $typeConfig['identifier'], |
|
363 | ))); |
||
364 | 3 | $container->setDefinition($serviceId, $serviceDef); |
|
365 | |||
366 | 3 | return $serviceId; |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
371 | * |
||
372 | * @param array $typeConfig |
||
373 | * @param ContainerBuilder $container |
||
374 | * @param string $indexName |
||
375 | * @param string $typeName |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | 3 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
398 | |||
399 | /** |
||
400 | * Creates and loads an object persister for a type. |
||
401 | * |
||
402 | * @param array $typeConfig |
||
403 | * @param Reference $typeRef |
||
404 | * @param ContainerBuilder $container |
||
405 | * @param string $indexName |
||
406 | * @param string $typeName |
||
407 | * @param string $transformerId |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 3 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
412 | { |
||
413 | 3 | if (isset($typeConfig['persister']['service'])) { |
|
414 | return $typeConfig['persister']['service']; |
||
415 | } |
||
416 | |||
417 | $arguments = array( |
||
418 | 3 | $typeRef, |
|
419 | 3 | new Reference($transformerId), |
|
420 | 3 | $typeConfig['model'], |
|
421 | ); |
||
422 | |||
423 | 3 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
424 | 3 | $abstractId = 'fos_elastica.object_serializer_persister'; |
|
425 | 3 | $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['reference'], $typeName); |
|
426 | 3 | $arguments[] = array(new Reference($callbackId), 'serialize'); |
|
427 | } else { |
||
428 | $abstractId = 'fos_elastica.object_persister'; |
||
429 | $mapping = $this->indexConfigs[$indexName]['types'][$typeName]['mapping']; |
||
430 | $argument = $mapping['properties']; |
||
431 | if (isset($mapping['_parent'])) { |
||
432 | $argument['_parent'] = $mapping['_parent']; |
||
433 | } |
||
434 | $arguments[] = $argument; |
||
435 | } |
||
436 | |||
437 | 3 | $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); |
|
438 | 3 | $serviceDef = new DefinitionDecorator($abstractId); |
|
439 | 3 | foreach ($arguments as $i => $argument) { |
|
440 | 3 | $serviceDef->replaceArgument($i, $argument); |
|
441 | } |
||
442 | |||
443 | 3 | $container->setDefinition($serviceId, $serviceDef); |
|
444 | |||
445 | 3 | return $serviceId; |
|
446 | } |
||
447 | |||
448 | /** |
||
449 | * Loads a provider for a type. |
||
450 | * |
||
451 | * @param array $typeConfig |
||
452 | * @param ContainerBuilder $container |
||
453 | * @param string $objectPersisterId |
||
454 | * @param string $indexName |
||
455 | * @param string $typeName |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | 3 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
482 | |||
483 | /** |
||
484 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
485 | * |
||
486 | * @param array $typeConfig |
||
487 | * @param ContainerBuilder $container |
||
488 | * @param string $objectPersisterId |
||
489 | * @param string $indexName |
||
490 | * @param string $typeName |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | 3 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
540 | |||
541 | /** |
||
542 | * Map Elastica to Doctrine events for the current driver. |
||
543 | */ |
||
544 | 3 | private function getDoctrineEvents(array $typeConfig) |
|
545 | { |
||
546 | 3 | switch ($typeConfig['driver']) { |
|
547 | 3 | case 'orm': |
|
548 | 3 | $eventsClass = '\Doctrine\ORM\Events'; |
|
549 | 3 | break; |
|
550 | case 'phpcr': |
||
551 | $eventsClass = '\Doctrine\ODM\PHPCR\Event'; |
||
552 | break; |
||
553 | case 'mongodb': |
||
554 | $eventsClass = '\Doctrine\ODM\MongoDB\Events'; |
||
555 | break; |
||
556 | default: |
||
557 | throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver'])); |
||
558 | } |
||
559 | |||
560 | 3 | $events = array(); |
|
561 | $eventMapping = array( |
||
562 | 3 | 'insert' => array(constant($eventsClass.'::postPersist')), |
|
563 | 3 | 'update' => array(constant($eventsClass.'::postUpdate')), |
|
564 | 3 | 'delete' => array(constant($eventsClass.'::preRemove')), |
|
565 | 3 | 'flush' => array($typeConfig['listener']['immediate'] ? constant($eventsClass.'::preFlush') : constant($eventsClass.'::postFlush')), |
|
566 | ); |
||
567 | |||
568 | 3 | foreach ($eventMapping as $event => $doctrineEvents) { |
|
569 | 3 | if (isset($typeConfig['listener'][$event]) && $typeConfig['listener'][$event]) { |
|
570 | 3 | $events = array_merge($events, $doctrineEvents); |
|
571 | } |
||
572 | } |
||
573 | |||
574 | 3 | return $events; |
|
575 | } |
||
576 | |||
577 | /** |
||
578 | * Loads a Type specific Finder. |
||
579 | * |
||
580 | * @param array $typeConfig |
||
581 | * @param ContainerBuilder $container |
||
582 | * @param string $elasticaToModelId |
||
583 | * @param Reference $typeRef |
||
584 | * @param string $indexName |
||
585 | * @param string $typeName |
||
586 | * |
||
587 | * @return string |
||
588 | */ |
||
589 | 3 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
590 | { |
||
591 | 3 | if (isset($typeConfig['finder']['service'])) { |
|
592 | $finderId = $typeConfig['finder']['service']; |
||
593 | } else { |
||
594 | 3 | $finderId = sprintf('fos_elastica.finder.%s.%s', $indexName, $typeName); |
|
595 | 3 | $finderDef = new DefinitionDecorator('fos_elastica.finder'); |
|
596 | 3 | $finderDef->replaceArgument(0, $typeRef); |
|
597 | 3 | $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); |
|
598 | 3 | $container->setDefinition($finderId, $finderDef); |
|
599 | } |
||
600 | |||
601 | 3 | $indexTypeName = "$indexName/$typeName"; |
|
602 | 3 | $arguments = [$indexTypeName, new Reference($finderId)]; |
|
603 | 3 | if (isset($typeConfig['repository'])) { |
|
604 | $arguments[] = $typeConfig['repository']; |
||
605 | } |
||
606 | |||
607 | 3 | $container->getDefinition('fos_elastica.repository_manager') |
|
608 | 3 | ->addMethodCall('addType', $arguments); |
|
609 | |||
610 | 3 | $managerId = sprintf('fos_elastica.manager.%s', $typeConfig['driver']); |
|
611 | 3 | $container->getDefinition($managerId) |
|
612 | 3 | ->addMethodCall('addEntity', [$typeConfig['model'], $indexTypeName]); |
|
613 | |||
614 | 3 | return $finderId; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Loads the index manager. |
||
619 | * |
||
620 | * @param ContainerBuilder $container |
||
621 | **/ |
||
622 | private function loadIndexManager(ContainerBuilder $container) |
||
631 | |||
632 | /** |
||
633 | * Makes sure a specific driver has been loaded. |
||
634 | * |
||
635 | * @param ContainerBuilder $container |
||
636 | * @param string $driver |
||
637 | */ |
||
638 | 3 | private function loadDriver(ContainerBuilder $container, $driver) |
|
648 | |||
649 | /** |
||
650 | * Loads and configures the serializer prototype. |
||
651 | * |
||
652 | * @param array $config |
||
653 | * @param ContainerBuilder $container |
||
654 | */ |
||
655 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
666 | |||
667 | /** |
||
668 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
669 | * |
||
670 | * @param string $defaultManager |
||
671 | * @param ContainerBuilder $container |
||
672 | */ |
||
673 | 3 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
689 | |||
690 | /** |
||
691 | * Returns a reference to a client given its configured name. |
||
692 | * |
||
693 | * @param string $clientName |
||
694 | * |
||
695 | * @return Reference |
||
696 | * |
||
697 | * @throws \InvalidArgumentException |
||
698 | */ |
||
699 | private function getClient($clientName) |
||
707 | } |
||
708 |