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 | 13 | public function load(array $configs, ContainerBuilder $container) |
|
| 38 | { |
||
| 39 | 13 | $configuration = $this->getConfiguration($configs, $container); |
|
| 40 | 13 | $config = $this->processConfiguration($configuration, $configs); |
|
| 41 | |||
| 42 | 13 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 43 | |||
| 44 | 13 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
| 45 | // No Clients or indexes are defined |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | 13 | foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) { |
|
| 50 | 13 | $loader->load(sprintf('%s.xml', $basename)); |
|
| 51 | } |
||
| 52 | |||
| 53 | 13 | if (empty($config['default_client'])) { |
|
| 54 | 13 | $keys = array_keys($config['clients']); |
|
| 55 | 13 | $config['default_client'] = reset($keys); |
|
| 56 | } |
||
| 57 | |||
| 58 | 13 | if (empty($config['default_index'])) { |
|
| 59 | 13 | $keys = array_keys($config['indexes']); |
|
| 60 | 13 | $config['default_index'] = reset($keys); |
|
| 61 | } |
||
| 62 | |||
| 63 | 13 | if (isset($config['serializer'])) { |
|
| 64 | 2 | $loader->load('serializer.xml'); |
|
| 65 | |||
| 66 | 2 | $this->loadSerializer($config['serializer'], $container); |
|
| 67 | } |
||
| 68 | |||
| 69 | 13 | $this->loadClients($config['clients'], $container); |
|
| 70 | 13 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
| 71 | |||
| 72 | 13 | $this->loadIndexes($config['indexes'], $container); |
|
| 73 | 13 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
| 74 | |||
| 75 | 13 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
| 76 | |||
| 77 | 13 | $this->loadIndexManager($container); |
|
| 78 | |||
| 79 | 13 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
| 80 | 13 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param array $config |
||
| 84 | * @param ContainerBuilder $container |
||
| 85 | * |
||
| 86 | * @return Configuration |
||
| 87 | */ |
||
| 88 | 13 | 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 | 13 | private function loadClients(array $clients, ContainerBuilder $container) |
|
| 102 | { |
||
| 103 | 13 | foreach ($clients as $name => $clientConfig) { |
|
| 104 | 13 | $clientId = sprintf('fos_elastica.client.%s', $name); |
|
| 105 | |||
| 106 | 13 | $clientDef = new DefinitionDecorator('fos_elastica.client_prototype'); |
|
| 107 | 13 | $clientDef->replaceArgument(0, $clientConfig); |
|
| 108 | |||
| 109 | 13 | $logger = $clientConfig['connections'][0]['logger']; |
|
| 110 | 13 | if (false !== $logger) { |
|
| 111 | 13 | $clientDef->addMethodCall('setLogger', array(new Reference($logger))); |
|
| 112 | } |
||
| 113 | 13 | $clientDef->addTag('fos_elastica.client'); |
|
| 114 | |||
| 115 | 13 | $container->setDefinition($clientId, $clientDef); |
|
| 116 | |||
| 117 | 13 | $this->clients[$name] = array( |
|
| 118 | 13 | 'id' => $clientId, |
|
| 119 | 13 | 'reference' => new Reference($clientId), |
|
| 120 | ); |
||
| 121 | } |
||
| 122 | 13 | } |
|
| 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 | 13 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
| 135 | { |
||
| 136 | 13 | $indexableCallbacks = array(); |
|
| 137 | |||
| 138 | 13 | foreach ($indexes as $name => $index) { |
|
| 139 | 13 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
| 140 | 13 | $indexName = isset($index['index_name']) ? $index['index_name'] : $name; |
|
| 141 | |||
| 142 | 13 | $indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); |
|
| 143 | 13 | $indexDef->replaceArgument(0, $indexName); |
|
| 144 | 13 | $indexDef->addTag('fos_elastica.index', array( |
|
| 145 | 13 | 'name' => $name, |
|
| 146 | )); |
||
| 147 | |||
| 148 | 13 | if (method_exists($indexDef, 'setFactory')) { |
|
| 149 | 13 | $indexDef->setFactory(array(new Reference('fos_elastica.client'), 'getIndex')); |
|
| 150 | } else { |
||
| 151 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
| 152 | $indexDef->setFactoryService('fos_elastica.client'); |
||
| 153 | $indexDef->setFactoryMethod('getIndex'); |
||
| 154 | } |
||
| 155 | |||
| 156 | 13 | if (isset($index['client'])) { |
|
| 157 | 2 | $client = $this->getClient($index['client']); |
|
| 158 | |||
| 159 | 2 | if (method_exists($indexDef, 'setFactory')) { |
|
| 160 | 2 | $indexDef->setFactory(array($client, 'getIndex')); |
|
| 161 | } else { |
||
| 162 | // To be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
| 163 | $indexDef->setFactoryService($client); |
||
| 164 | $indexDef->setFactoryMethod('getIndex'); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | 13 | $container->setDefinition($indexId, $indexDef); |
|
| 169 | 13 | $reference = new Reference($indexId); |
|
| 170 | |||
| 171 | 13 | $this->indexConfigs[$name] = array( |
|
| 172 | 13 | 'elasticsearch_name' => $indexName, |
|
| 173 | 13 | 'reference' => $reference, |
|
| 174 | 13 | 'name' => $name, |
|
| 175 | 13 | 'settings' => $index['settings'], |
|
| 176 | 13 | 'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : array(), |
|
| 177 | 13 | 'use_alias' => $index['use_alias'], |
|
| 178 | ); |
||
| 179 | |||
| 180 | 13 | if ($index['finder']) { |
|
| 181 | $this->loadIndexFinder($container, $name, $reference); |
||
| 182 | } |
||
| 183 | |||
| 184 | 13 | $this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name], $indexableCallbacks); |
|
| 185 | } |
||
| 186 | |||
| 187 | 13 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
| 188 | 13 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
| 189 | 13 | } |
|
| 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 | 13 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Loads the optional provider and finder for a type. |
||
| 317 | * |
||
| 318 | * @param array $typeConfig |
||
| 319 | * @param ContainerBuilder $container |
||
| 320 | * @param Reference $typeRef |
||
| 321 | * @param string $indexName |
||
| 322 | * @param string $typeName |
||
| 323 | */ |
||
| 324 | 9 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Creates and loads an ElasticaToModelTransformer. |
||
| 347 | * |
||
| 348 | * @param array $typeConfig |
||
| 349 | * @param ContainerBuilder $container |
||
| 350 | * @param string $indexName |
||
| 351 | * @param string $typeName |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 9 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
| 383 | * |
||
| 384 | * @param array $typeConfig |
||
| 385 | * @param ContainerBuilder $container |
||
| 386 | * @param string $indexName |
||
| 387 | * @param string $typeName |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 9 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Creates and loads an object persister for a type. |
||
| 413 | * |
||
| 414 | * @param array $typeConfig |
||
| 415 | * @param Reference $typeRef |
||
| 416 | * @param ContainerBuilder $container |
||
| 417 | * @param string $indexName |
||
| 418 | * @param string $typeName |
||
| 419 | * @param string $transformerId |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 9 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Loads a provider for a type. |
||
| 462 | * |
||
| 463 | * @param array $typeConfig |
||
| 464 | * @param ContainerBuilder $container |
||
| 465 | * @param string $objectPersisterId |
||
| 466 | * @param string $indexName |
||
| 467 | * @param string $typeName |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | 5 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
| 497 | * |
||
| 498 | * @param array $typeConfig |
||
| 499 | * @param ContainerBuilder $container |
||
| 500 | * @param string $objectPersisterId |
||
| 501 | * @param string $indexName |
||
| 502 | * @param string $typeName |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | 5 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Map Elastica to Doctrine events for the current driver. |
||
| 555 | */ |
||
| 556 | 5 | private function getDoctrineEvents(array $typeConfig) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Loads a Type specific Finder. |
||
| 591 | * |
||
| 592 | * @param array $typeConfig |
||
| 593 | * @param ContainerBuilder $container |
||
| 594 | * @param string $elasticaToModelId |
||
| 595 | * @param Reference $typeRef |
||
| 596 | * @param string $indexName |
||
| 597 | * @param string $typeName |
||
| 598 | * |
||
| 599 | * @return string |
||
| 600 | */ |
||
| 601 | 5 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Loads the index manager. |
||
| 626 | * |
||
| 627 | * @param ContainerBuilder $container |
||
| 628 | **/ |
||
| 629 | private function loadIndexManager(ContainerBuilder $container) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Makes sure a specific driver has been loaded. |
||
| 641 | * |
||
| 642 | * @param ContainerBuilder $container |
||
| 643 | * @param string $driver |
||
| 644 | */ |
||
| 645 | 8 | private function loadDriver(ContainerBuilder $container, $driver) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Loads and configures the serializer prototype. |
||
| 658 | * |
||
| 659 | * @param array $config |
||
| 660 | * @param ContainerBuilder $container |
||
| 661 | */ |
||
| 662 | 2 | private function loadSerializer($config, ContainerBuilder $container) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
| 677 | * |
||
| 678 | * @param string $defaultManager |
||
| 679 | * @param ContainerBuilder $container |
||
| 680 | */ |
||
| 681 | 13 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Returns a reference to a client given its configured name. |
||
| 700 | * |
||
| 701 | * @param string $clientName |
||
| 702 | * |
||
| 703 | * @return Reference |
||
| 704 | * |
||
| 705 | * @throws \InvalidArgumentException |
||
| 706 | */ |
||
| 707 | 2 | private function getClient($clientName) |
|
| 715 | } |
||
| 716 |