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 |
||
13 | class FOSElasticaExtension extends Extension |
||
14 | { |
||
15 | /** |
||
16 | * Definition of elastica clients as configured by this extension. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | private $clients = array(); |
||
21 | |||
22 | /** |
||
23 | * An array of indexes as configured by the extension. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $indexConfigs = array(); |
||
28 | |||
29 | /** |
||
30 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
31 | * here. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $loadedDrivers = array(); |
||
36 | |||
37 | 8 | public function load(array $configs, ContainerBuilder $container) |
|
81 | |||
82 | /** |
||
83 | * @param array $config |
||
84 | * @param ContainerBuilder $container |
||
85 | * |
||
86 | * @return Configuration |
||
87 | */ |
||
88 | 8 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
92 | |||
93 | /** |
||
94 | * Loads the configured clients. |
||
95 | * |
||
96 | * @param array $clients An array of clients configurations |
||
97 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 8 | private function loadClients(array $clients, ContainerBuilder $container) |
|
123 | |||
124 | /** |
||
125 | * Loads the configured indexes. |
||
126 | * |
||
127 | * @param array $indexes An array of indexes configurations |
||
128 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
129 | * |
||
130 | * @throws \InvalidArgumentException |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 8 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
190 | |||
191 | /** |
||
192 | * Loads the configured index finders. |
||
193 | * |
||
194 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
195 | * @param string $name The index name |
||
196 | * @param Reference $index Reference to the related index |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
216 | |||
217 | /** |
||
218 | * Loads the configured types. |
||
219 | * |
||
220 | * @param array $types |
||
221 | * @param ContainerBuilder $container |
||
222 | * @param array $indexConfig |
||
223 | * @param array $indexableCallbacks |
||
224 | */ |
||
225 | 8 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
226 | { |
||
227 | 8 | foreach ($types as $name => $type) { |
|
228 | 8 | $indexName = $indexConfig['name']; |
|
229 | |||
230 | 8 | $typeId = sprintf('%s.%s', $indexConfig['reference'], $name); |
|
231 | 8 | $typeDef = new DefinitionDecorator('fos_elastica.type_prototype'); |
|
232 | 8 | $typeDef->replaceArgument(0, $name); |
|
233 | |||
234 | 8 | if (method_exists($typeDef, 'setFactory')) { |
|
235 | 8 | $typeDef->setFactory(array($indexConfig['reference'], 'getType')); |
|
236 | 8 | } else { |
|
237 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
238 | $typeDef->setFactoryService($indexConfig['reference']); |
||
239 | $typeDef->setFactoryMethod('getType'); |
||
240 | } |
||
241 | |||
242 | 8 | $container->setDefinition($typeId, $typeDef); |
|
243 | |||
244 | $typeConfig = array( |
||
245 | 8 | 'name' => $name, |
|
246 | 8 | 'mapping' => array(), // An array containing anything that gets sent directly to ElasticSearch |
|
247 | 8 | 'config' => array(), |
|
248 | 8 | ); |
|
249 | |||
250 | foreach (array( |
||
251 | 8 | 'dynamic_templates', |
|
252 | 8 | 'properties', |
|
253 | 8 | '_all', |
|
254 | 8 | '_boost', |
|
255 | 8 | '_id', |
|
256 | 8 | '_parent', |
|
257 | 8 | '_routing', |
|
258 | 8 | '_source', |
|
259 | 8 | '_timestamp', |
|
260 | 8 | '_ttl', |
|
261 | 8 | ) as $field) { |
|
262 | 8 | if (isset($type[$field])) { |
|
263 | 8 | $typeConfig['mapping'][$field] = $type[$field]; |
|
264 | 8 | } |
|
265 | 8 | } |
|
266 | |||
267 | foreach (array( |
||
268 | 8 | 'persistence', |
|
269 | 8 | 'serializer', |
|
270 | 8 | 'index_analyzer', |
|
271 | 8 | 'search_analyzer', |
|
272 | 8 | 'date_detection', |
|
273 | 8 | 'dynamic_date_formats', |
|
274 | 8 | 'numeric_detection', |
|
275 | 8 | ) as $field) { |
|
276 | 8 | $typeConfig['config'][$field] = array_key_exists($field, $type) ? |
|
277 | 8 | $type[$field] : |
|
278 | 8 | null; |
|
279 | 8 | } |
|
280 | |||
281 | 8 | $this->indexConfigs[$indexName]['types'][$name] = $typeConfig; |
|
282 | |||
283 | 8 | if (isset($type['persistence'])) { |
|
284 | 4 | $this->loadTypePersistenceIntegration($type['persistence'], $container, new Reference($typeId), $indexName, $name); |
|
285 | |||
286 | 4 | $typeConfig['persistence'] = $type['persistence']; |
|
287 | 4 | } |
|
288 | |||
289 | 8 | if (isset($type['indexable_callback'])) { |
|
290 | $indexableCallbacks[sprintf('%s/%s', $indexName, $name)] = $type['indexable_callback']; |
||
291 | } |
||
292 | |||
293 | 8 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
294 | 2 | $typeSerializerId = sprintf('%s.serializer.callback', $typeId); |
|
295 | 2 | $typeSerializerDef = new DefinitionDecorator('fos_elastica.serializer_callback_prototype'); |
|
296 | |||
297 | 2 | if (isset($type['serializer']['groups'])) { |
|
298 | 2 | $typeSerializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); |
|
299 | 2 | } |
|
300 | 2 | if (isset($type['serializer']['version'])) { |
|
301 | 2 | $typeSerializerDef->addMethodCall('setVersion', array($type['serializer']['version'])); |
|
302 | 2 | } |
|
303 | |||
304 | 2 | $typeDef->addMethodCall('setSerializer', array(array(new Reference($typeSerializerId), 'serialize'))); |
|
305 | 2 | $container->setDefinition($typeSerializerId, $typeSerializerDef); |
|
306 | 2 | } |
|
307 | 8 | } |
|
308 | 8 | } |
|
309 | |||
310 | /** |
||
311 | * Loads the optional provider and finder for a type. |
||
312 | * |
||
313 | * @param array $typeConfig |
||
314 | * @param ContainerBuilder $container |
||
315 | * @param Reference $typeRef |
||
316 | * @param string $indexName |
||
317 | * @param string $typeName |
||
318 | */ |
||
319 | 4 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
320 | { |
||
321 | 4 | if (isset($typeConfig['driver'])) { |
|
322 | 3 | $this->loadDriver($container, $typeConfig['driver']); |
|
323 | 3 | } |
|
324 | |||
325 | 4 | $elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($typeConfig, $container, $indexName, $typeName); |
|
326 | 4 | $modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($typeConfig, $container, $indexName, $typeName); |
|
327 | 4 | $objectPersisterId = $this->loadObjectPersister($typeConfig, $typeRef, $container, $indexName, $typeName, $modelToElasticaTransformerId); |
|
328 | |||
329 | 4 | if (isset($typeConfig['provider'])) { |
|
330 | $this->loadTypeProvider($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
||
331 | } |
||
332 | 4 | if (isset($typeConfig['finder'])) { |
|
333 | $this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName); |
||
334 | } |
||
335 | 4 | if (isset($typeConfig['listener'])) { |
|
336 | $this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
||
337 | } |
||
338 | 4 | } |
|
339 | |||
340 | /** |
||
341 | * Creates and loads an ElasticaToModelTransformer. |
||
342 | * |
||
343 | * @param array $typeConfig |
||
344 | * @param ContainerBuilder $container |
||
345 | * @param string $indexName |
||
346 | * @param string $typeName |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | 4 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
375 | |||
376 | /** |
||
377 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
378 | * |
||
379 | * @param array $typeConfig |
||
380 | * @param ContainerBuilder $container |
||
381 | * @param string $indexName |
||
382 | * @param string $typeName |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | 4 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
405 | |||
406 | /** |
||
407 | * Creates and loads an object persister for a type. |
||
408 | * |
||
409 | * @param array $typeConfig |
||
410 | * @param Reference $typeRef |
||
411 | * @param ContainerBuilder $container |
||
412 | * @param string $indexName |
||
413 | * @param string $typeName |
||
414 | * @param string $transformerId |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | 4 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
454 | |||
455 | /** |
||
456 | * Loads a provider for a type. |
||
457 | * |
||
458 | * @param array $typeConfig |
||
459 | * @param ContainerBuilder $container |
||
460 | * @param string $objectPersisterId |
||
461 | * @param string $indexName |
||
462 | * @param string $typeName |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
||
467 | { |
||
468 | if (isset($typeConfig['provider']['service'])) { |
||
469 | return $typeConfig['provider']['service']; |
||
470 | } |
||
471 | |||
472 | /* Note: provider services may conflict with "prototype.driver", if the |
||
473 | * index and type names were "prototype" and a driver, respectively. |
||
474 | */ |
||
475 | $providerId = sprintf('fos_elastica.provider.%s.%s', $indexName, $typeName); |
||
476 | $providerDef = new DefinitionDecorator('fos_elastica.provider.prototype.'.$typeConfig['driver']); |
||
477 | $providerDef->addTag('fos_elastica.provider', array('index' => $indexName, 'type' => $typeName)); |
||
478 | $providerDef->replaceArgument(0, new Reference($objectPersisterId)); |
||
479 | $providerDef->replaceArgument(2, $typeConfig['model']); |
||
480 | // Propel provider can simply ignore Doctrine-specific options |
||
481 | $providerDef->replaceArgument(3, array_merge(array_diff_key($typeConfig['provider'], array('service' => 1)), array( |
||
482 | 'indexName' => $indexName, |
||
483 | 'typeName' => $typeName, |
||
484 | ))); |
||
485 | $container->setDefinition($providerId, $providerDef); |
||
486 | |||
487 | return $providerId; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
492 | * |
||
493 | * @param array $typeConfig |
||
494 | * @param ContainerBuilder $container |
||
495 | * @param string $objectPersisterId |
||
496 | * @param string $indexName |
||
497 | * @param string $typeName |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
||
502 | { |
||
503 | if (isset($typeConfig['listener']['service'])) { |
||
504 | return $typeConfig['listener']['service']; |
||
505 | } |
||
506 | |||
507 | /* Note: listener services may conflict with "prototype.driver", if the |
||
508 | * index and type names were "prototype" and a driver, respectively. |
||
509 | */ |
||
510 | $abstractListenerId = sprintf('fos_elastica.listener.prototype.%s', $typeConfig['driver']); |
||
511 | $listenerId = sprintf('fos_elastica.listener.%s.%s', $indexName, $typeName); |
||
512 | $listenerDef = new DefinitionDecorator($abstractListenerId); |
||
513 | $listenerDef->replaceArgument(0, new Reference($objectPersisterId)); |
||
514 | $listenerDef->replaceArgument(2, array( |
||
515 | 'identifier' => $typeConfig['identifier'], |
||
516 | 'indexName' => $indexName, |
||
517 | 'typeName' => $typeName, |
||
518 | )); |
||
519 | $listenerDef->replaceArgument(3, $typeConfig['listener']['logger'] ? |
||
520 | new Reference($typeConfig['listener']['logger']) : |
||
521 | null |
||
522 | ); |
||
523 | |||
524 | $tagName = null; |
||
525 | switch ($typeConfig['driver']) { |
||
526 | case 'orm': |
||
527 | $tagName = 'doctrine.event_listener'; |
||
528 | break; |
||
529 | case 'phpcr': |
||
530 | $tagName = 'doctrine_phpcr.event_listener'; |
||
531 | break; |
||
532 | case 'mongodb': |
||
533 | $tagName = 'doctrine_mongodb.odm.event_listener'; |
||
534 | break; |
||
535 | } |
||
536 | |||
537 | if (null !== $tagName) { |
||
538 | foreach ($this->getDoctrineEvents($typeConfig) as $event) { |
||
539 | $listenerDef->addTag($tagName, array('event' => $event)); |
||
540 | } |
||
541 | } |
||
542 | |||
543 | $container->setDefinition($listenerId, $listenerDef); |
||
544 | |||
545 | return $listenerId; |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Map Elastica to Doctrine events for the current driver. |
||
550 | */ |
||
551 | private function getDoctrineEvents(array $typeConfig) |
||
552 | { |
||
553 | switch ($typeConfig['driver']) { |
||
554 | case 'orm': |
||
555 | $eventsClass = '\Doctrine\ORM\Events'; |
||
556 | break; |
||
557 | case 'phpcr': |
||
558 | $eventsClass = '\Doctrine\ODM\PHPCR\Event'; |
||
559 | break; |
||
560 | case 'mongodb': |
||
561 | $eventsClass = '\Doctrine\ODM\MongoDB\Events'; |
||
562 | break; |
||
563 | default: |
||
564 | throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver'])); |
||
565 | } |
||
566 | |||
567 | $events = array(); |
||
568 | $eventMapping = array( |
||
569 | 'insert' => array(constant($eventsClass.'::postPersist')), |
||
570 | 'update' => array(constant($eventsClass.'::postUpdate')), |
||
571 | 'delete' => array(constant($eventsClass.'::preRemove')), |
||
572 | 'flush' => array($typeConfig['listener']['immediate'] ? constant($eventsClass.'::preFlush') : constant($eventsClass.'::postFlush')), |
||
573 | ); |
||
574 | |||
575 | foreach ($eventMapping as $event => $doctrineEvents) { |
||
576 | if (isset($typeConfig['listener'][$event]) && $typeConfig['listener'][$event]) { |
||
577 | $events = array_merge($events, $doctrineEvents); |
||
578 | } |
||
579 | } |
||
580 | |||
581 | return $events; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Loads a Type specific Finder. |
||
586 | * |
||
587 | * @param array $typeConfig |
||
588 | * @param ContainerBuilder $container |
||
589 | * @param string $elasticaToModelId |
||
590 | * @param Reference $typeRef |
||
591 | * @param string $indexName |
||
592 | * @param string $typeName |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
||
597 | { |
||
598 | if (isset($typeConfig['finder']['service'])) { |
||
599 | $finderId = $typeConfig['finder']['service']; |
||
600 | } else { |
||
601 | $finderId = sprintf('fos_elastica.finder.%s.%s', $indexName, $typeName); |
||
602 | $finderDef = new DefinitionDecorator('fos_elastica.finder'); |
||
603 | $finderDef->replaceArgument(0, $typeRef); |
||
604 | $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); |
||
605 | $container->setDefinition($finderId, $finderDef); |
||
606 | } |
||
607 | |||
608 | $managerId = sprintf('fos_elastica.manager.%s', $typeConfig['driver']); |
||
609 | $managerDef = $container->getDefinition($managerId); |
||
610 | $arguments = array( $typeConfig['model'], new Reference($finderId)); |
||
611 | if (isset($typeConfig['repository'])) { |
||
612 | $arguments[] = $typeConfig['repository']; |
||
613 | } |
||
614 | $managerDef->addMethodCall('addEntity', $arguments); |
||
615 | |||
616 | return $finderId; |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * Loads the index manager. |
||
621 | * |
||
622 | * @param ContainerBuilder $container |
||
623 | **/ |
||
624 | private function loadIndexManager(ContainerBuilder $container) |
||
633 | |||
634 | /** |
||
635 | * Makes sure a specific driver has been loaded. |
||
636 | * |
||
637 | * @param ContainerBuilder $container |
||
638 | * @param string $driver |
||
639 | */ |
||
640 | 3 | private function loadDriver(ContainerBuilder $container, $driver) |
|
650 | |||
651 | /** |
||
652 | * Loads and configures the serializer prototype. |
||
653 | * |
||
654 | * @param array $config |
||
655 | * @param ContainerBuilder $container |
||
656 | */ |
||
657 | 2 | private function loadSerializer($config, ContainerBuilder $container) |
|
669 | |||
670 | /** |
||
671 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
672 | * |
||
673 | * @param string $defaultManager |
||
674 | * @param ContainerBuilder $container |
||
675 | */ |
||
676 | 8 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
692 | |||
693 | /** |
||
694 | * Returns a reference to a client given its configured name. |
||
695 | * |
||
696 | * @param string $clientName |
||
697 | * |
||
698 | * @return Reference |
||
699 | * |
||
700 | * @throws \InvalidArgumentException |
||
701 | */ |
||
702 | 2 | private function getClient($clientName) |
|
710 | } |
||
711 |