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 |
||
22 | class FOSElasticaExtension extends Extension |
||
23 | { |
||
24 | /** |
||
25 | * Definition of elastica clients as configured by this extension. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $clients = []; |
||
30 | |||
31 | /** |
||
32 | * An array of indexes as configured by the extension. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $indexConfigs = []; |
||
37 | |||
38 | /** |
||
39 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
40 | * here. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $loadedDrivers = []; |
||
45 | |||
46 | 18 | public function load(array $configs, ContainerBuilder $container) |
|
47 | { |
||
48 | 18 | $configuration = $this->getConfiguration($configs, $container); |
|
49 | 18 | $config = $this->processConfiguration($configuration, $configs); |
|
50 | |||
51 | 18 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
52 | |||
53 | 18 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
54 | // No Clients or indexes are defined |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | 18 | foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener', 'commands'] as $basename) { |
|
59 | 18 | $loader->load(sprintf('%s.xml', $basename)); |
|
60 | } |
||
61 | |||
62 | 18 | if (empty($config['default_client'])) { |
|
63 | 18 | $keys = array_keys($config['clients']); |
|
64 | 18 | $config['default_client'] = reset($keys); |
|
65 | } |
||
66 | |||
67 | 18 | if (empty($config['default_index'])) { |
|
68 | 18 | $keys = array_keys($config['indexes']); |
|
69 | 18 | $config['default_index'] = reset($keys); |
|
70 | } |
||
71 | |||
72 | 18 | if (isset($config['serializer'])) { |
|
73 | 1 | $loader->load('serializer.xml'); |
|
74 | |||
75 | 1 | $this->loadSerializer($config['serializer'], $container); |
|
76 | } |
||
77 | |||
78 | 18 | $this->loadClients($config['clients'], $container); |
|
79 | 18 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
80 | |||
81 | 18 | $this->loadIndexes($config['indexes'], $container); |
|
82 | 18 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
83 | 18 | $container->setParameter('fos_elastica.default_index', $config['default_index']); |
|
84 | |||
85 | 18 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
86 | |||
87 | 18 | $this->loadIndexManager($container); |
|
88 | |||
89 | 18 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
90 | 18 | } |
|
91 | |||
92 | /** |
||
93 | * @param array $config |
||
94 | * @param ContainerBuilder $container |
||
95 | * |
||
96 | * @return Configuration |
||
97 | */ |
||
98 | 18 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
102 | |||
103 | /** |
||
104 | * Loads the configured clients. |
||
105 | * |
||
106 | * @param array $clients An array of clients configurations |
||
107 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 18 | private function loadClients(array $clients, ContainerBuilder $container) |
|
134 | |||
135 | /** |
||
136 | * Loads the configured indexes. |
||
137 | * |
||
138 | * @param array $indexes An array of indexes configurations |
||
139 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
140 | * |
||
141 | * @throws \InvalidArgumentException |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 18 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
188 | |||
189 | /** |
||
190 | * Loads the configured index finders. |
||
191 | * |
||
192 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
193 | * @param string $name The index name |
||
194 | * @param Reference $index Reference to the related index |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
214 | |||
215 | /** |
||
216 | * Loads the configured types. |
||
217 | * |
||
218 | * @param array $types |
||
219 | * @param ContainerBuilder $container |
||
220 | * @param array $indexConfig |
||
221 | * @param array $indexableCallbacks |
||
222 | */ |
||
223 | 18 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
308 | |||
309 | /** |
||
310 | * Loads the optional provider and finder for a type. |
||
311 | * |
||
312 | * @param array $typeConfig |
||
313 | * @param ContainerBuilder $container |
||
314 | * @param Reference $typeRef |
||
315 | * @param string $indexName |
||
316 | * @param string $typeName |
||
317 | */ |
||
318 | 15 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
338 | |||
339 | /** |
||
340 | * Creates and loads an ElasticaToModelTransformer. |
||
341 | * |
||
342 | * @param array $typeConfig |
||
343 | * @param ContainerBuilder $container |
||
344 | * @param string $indexName |
||
345 | * @param string $typeName |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | 15 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
374 | |||
375 | /** |
||
376 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
377 | * |
||
378 | * @param array $typeConfig |
||
379 | * @param ContainerBuilder $container |
||
380 | * @param string $indexName |
||
381 | * @param string $typeName |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 15 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
404 | |||
405 | /** |
||
406 | * Creates and loads an object persister for a type. |
||
407 | * |
||
408 | * @param array $typeConfig |
||
409 | * @param Reference $typeRef |
||
410 | * @param ContainerBuilder $container |
||
411 | * @param string $indexName |
||
412 | * @param string $typeName |
||
413 | * @param string $transformerId |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | 15 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
455 | |||
456 | /** |
||
457 | * Loads a pager provider for a type. |
||
458 | * |
||
459 | * @param array $typeConfig |
||
460 | * @param ContainerBuilder $container |
||
461 | * @param string $indexName |
||
462 | * @param string $typeName |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | 15 | private function loadTypePagerProvider(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
467 | { |
||
468 | 15 | if (isset($typeConfig['provider']['service'])) { |
|
469 | return $typeConfig['provider']['service']; |
||
470 | } |
||
471 | |||
472 | 15 | $baseConfig = $typeConfig['provider']; |
|
473 | 15 | unset($baseConfig['service']); |
|
474 | |||
475 | 15 | $driver = $typeConfig['driver']; |
|
476 | |||
477 | 15 | switch ($driver) { |
|
478 | case 'orm': |
||
479 | 9 | $providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
|
480 | 9 | $providerDef->replaceArgument(2, $typeConfig['model']); |
|
481 | 9 | $providerDef->replaceArgument(3, $baseConfig); |
|
482 | |||
483 | 9 | break; |
|
484 | View Code Duplication | case 'mongodb': |
|
485 | $providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
||
486 | $providerDef->replaceArgument(2, $typeConfig['model']); |
||
487 | $providerDef->replaceArgument(3, $baseConfig); |
||
488 | |||
489 | break; |
||
490 | View Code Duplication | case 'phpcr': |
|
491 | 1 | $providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
|
492 | 1 | $providerDef->replaceArgument(2, $typeConfig['model']); |
|
493 | 1 | $providerDef->replaceArgument(3, $baseConfig); |
|
494 | |||
495 | 1 | break; |
|
496 | View Code Duplication | case 'propel': |
|
497 | 5 | $providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
|
498 | 5 | $providerDef->replaceArgument(0, $typeConfig['model']); |
|
499 | 5 | $providerDef->replaceArgument(1, $baseConfig); |
|
500 | |||
501 | 5 | break; |
|
502 | default: |
||
503 | throw new \LogicException(sprintf('The pager provider for driver "%s" does not exist.', $driver)); |
||
504 | } |
||
505 | |||
506 | /* Note: provider services may conflict with "prototype.driver", if the |
||
507 | * index and type names were "prototype" and a driver, respectively. |
||
508 | */ |
||
509 | 15 | $providerId = sprintf('fos_elastica.pager_provider.%s.%s', $indexName, $typeName); |
|
510 | 15 | $providerDef->addTag('fos_elastica.pager_provider', ['index' => $indexName, 'type' => $typeName]); |
|
511 | |||
512 | 15 | $container->setDefinition($providerId, $providerDef); |
|
513 | |||
514 | 15 | return $providerId; |
|
515 | } |
||
516 | |||
517 | /** |
||
518 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
519 | * |
||
520 | * @param array $typeConfig |
||
521 | * @param ContainerBuilder $container |
||
522 | * @param string $objectPersisterId |
||
523 | * @param string $indexName |
||
524 | * @param string $typeName |
||
525 | * |
||
526 | * @return string |
||
527 | */ |
||
528 | 15 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
529 | { |
||
530 | 15 | if (isset($typeConfig['listener']['service'])) { |
|
531 | return $typeConfig['listener']['service']; |
||
532 | } |
||
533 | |||
534 | /* Note: listener services may conflict with "prototype.driver", if the |
||
535 | * index and type names were "prototype" and a driver, respectively. |
||
536 | */ |
||
537 | 15 | $abstractListenerId = sprintf('fos_elastica.listener.prototype.%s', $typeConfig['driver']); |
|
538 | 15 | $listenerId = sprintf('fos_elastica.listener.%s.%s', $indexName, $typeName); |
|
539 | 15 | $listenerDef = new ChildDefinition($abstractListenerId); |
|
540 | 15 | $listenerDef->replaceArgument(0, new Reference($objectPersisterId)); |
|
541 | 15 | $listenerDef->replaceArgument(3, $typeConfig['listener']['logger'] ? |
|
542 | new Reference($typeConfig['listener']['logger']) : |
||
543 | 15 | null |
|
544 | ); |
||
545 | $listenerConfig = [ |
||
546 | 15 | 'identifier' => $typeConfig['identifier'], |
|
547 | 15 | 'indexName' => $indexName, |
|
548 | 15 | 'typeName' => $typeName, |
|
549 | ]; |
||
550 | |||
551 | 15 | $tagName = null; |
|
552 | 15 | switch ($typeConfig['driver']) { |
|
553 | case 'orm': |
||
554 | 9 | $tagName = 'doctrine.event_listener'; |
|
555 | 9 | break; |
|
556 | case 'phpcr': |
||
557 | 1 | $tagName = 'doctrine_phpcr.event_listener'; |
|
558 | 1 | break; |
|
559 | case 'mongodb': |
||
560 | $tagName = 'doctrine_mongodb.odm.event_listener'; |
||
561 | break; |
||
562 | } |
||
563 | |||
564 | 15 | if ($typeConfig['listener']['defer']) { |
|
565 | $listenerDef->setPublic(true); |
||
566 | $listenerDef->addTag( |
||
567 | 'kernel.event_listener', |
||
568 | ['event' => 'kernel.terminate', 'method' => 'onTerminate'] |
||
569 | ); |
||
570 | $listenerDef->addTag( |
||
571 | 'kernel.event_listener', |
||
572 | ['event' => 'console.terminate', 'method' => 'onTerminate'] |
||
573 | ); |
||
574 | $listenerConfig['defer'] = true; |
||
575 | } |
||
576 | |||
577 | 15 | $listenerDef->replaceArgument(2, $listenerConfig); |
|
578 | |||
579 | 15 | if (null !== $tagName) { |
|
580 | 10 | foreach ($this->getDoctrineEvents($typeConfig) as $event) { |
|
581 | 10 | $listenerDef->addTag($tagName, ['event' => $event]); |
|
582 | } |
||
583 | } |
||
584 | |||
585 | 15 | $container->setDefinition($listenerId, $listenerDef); |
|
586 | |||
587 | 15 | return $listenerId; |
|
588 | } |
||
589 | |||
590 | /** |
||
591 | * Map Elastica to Doctrine events for the current driver. |
||
592 | */ |
||
593 | 10 | private function getDoctrineEvents(array $typeConfig) |
|
594 | { |
||
595 | 10 | switch ($typeConfig['driver']) { |
|
596 | case 'orm': |
||
597 | 9 | $eventsClass = '\Doctrine\ORM\Events'; |
|
598 | 9 | break; |
|
599 | case 'phpcr': |
||
600 | 1 | $eventsClass = '\Doctrine\ODM\PHPCR\Event'; |
|
601 | 1 | break; |
|
602 | case 'mongodb': |
||
603 | $eventsClass = '\Doctrine\ODM\MongoDB\Events'; |
||
604 | break; |
||
605 | default: |
||
606 | throw new \InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver'])); |
||
607 | } |
||
608 | |||
609 | 10 | $events = []; |
|
610 | $eventMapping = [ |
||
611 | 10 | 'insert' => [constant($eventsClass.'::postPersist')], |
|
612 | 10 | 'update' => [constant($eventsClass.'::postUpdate')], |
|
613 | 10 | 'delete' => [constant($eventsClass.'::preRemove')], |
|
614 | 10 | 'flush' => [constant($eventsClass.'::postFlush')], |
|
615 | ]; |
||
616 | |||
617 | 10 | foreach ($eventMapping as $event => $doctrineEvents) { |
|
618 | 10 | if (isset($typeConfig['listener'][$event]) && $typeConfig['listener'][$event]) { |
|
619 | 10 | $events = array_merge($events, $doctrineEvents); |
|
620 | } |
||
621 | } |
||
622 | |||
623 | 10 | return $events; |
|
624 | } |
||
625 | |||
626 | /** |
||
627 | * Loads a Type specific Finder. |
||
628 | * |
||
629 | * @param array $typeConfig |
||
630 | * @param ContainerBuilder $container |
||
631 | * @param string $elasticaToModelId |
||
632 | * @param Reference $typeRef |
||
633 | * @param string $indexName |
||
634 | * @param string $typeName |
||
635 | * |
||
636 | * @return string |
||
637 | */ |
||
638 | 15 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
665 | |||
666 | /** |
||
667 | * Loads the index manager. |
||
668 | * |
||
669 | * @param ContainerBuilder $container |
||
670 | **/ |
||
671 | private function loadIndexManager(ContainerBuilder $container) |
||
680 | |||
681 | /** |
||
682 | * Makes sure a specific driver has been loaded. |
||
683 | * |
||
684 | * @param ContainerBuilder $container |
||
685 | * @param string $driver |
||
686 | */ |
||
687 | 15 | private function loadDriver(ContainerBuilder $container, $driver) |
|
697 | |||
698 | /** |
||
699 | * Loads and configures the serializer prototype. |
||
700 | * |
||
701 | * @param array $config |
||
702 | * @param ContainerBuilder $container |
||
703 | */ |
||
704 | 1 | private function loadSerializer($config, ContainerBuilder $container) |
|
715 | |||
716 | /** |
||
717 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
718 | * |
||
719 | * @param string $defaultManager |
||
720 | * @param ContainerBuilder $container |
||
721 | */ |
||
722 | 18 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
738 | |||
739 | /** |
||
740 | * Returns a reference to a client given its configured name. |
||
741 | * |
||
742 | * @param string $clientName |
||
743 | * |
||
744 | * @return Reference |
||
745 | * |
||
746 | * @throws \InvalidArgumentException |
||
747 | */ |
||
748 | 2 | private function getClient($clientName) |
|
756 | } |
||
757 |