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 |
||
| 28 | class FOSElasticaExtension extends Extension |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Definition of elastica clients as configured by this extension. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $clients = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * An array of indexes as configured by the extension. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $indexConfigs = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * An array of index templates as configured by the extension. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $indexTemplateConfigs = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
| 53 | * here. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $loadedDrivers = []; |
||
| 58 | |||
| 59 | 21 | public function load(array $configs, ContainerBuilder $container) |
|
| 60 | { |
||
| 61 | 21 | $configuration = $this->getConfiguration($configs, $container); |
|
| 62 | 21 | $config = $this->processConfiguration($configuration, $configs); |
|
| 63 | |||
| 64 | 21 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 65 | |||
| 66 | 21 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
| 67 | // No Clients or indexes are defined |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | 21 | foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener', 'commands'] as $basename) { |
|
| 72 | 21 | $loader->load(sprintf('%s.xml', $basename)); |
|
| 73 | } |
||
| 74 | |||
| 75 | 21 | if (empty($config['default_client'])) { |
|
| 76 | 21 | $keys = array_keys($config['clients']); |
|
| 77 | 21 | $config['default_client'] = reset($keys); |
|
| 78 | } |
||
| 79 | |||
| 80 | 21 | if (empty($config['default_index'])) { |
|
| 81 | 21 | $keys = array_keys($config['indexes']); |
|
| 82 | 21 | $config['default_index'] = reset($keys); |
|
| 83 | } |
||
| 84 | |||
| 85 | 21 | if ($this->isConfigEnabled($container, $config['messenger'])) { |
|
| 86 | $this->registerMessengerConfiguration($config['messenger'], $container, $loader); |
||
| 87 | } |
||
| 88 | |||
| 89 | 21 | if (isset($config['serializer'])) { |
|
| 90 | 1 | $loader->load('serializer.xml'); |
|
| 91 | |||
| 92 | 1 | $this->loadSerializer($config['serializer'], $container); |
|
| 93 | } |
||
| 94 | |||
| 95 | 21 | $this->loadClients($config['clients'], $container); |
|
| 96 | 21 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
| 97 | 21 | $container->getAlias('fos_elastica.client')->setPublic(true); |
|
| 98 | 21 | $container->setAlias(ElasticaClient::class, new Alias('fos_elastica.client', false)); |
|
| 99 | 21 | $container->setAlias(Client::class, 'fos_elastica.client'); |
|
| 100 | 21 | $container->getAlias(Client::class)->setPublic(false); |
|
| 101 | |||
| 102 | 21 | $this->loadIndexes($config['indexes'], $container); |
|
| 103 | 21 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
| 104 | 21 | $container->getAlias('fos_elastica.index')->setPublic(true); |
|
| 105 | 21 | $container->setParameter('fos_elastica.default_index', $config['default_index']); |
|
| 106 | |||
| 107 | 21 | if ($usedIndexNames = \array_intersect_key($config['indexes'], $config['index_templates'])) { |
|
| 108 | throw new \DomainException(\sprintf('Index names "%s" are already in use and can not be used for index templates names', \implode('","', \array_keys($usedIndexNames)))); |
||
| 109 | } |
||
| 110 | 21 | $this->loadIndexTemplates($config['index_templates'], $container); |
|
| 111 | |||
| 112 | 21 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
| 113 | $container |
||
| 114 | 21 | ->getDefinition('fos_elastica.config_source.template_container') |
|
| 115 | 21 | ->replaceArgument(0, $this->indexTemplateConfigs); |
|
| 116 | |||
| 117 | 21 | $this->loadIndexManager($container); |
|
| 118 | 21 | $this->loadIndexTemplateManager($container); |
|
| 119 | |||
| 120 | 21 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
| 121 | 21 | } |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return Configuration |
||
| 125 | */ |
||
| 126 | 21 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 127 | { |
||
| 128 | 21 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Loads the configured clients. |
||
| 133 | * |
||
| 134 | * @param array $clients An array of clients configurations |
||
| 135 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | 21 | private function loadClients(array $clients, ContainerBuilder $container) |
|
| 140 | { |
||
| 141 | 21 | foreach ($clients as $name => $clientConfig) { |
|
| 142 | 21 | $clientId = sprintf('fos_elastica.client.%s', $name); |
|
| 143 | |||
| 144 | 21 | $clientDef = new ChildDefinition('fos_elastica.client_prototype'); |
|
| 145 | 21 | $clientDef->replaceArgument(0, $clientConfig); |
|
| 146 | 21 | $clientDef->replaceArgument(1, null); |
|
| 147 | |||
| 148 | 21 | $logger = $clientConfig['connections'][0]['logger']; |
|
| 149 | 21 | if (false !== $logger) { |
|
| 150 | 21 | $clientDef->addMethodCall('setLogger', [new Reference($logger)]); |
|
| 151 | } |
||
| 152 | |||
| 153 | 21 | $clientDef->addTag('fos_elastica.client'); |
|
| 154 | |||
| 155 | 21 | $container->setDefinition($clientId, $clientDef); |
|
| 156 | |||
| 157 | 21 | $this->clients[$name] = [ |
|
| 158 | 21 | 'id' => $clientId, |
|
| 159 | 21 | 'reference' => new Reference($clientId), |
|
| 160 | ]; |
||
| 161 | } |
||
| 162 | 21 | } |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Loads the configured indexes. |
||
| 166 | * |
||
| 167 | * @param array $indexes An array of indexes configurations |
||
| 168 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 169 | * |
||
| 170 | * @throws \InvalidArgumentException |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 21 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
| 175 | { |
||
| 176 | 21 | $indexableCallbacks = []; |
|
| 177 | |||
| 178 | 21 | foreach ($indexes as $name => $index) { |
|
| 179 | 21 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
| 180 | 21 | $indexName = $index['index_name'] ?? $name; |
|
| 181 | |||
| 182 | 21 | $indexDef = new ChildDefinition('fos_elastica.index_prototype'); |
|
| 183 | 21 | $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndex']); |
|
| 184 | 21 | $indexDef->replaceArgument(0, $indexName); |
|
| 185 | 21 | $indexDef->addTag('fos_elastica.index', [ |
|
| 186 | 21 | 'name' => $name, |
|
| 187 | ]); |
||
| 188 | |||
| 189 | 21 | View Code Duplication | if (isset($index['client'])) { |
| 190 | 1 | $client = $this->getClient($index['client']); |
|
| 191 | |||
| 192 | 1 | $indexDef->setFactory([$client, 'getIndex']); |
|
| 193 | } |
||
| 194 | |||
| 195 | 21 | $container->setDefinition($indexId, $indexDef); |
|
| 196 | 21 | $reference = new Reference($indexId); |
|
| 197 | |||
| 198 | 21 | $this->indexConfigs[$name] = [ |
|
| 199 | 21 | 'elasticsearch_name' => $indexName, |
|
| 200 | 21 | 'reference' => $reference, |
|
| 201 | 21 | 'model' => $index['persistence']['model'] ?? null, |
|
| 202 | 21 | 'name' => $name, |
|
| 203 | 21 | 'settings' => $index['settings'], |
|
| 204 | 21 | 'index_prototype' => $index['index_prototype'] ?? [], |
|
| 205 | 21 | 'use_alias' => $index['use_alias'], |
|
| 206 | ]; |
||
| 207 | |||
| 208 | 21 | if ($index['finder']) { |
|
| 209 | $this->loadIndexFinder($container, $name, $reference); |
||
| 210 | } |
||
| 211 | |||
| 212 | 21 | $this->loadIndexConfig((array) $index, $this->indexConfigs[$name]); |
|
| 213 | |||
| 214 | 21 | if (isset($index['indexable_callback'])) { |
|
| 215 | 4 | $indexableCallbacks[$name] = $this->buildCallback($index['indexable_callback'], $name); |
|
| 216 | } |
||
| 217 | |||
| 218 | 21 | $this->loadIndexSerializerIntegration($index['serializer'] ?? [], $container, $reference); |
|
| 219 | |||
| 220 | 21 | if (isset($index['persistence'])) { |
|
| 221 | 15 | $this->loadIndexPersistenceIntegration($index['persistence'], $container, $reference, $name); |
|
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | 21 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
| 226 | 21 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
| 227 | 21 | } |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Loads the configured indexes. |
||
| 231 | * |
||
| 232 | * @param array $indexTemplates An array of indexes configurations |
||
| 233 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 234 | * |
||
| 235 | * @throws \InvalidArgumentException |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | 21 | private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $container) |
|
| 240 | { |
||
| 241 | 21 | foreach ($indexTemplates as $name => $indexTemplate) { |
|
| 242 | 6 | $indexId = sprintf('fos_elastica.index_template.%s', $name); |
|
| 243 | 6 | $indexTemplateName = $indexTemplate['template_name'] ?? $name; |
|
| 244 | |||
| 245 | 6 | $indexDef = new ChildDefinition('fos_elastica.index_template_prototype'); |
|
| 246 | 6 | $indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndexTemplate']); |
|
| 247 | 6 | $indexDef->replaceArgument(0, $indexTemplateName); |
|
| 248 | 6 | $indexDef->addTag('fos_elastica.index_template', [ |
|
| 249 | 6 | 'name' => $name, |
|
| 250 | ]); |
||
| 251 | |||
| 252 | 6 | View Code Duplication | if (isset($indexTemplate['client'])) { |
| 253 | 6 | $client = $this->getClient($indexTemplate['client']); |
|
| 254 | 6 | $indexDef->setFactory([$client, 'getIndexTemplate']); |
|
| 255 | } |
||
| 256 | |||
| 257 | 6 | $container->setDefinition($indexId, $indexDef); |
|
| 258 | 6 | $reference = new Reference($indexId); |
|
| 259 | |||
| 260 | 6 | $this->indexTemplateConfigs[$name] = [ |
|
| 261 | 6 | 'elasticsearch_name' => $indexTemplateName, |
|
| 262 | 6 | 'reference' => $reference, |
|
| 263 | 6 | 'name' => $name, |
|
| 264 | 6 | 'settings' => $indexTemplate['settings'], |
|
| 265 | 6 | 'template' => $indexTemplate['template'], |
|
| 266 | ]; |
||
| 267 | |||
| 268 | 6 | $this->loadIndexConfig((array) $indexTemplate, $this->indexTemplateConfigs[$name]); |
|
| 269 | } |
||
| 270 | 21 | } |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Loads the configured index finders. |
||
| 274 | * |
||
| 275 | * @param string $name The index name |
||
| 276 | * @param Reference $index Reference to the related index |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | private function loadIndexFinder(ContainerBuilder $container, string $name, Reference $index): void |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Loads the configured $index. |
||
| 292 | */ |
||
| 293 | 21 | private function loadIndexConfig(array $index, array &$indexConfig): void |
|
| 294 | { |
||
| 323 | |||
| 324 | 4 | private function buildCallback($indexCallback, $indexName) |
|
| 349 | |||
| 350 | 4 | private function transformServiceReference($classOrService) |
|
| 354 | |||
| 355 | 21 | private function loadIndexSerializerIntegration(array $config, ContainerBuilder $container, Reference $indexRef): void |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Loads the optional provider and finder for a type. |
||
| 379 | */ |
||
| 380 | 15 | private function loadIndexPersistenceIntegration(array $config, ContainerBuilder $container, Reference $indexRef, string $indexName): void |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Creates and loads an ElasticaToModelTransformer. |
||
| 403 | */ |
||
| 404 | 15 | private function loadElasticaToModelTransformer(array $persistenceConfig, ContainerBuilder $container, string $indexName): string |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Creates and loads a ModelToElasticaTransformer for an index. |
||
| 429 | */ |
||
| 430 | 15 | private function loadModelToElasticaTransformer(array $config, ContainerBuilder $container, string $indexName): string |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Creates and loads an object persister for a index. |
||
| 453 | */ |
||
| 454 | 15 | private function loadObjectPersister(array $config, Reference $indexRef, ContainerBuilder $container, string $indexName, string $transformerId): string |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Loads a pager provider for a index. |
||
| 494 | */ |
||
| 495 | 15 | private function loadIndexPagerProvider(array $indexConfig, ContainerBuilder $container, string $indexName): string |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
| 531 | */ |
||
| 532 | 14 | private function loadIndexListener(array $indexConfig, ContainerBuilder $container, string $objectPersisterId, string $indexName): string |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Map Elastica to Doctrine events for the current driver. |
||
| 595 | */ |
||
| 596 | 14 | private function getDoctrineEvents(array $indexConfig) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Loads a Type specific Finder. |
||
| 631 | */ |
||
| 632 | 15 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, string $elasticaToModelId, Reference $indexRef, string $indexName): string |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Loads the index manager. |
||
| 661 | **/ |
||
| 662 | 21 | View Code Duplication | private function loadIndexManager(ContainerBuilder $container): void |
| 671 | |||
| 672 | /** |
||
| 673 | * Load index template manager. |
||
| 674 | */ |
||
| 675 | 21 | View Code Duplication | private function loadIndexTemplateManager(ContainerBuilder $container): void |
| 684 | |||
| 685 | /** |
||
| 686 | * Makes sure a specific driver has been loaded. |
||
| 687 | */ |
||
| 688 | 15 | private function loadDriver(ContainerBuilder $container, string $driver): void |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Loads and configures the serializer prototype. |
||
| 701 | */ |
||
| 702 | 1 | private function loadSerializer(array $config, ContainerBuilder $container): void |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
| 716 | */ |
||
| 717 | 21 | private function createDefaultManagerAlias(string $defaultManager, ContainerBuilder $container): void |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Returns a reference to a client given its configured name. |
||
| 737 | * |
||
| 738 | * @throws \InvalidArgumentException |
||
| 739 | */ |
||
| 740 | 7 | private function getClient(string $clientName): Reference |
|
| 748 | |||
| 749 | private function registerMessengerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader): void |
||
| 761 | } |
||
| 762 |