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 | 14 | public function load(array $configs, ContainerBuilder $container) |
|
38 | { |
||
39 | 14 | $configuration = $this->getConfiguration($configs, $container); |
|
40 | 14 | $config = $this->processConfiguration($configuration, $configs); |
|
41 | |||
42 | 14 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
43 | |||
44 | 14 | if (empty($config['clients']) || empty($config['indexes'])) { |
|
45 | // No Clients or indexes are defined |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | 14 | foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) { |
|
50 | 14 | $loader->load(sprintf('%s.xml', $basename)); |
|
51 | } |
||
52 | |||
53 | 14 | if (empty($config['default_client'])) { |
|
54 | 14 | $keys = array_keys($config['clients']); |
|
55 | 14 | $config['default_client'] = reset($keys); |
|
56 | } |
||
57 | |||
58 | 14 | if (empty($config['default_index'])) { |
|
59 | 14 | $keys = array_keys($config['indexes']); |
|
60 | 14 | $config['default_index'] = reset($keys); |
|
61 | } |
||
62 | |||
63 | 14 | if (isset($config['serializer'])) { |
|
64 | 3 | $loader->load('serializer.xml'); |
|
65 | |||
66 | 3 | $this->loadSerializer($config['serializer'], $container); |
|
67 | } |
||
68 | |||
69 | 14 | $this->loadClients($config['clients'], $container); |
|
70 | 14 | $container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
|
71 | |||
72 | 14 | $this->loadIndexes($config['indexes'], $container); |
|
73 | 14 | $container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
|
74 | |||
75 | 14 | $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
|
76 | |||
77 | 14 | $this->loadIndexManager($container); |
|
78 | |||
79 | 14 | $this->createDefaultManagerAlias($config['default_manager'], $container); |
|
80 | 14 | } |
|
81 | |||
82 | /** |
||
83 | * @param array $config |
||
84 | * @param ContainerBuilder $container |
||
85 | * |
||
86 | * @return Configuration |
||
87 | */ |
||
88 | 14 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
89 | { |
||
90 | 14 | return new Configuration($container->getParameter('kernel.debug')); |
|
91 | } |
||
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 | 14 | private function loadClients(array $clients, ContainerBuilder $container) |
|
124 | |||
125 | /** |
||
126 | * Loads the configured indexes. |
||
127 | * |
||
128 | * @param array $indexes An array of indexes configurations |
||
129 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 14 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
178 | |||
179 | /** |
||
180 | * Loads the configured index finders. |
||
181 | * |
||
182 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
183 | * @param string $name The index name |
||
184 | * @param Reference $index Reference to the related index |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
204 | |||
205 | /** |
||
206 | * Loads the configured types. |
||
207 | * |
||
208 | * @param array $types |
||
209 | * @param ContainerBuilder $container |
||
210 | * @param array $indexConfig |
||
211 | * @param array $indexableCallbacks |
||
212 | */ |
||
213 | 14 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
301 | |||
302 | /** |
||
303 | * Loads the optional provider and finder for a type. |
||
304 | * |
||
305 | * @param array $typeConfig |
||
306 | * @param ContainerBuilder $container |
||
307 | * @param Reference $typeRef |
||
308 | * @param string $indexName |
||
309 | * @param string $typeName |
||
310 | */ |
||
311 | 10 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
312 | { |
||
313 | 10 | if (isset($typeConfig['driver'])) { |
|
314 | 10 | $this->loadDriver($container, $typeConfig['driver']); |
|
315 | } |
||
316 | |||
317 | 10 | $elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($typeConfig, $container, $indexName, $typeName); |
|
318 | 10 | $modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($typeConfig, $container, $indexName, $typeName); |
|
319 | 10 | $objectPersisterId = $this->loadObjectPersister($typeConfig, $typeRef, $container, $indexName, $typeName, $modelToElasticaTransformerId); |
|
320 | |||
321 | 10 | if (isset($typeConfig['provider'])) { |
|
322 | 10 | $this->loadTypeProvider($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
323 | } |
||
324 | 10 | if (isset($typeConfig['finder'])) { |
|
325 | 10 | $this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName); |
|
326 | } |
||
327 | 10 | if (isset($typeConfig['listener'])) { |
|
328 | 10 | $this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
329 | } |
||
330 | 10 | } |
|
331 | |||
332 | /** |
||
333 | * Creates and loads an ElasticaToModelTransformer. |
||
334 | * |
||
335 | * @param array $typeConfig |
||
336 | * @param ContainerBuilder $container |
||
337 | * @param string $indexName |
||
338 | * @param string $typeName |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | 10 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
367 | |||
368 | /** |
||
369 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
370 | * |
||
371 | * @param array $typeConfig |
||
372 | * @param ContainerBuilder $container |
||
373 | * @param string $indexName |
||
374 | * @param string $typeName |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | 10 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
397 | |||
398 | /** |
||
399 | * Creates and loads an object persister for a type. |
||
400 | * |
||
401 | * @param array $typeConfig |
||
402 | * @param Reference $typeRef |
||
403 | * @param ContainerBuilder $container |
||
404 | * @param string $indexName |
||
405 | * @param string $typeName |
||
406 | * @param string $transformerId |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 10 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
411 | { |
||
412 | 10 | if (isset($typeConfig['persister']['service'])) { |
|
413 | 1 | return $typeConfig['persister']['service']; |
|
414 | } |
||
415 | |||
416 | $arguments = array( |
||
417 | 9 | $typeRef, |
|
418 | 9 | new Reference($transformerId), |
|
419 | 9 | $typeConfig['model'], |
|
420 | ); |
||
421 | |||
422 | 9 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
423 | 3 | $abstractId = 'fos_elastica.object_serializer_persister'; |
|
424 | 3 | $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['reference'], $typeName); |
|
425 | 3 | $arguments[] = array(new Reference($callbackId), 'serialize'); |
|
426 | } else { |
||
427 | 6 | $abstractId = 'fos_elastica.object_persister'; |
|
428 | 6 | $mapping = $this->indexConfigs[$indexName]['types'][$typeName]['mapping']; |
|
429 | 6 | $argument = $mapping['properties']; |
|
430 | 6 | if (isset($mapping['_parent'])) { |
|
431 | 1 | $argument['_parent'] = $mapping['_parent']; |
|
432 | } |
||
433 | 6 | $arguments[] = $argument; |
|
434 | } |
||
435 | |||
436 | 9 | $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); |
|
437 | 9 | $serviceDef = new DefinitionDecorator($abstractId); |
|
438 | 9 | foreach ($arguments as $i => $argument) { |
|
439 | 9 | $serviceDef->replaceArgument($i, $argument); |
|
440 | } |
||
441 | |||
442 | 9 | $container->setDefinition($serviceId, $serviceDef); |
|
443 | |||
444 | 9 | return $serviceId; |
|
445 | } |
||
446 | |||
447 | /** |
||
448 | * Loads a provider for a type. |
||
449 | * |
||
450 | * @param array $typeConfig |
||
451 | * @param ContainerBuilder $container |
||
452 | * @param string $objectPersisterId |
||
453 | * @param string $indexName |
||
454 | * @param string $typeName |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | 10 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
481 | |||
482 | /** |
||
483 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
484 | * |
||
485 | * @param array $typeConfig |
||
486 | * @param ContainerBuilder $container |
||
487 | * @param string $objectPersisterId |
||
488 | * @param string $indexName |
||
489 | * @param string $typeName |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | 10 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
539 | |||
540 | /** |
||
541 | * Map Elastica to Doctrine events for the current driver. |
||
542 | */ |
||
543 | 10 | private function getDoctrineEvents(array $typeConfig) |
|
575 | |||
576 | /** |
||
577 | * Loads a Type specific Finder. |
||
578 | * |
||
579 | * @param array $typeConfig |
||
580 | * @param ContainerBuilder $container |
||
581 | * @param string $elasticaToModelId |
||
582 | * @param Reference $typeRef |
||
583 | * @param string $indexName |
||
584 | * @param string $typeName |
||
585 | * |
||
586 | * @return string |
||
587 | */ |
||
588 | 10 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
615 | |||
616 | /** |
||
617 | * Loads the index manager. |
||
618 | * |
||
619 | * @param ContainerBuilder $container |
||
620 | **/ |
||
621 | private function loadIndexManager(ContainerBuilder $container) |
||
630 | |||
631 | /** |
||
632 | * Makes sure a specific driver has been loaded. |
||
633 | * |
||
634 | * @param ContainerBuilder $container |
||
635 | * @param string $driver |
||
636 | */ |
||
637 | 10 | private function loadDriver(ContainerBuilder $container, $driver) |
|
647 | |||
648 | /** |
||
649 | * Loads and configures the serializer prototype. |
||
650 | * |
||
651 | * @param array $config |
||
652 | * @param ContainerBuilder $container |
||
653 | */ |
||
654 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
666 | |||
667 | /** |
||
668 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
669 | * |
||
670 | * @param string $defaultManager |
||
671 | * @param ContainerBuilder $container |
||
672 | */ |
||
673 | 14 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
689 | |||
690 | /** |
||
691 | * Returns a reference to a client given its configured name. |
||
692 | * |
||
693 | * @param string $clientName |
||
694 | * |
||
695 | * @return Reference |
||
696 | * |
||
697 | * @throws \InvalidArgumentException |
||
698 | */ |
||
699 | 2 | private function getClient($clientName) |
|
707 | } |
||
708 |