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 |
||
| 24 | class FOSElasticaExtension extends Extension |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Definition of elastica clients as configured by this extension. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $clients = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An array of indexes as configured by the extension. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $indexConfigs = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
| 42 | * here. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $loadedDrivers = []; |
||
| 47 | |||
| 48 | 17 | public function load(array $configs, ContainerBuilder $container) |
|
| 49 | { |
||
| 50 | 17 | $configuration = $this->getConfiguration($configs, $container); |
|
| 51 | 17 | $config = $this->processConfiguration($configuration, $configs); |
|
| 52 | |||
| 53 | 17 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 54 | |||
| 55 | 17 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
| 56 | // No Clients or indexes are defined |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | 17 | foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener', 'commands'] as $basename) { |
|
| 61 | 17 | $loader->load(sprintf('%s.xml', $basename)); |
|
| 62 | } |
||
| 63 | |||
| 64 | 17 | if (empty($config['default_client'])) { |
|
| 65 | 17 | $keys = array_keys($config['clients']); |
|
| 66 | 17 | $config['default_client'] = reset($keys); |
|
| 67 | } |
||
| 68 | |||
| 69 | 17 | if (empty($config['default_index'])) { |
|
| 70 | 17 | $keys = array_keys($config['indexes']); |
|
| 71 | 17 | $config['default_index'] = reset($keys); |
|
| 72 | } |
||
| 73 | |||
| 74 | 17 | if (isset($config['serializer'])) { |
|
| 75 | $loader->load('serializer.xml'); |
||
| 76 | |||
| 77 | $this->loadSerializer($config['serializer'], $container); |
||
| 78 | } |
||
| 79 | |||
| 80 | 17 | $this->loadClients($config['clients'], $container); |
|
| 81 | 17 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
| 82 | 17 | $container->getAlias('fos_elastica.client')->setPublic(true); |
|
| 83 | 17 | $container->setAlias(Client::class, 'fos_elastica.client'); |
|
| 84 | 17 | $container->getAlias(Client::class)->setPublic(false); |
|
| 85 | |||
| 86 | 17 | $this->loadIndexes($config['indexes'], $container); |
|
| 87 | 17 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
| 88 | 17 | $container->getAlias('fos_elastica.index')->setPublic(true); |
|
| 89 | 17 | $container->setParameter('fos_elastica.default_index', $config['default_index']); |
|
| 90 | |||
| 91 | 17 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
| 92 | |||
| 93 | 17 | $this->loadIndexManager($container); |
|
| 94 | |||
| 95 | 17 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
| 96 | 17 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $config |
||
| 100 | * @param ContainerBuilder $container |
||
| 101 | * |
||
| 102 | * @return Configuration |
||
| 103 | */ |
||
| 104 | 17 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Loads the configured clients. |
||
| 111 | * |
||
| 112 | * @param array $clients An array of clients configurations |
||
| 113 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | 17 | private function loadClients(array $clients, ContainerBuilder $container) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Loads the configured indexes. |
||
| 143 | * |
||
| 144 | * @param array $indexes An array of indexes configurations |
||
| 145 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 146 | * |
||
| 147 | * @throws \InvalidArgumentException |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 17 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Loads the configured index finders. |
||
| 197 | * |
||
| 198 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 199 | * @param string $name The index name |
||
| 200 | * @param Reference $index Reference to the related index |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Loads the configured types. |
||
| 223 | * |
||
| 224 | * @param array $types |
||
| 225 | * @param ContainerBuilder $container |
||
| 226 | * @param array $indexConfig |
||
| 227 | * @param array $indexableCallbacks |
||
| 228 | */ |
||
| 229 | 17 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
| 230 | { |
||
| 231 | 17 | foreach ($types as $name => $type) { |
|
| 232 | 17 | $indexName = $indexConfig['name']; |
|
| 233 | |||
| 234 | 17 | $typeId = sprintf('%s.%s', $indexConfig['reference'], $name); |
|
| 235 | 17 | $typeDef = new ChildDefinition('fos_elastica.type_prototype'); |
|
| 236 | 17 | $typeDef->setFactory([$indexConfig['reference'], 'getType']); |
|
| 237 | 17 | $typeDef->replaceArgument(0, $name); |
|
| 238 | |||
| 239 | 17 | $container->setDefinition($typeId, $typeDef); |
|
| 240 | |||
| 241 | $typeConfig = [ |
||
| 242 | 17 | 'name' => $name, |
|
| 243 | 'mapping' => [], // An array containing anything that gets sent directly to ElasticSearch |
||
| 244 | 'config' => [], |
||
| 245 | ]; |
||
| 246 | |||
| 247 | foreach ([ |
||
| 248 | 17 | 'dynamic_templates', |
|
| 249 | 'properties', |
||
| 250 | '_all', |
||
| 251 | '_id', |
||
| 252 | '_parent', |
||
| 253 | '_routing', |
||
| 254 | '_source', |
||
| 255 | ] as $field) { |
||
| 256 | 17 | if (isset($type[$field])) { |
|
| 257 | 17 | $typeConfig['mapping'][$field] = $type[$field]; |
|
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | foreach ([ |
||
| 262 | 17 | 'persistence', |
|
| 263 | 'serializer', |
||
| 264 | 'analyzer', |
||
| 265 | 'search_analyzer', |
||
| 266 | 'dynamic', |
||
| 267 | 'date_detection', |
||
| 268 | 'dynamic_date_formats', |
||
| 269 | 'numeric_detection', |
||
| 270 | ] as $field) { |
||
| 271 | 17 | $typeConfig['config'][$field] = array_key_exists($field, $type) ? |
|
| 272 | 17 | $type[$field] : |
|
| 273 | 17 | null; |
|
| 274 | } |
||
| 275 | |||
| 276 | 17 | $this->indexConfigs[$indexName]['types'][$name] = $typeConfig; |
|
| 277 | |||
| 278 | 17 | if (isset($type['persistence'])) { |
|
| 279 | 14 | $this->loadTypePersistenceIntegration($type['persistence'], $container, new Reference($typeId), $indexName, $name); |
|
| 280 | |||
| 281 | 14 | $typeConfig['persistence'] = $type['persistence']; |
|
| 282 | } |
||
| 283 | |||
| 284 | 17 | if (isset($type['_parent'])) { |
|
| 285 | // _parent mapping cannot contain `property` and `identifier`, so removing them after building `persistence` |
||
| 286 | 4 | unset($indexConfig['types'][$name]['mapping']['_parent']['property'], $indexConfig['types'][$name]['mapping']['_parent']['identifier']); |
|
| 287 | } |
||
| 288 | |||
| 289 | 17 | if (isset($type['indexable_callback'])) { |
|
| 290 | 4 | $indexableCallbacks[sprintf('%s/%s', $indexName, $name)] = $this->buildCallback($type['indexable_callback'], $name); |
|
| 291 | } |
||
| 292 | |||
| 293 | 17 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
| 294 | $typeSerializerId = sprintf('%s.serializer.callback', $typeId); |
||
| 295 | $typeSerializerDef = new ChildDefinition('fos_elastica.serializer_callback_prototype'); |
||
| 296 | |||
| 297 | if (isset($type['serializer']['groups'])) { |
||
| 298 | $typeSerializerDef->addMethodCall('setGroups', [$type['serializer']['groups']]); |
||
| 299 | } |
||
| 300 | |||
| 301 | if (isset($type['serializer']['serialize_null'])) { |
||
| 302 | $typeSerializerDef->addMethodCall('setSerializeNull', [$type['serializer']['serialize_null']]); |
||
| 303 | } |
||
| 304 | |||
| 305 | if (isset($type['serializer']['version'])) { |
||
| 306 | $typeSerializerDef->addMethodCall('setVersion', [$type['serializer']['version']]); |
||
| 307 | } |
||
| 308 | |||
| 309 | $typeDef->addMethodCall('setSerializer', [[new Reference($typeSerializerId), 'serialize']]); |
||
| 310 | 17 | $container->setDefinition($typeSerializerId, $typeSerializerDef); |
|
| 311 | } |
||
| 312 | } |
||
| 313 | 17 | } |
|
| 314 | |||
| 315 | 4 | private function buildCallback($indexCallback, $typeName) |
|
| 340 | |||
| 341 | 4 | private function transformServiceReference($classOrService) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Loads the optional provider and finder for a type. |
||
| 348 | * |
||
| 349 | * @param array $typeConfig |
||
| 350 | * @param ContainerBuilder $container |
||
| 351 | * @param Reference $typeRef |
||
| 352 | * @param string $indexName |
||
| 353 | * @param string $typeName |
||
| 354 | */ |
||
| 355 | 14 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Creates and loads an ElasticaToModelTransformer. |
||
| 378 | * |
||
| 379 | * @param array $typeConfig |
||
| 380 | * @param ContainerBuilder $container |
||
| 381 | * @param string $indexName |
||
| 382 | * @param string $typeName |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 14 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
| 414 | * |
||
| 415 | * @param array $typeConfig |
||
| 416 | * @param ContainerBuilder $container |
||
| 417 | * @param string $indexName |
||
| 418 | * @param string $typeName |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | 14 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 423 | { |
||
| 424 | 14 | if (isset($typeConfig['model_to_elastica_transformer']['service'])) { |
|
| 425 | return $typeConfig['model_to_elastica_transformer']['service']; |
||
| 426 | } |
||
| 427 | |||
| 428 | 14 | $abstractId = $container->hasDefinition('fos_elastica.serializer_callback_prototype') ? |
|
| 429 | 'fos_elastica.model_to_elastica_identifier_transformer' : |
||
| 430 | 14 | 'fos_elastica.model_to_elastica_transformer'; |
|
| 431 | |||
| 432 | 14 | $serviceId = sprintf('fos_elastica.model_to_elastica_transformer.%s.%s', $indexName, $typeName); |
|
| 433 | 14 | $serviceDef = new ChildDefinition($abstractId); |
|
| 434 | 14 | $serviceDef->replaceArgument(0, [ |
|
| 435 | 14 | 'identifier' => $typeConfig['identifier'], |
|
| 436 | ]); |
||
| 437 | 14 | $container->setDefinition($serviceId, $serviceDef); |
|
| 438 | |||
| 439 | 14 | return $serviceId; |
|
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Creates and loads an object persister for a type. |
||
| 444 | * |
||
| 445 | * @param array $typeConfig |
||
| 446 | * @param Reference $typeRef |
||
| 447 | * @param ContainerBuilder $container |
||
| 448 | * @param string $indexName |
||
| 449 | * @param string $typeName |
||
| 450 | * @param string $transformerId |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 14 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
| 455 | { |
||
| 456 | 14 | if (isset($typeConfig['persister']['service'])) { |
|
| 457 | 1 | return $typeConfig['persister']['service']; |
|
| 458 | } |
||
| 459 | |||
| 460 | $arguments = [ |
||
| 461 | 13 | $typeRef, |
|
| 462 | 13 | new Reference($transformerId), |
|
| 463 | 13 | $typeConfig['model'], |
|
| 464 | ]; |
||
| 465 | |||
| 466 | 13 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
| 467 | $abstractId = 'fos_elastica.object_serializer_persister'; |
||
| 468 | $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['reference'], $typeName); |
||
| 469 | $arguments[] = [new Reference($callbackId), 'serialize']; |
||
| 470 | } else { |
||
| 471 | 13 | $abstractId = 'fos_elastica.object_persister'; |
|
| 472 | 13 | $mapping = $this->indexConfigs[$indexName]['types'][$typeName]['mapping']; |
|
| 473 | 13 | $argument = $mapping['properties']; |
|
| 474 | 13 | if (isset($mapping['_parent'])) { |
|
| 475 | 1 | $argument['_parent'] = $mapping['_parent']; |
|
| 476 | } |
||
| 477 | 13 | $arguments[] = $argument; |
|
| 478 | } |
||
| 479 | |||
| 480 | 13 | $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); |
|
| 481 | 13 | $serviceDef = new ChildDefinition($abstractId); |
|
| 482 | 13 | foreach ($arguments as $i => $argument) { |
|
| 483 | 13 | $serviceDef->replaceArgument($i, $argument); |
|
| 484 | } |
||
| 485 | |||
| 486 | 13 | $serviceDef->addTag('fos_elastica.persister', ['index' => $indexName, 'type' => $typeName]); |
|
| 487 | |||
| 488 | 13 | $container->setDefinition($serviceId, $serviceDef); |
|
| 489 | |||
| 490 | 13 | return $serviceId; |
|
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Loads a pager provider for a type. |
||
| 495 | * |
||
| 496 | * @param array $typeConfig |
||
| 497 | * @param ContainerBuilder $container |
||
| 498 | * @param string $indexName |
||
| 499 | * @param string $typeName |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | 14 | private function loadTypePagerProvider(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
| 556 | * |
||
| 557 | * @param array $typeConfig |
||
| 558 | * @param ContainerBuilder $container |
||
| 559 | * @param string $objectPersisterId |
||
| 560 | * @param string $indexName |
||
| 561 | * @param string $typeName |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | 14 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Map Elastica to Doctrine events for the current driver. |
||
| 629 | */ |
||
| 630 | 9 | private function getDoctrineEvents(array $typeConfig) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Loads a Type specific Finder. |
||
| 665 | * |
||
| 666 | * @param array $typeConfig |
||
| 667 | * @param ContainerBuilder $container |
||
| 668 | * @param string $elasticaToModelId |
||
| 669 | * @param Reference $typeRef |
||
| 670 | * @param string $indexName |
||
| 671 | * @param string $typeName |
||
| 672 | * |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | 14 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Loads the index manager. |
||
| 705 | * |
||
| 706 | * @param ContainerBuilder $container |
||
| 707 | **/ |
||
| 708 | private function loadIndexManager(ContainerBuilder $container) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Makes sure a specific driver has been loaded. |
||
| 720 | * |
||
| 721 | * @param ContainerBuilder $container |
||
| 722 | * @param string $driver |
||
| 723 | */ |
||
| 724 | 14 | private function loadDriver(ContainerBuilder $container, $driver) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Loads and configures the serializer prototype. |
||
| 737 | * |
||
| 738 | * @param array $config |
||
| 739 | * @param ContainerBuilder $container |
||
| 740 | */ |
||
| 741 | private function loadSerializer($config, ContainerBuilder $container) |
||
| 742 | { |
||
| 743 | $container->setAlias('fos_elastica.serializer', $config['serializer']); |
||
| 744 | |||
| 745 | $serializer = $container->getDefinition('fos_elastica.serializer_callback_prototype'); |
||
| 746 | $serializer->setClass($config['callback_class']); |
||
| 747 | |||
| 748 | if (is_subclass_of($config['callback_class'], ContainerAwareInterface::class)) { |
||
|
|
|||
| 749 | $serializer->addMethodCall('setContainer', [new Reference('service_container')]); |
||
| 750 | } |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
| 755 | * |
||
| 756 | * @param string $defaultManager |
||
| 757 | * @param ContainerBuilder $container |
||
| 758 | */ |
||
| 759 | 17 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Returns a reference to a client given its configured name. |
||
| 781 | * |
||
| 782 | * @param string $clientName |
||
| 783 | * |
||
| 784 | * @return Reference |
||
| 785 | * |
||
| 786 | * @throws \InvalidArgumentException |
||
| 787 | */ |
||
| 788 | 2 | private function getClient($clientName) |
|
| 796 | } |
||
| 797 |