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) |
|
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) |
|
102 | { |
||
103 | 14 | foreach ($clients as $name => $clientConfig) { |
|
104 | 14 | $clientId = sprintf('fos_elastica.client.%s', $name); |
|
105 | |||
106 | 14 | $clientDef = new DefinitionDecorator('fos_elastica.client_prototype'); |
|
107 | 14 | $clientDef->replaceArgument(0, $clientConfig); |
|
108 | |||
109 | 14 | $logger = $clientConfig['connections'][0]['logger']; |
|
110 | 14 | if (false !== $logger) { |
|
111 | 14 | $clientDef->addMethodCall('setLogger', array(new Reference($logger))); |
|
112 | } |
||
113 | 14 | $clientDef->addTag('fos_elastica.client'); |
|
114 | |||
115 | 14 | $container->setDefinition($clientId, $clientDef); |
|
116 | |||
117 | 14 | $this->clients[$name] = array( |
|
118 | 14 | 'id' => $clientId, |
|
119 | 14 | 'reference' => new Reference($clientId), |
|
120 | ); |
||
121 | } |
||
122 | 14 | } |
|
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 | 14 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
135 | { |
||
136 | 14 | $indexableCallbacks = array(); |
|
137 | |||
138 | 14 | foreach ($indexes as $name => $index) { |
|
139 | 14 | $indexId = sprintf('fos_elastica.index.%s', $name); |
|
140 | 14 | $indexName = isset($index['index_name']) ? $index['index_name'] : $name; |
|
141 | |||
142 | 14 | $indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); |
|
143 | 14 | $indexDef->replaceArgument(0, $indexName); |
|
144 | 14 | $indexDef->addTag('fos_elastica.index', array( |
|
145 | 14 | 'name' => $name, |
|
146 | )); |
||
147 | |||
148 | 14 | if (method_exists($indexDef, 'setFactory')) { |
|
149 | 14 | $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 | 14 | 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 | 14 | $container->setDefinition($indexId, $indexDef); |
|
169 | 14 | $reference = new Reference($indexId); |
|
170 | |||
171 | 14 | $this->indexConfigs[$name] = array( |
|
172 | 14 | 'elasticsearch_name' => $indexName, |
|
173 | 14 | 'reference' => $reference, |
|
174 | 14 | 'name' => $name, |
|
175 | 14 | 'settings' => $index['settings'], |
|
176 | 14 | 'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : array(), |
|
177 | 14 | 'use_alias' => $index['use_alias'], |
|
178 | ); |
||
179 | |||
180 | 14 | if ($index['finder']) { |
|
181 | $this->loadIndexFinder($container, $name, $reference); |
||
182 | } |
||
183 | |||
184 | 14 | $this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name], $indexableCallbacks); |
|
185 | } |
||
186 | |||
187 | 14 | $indexable = $container->getDefinition('fos_elastica.indexable'); |
|
188 | 14 | $indexable->replaceArgument(0, $indexableCallbacks); |
|
189 | 14 | } |
|
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 | 14 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
315 | |||
316 | /** |
||
317 | * Loads the optional provider and finder for a type. |
||
318 | * |
||
319 | * @param array $typeConfig |
||
320 | * @param ContainerBuilder $container |
||
321 | * @param Reference $typeRef |
||
322 | * @param string $indexName |
||
323 | * @param string $typeName |
||
324 | */ |
||
325 | 10 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
345 | |||
346 | /** |
||
347 | * Creates and loads an ElasticaToModelTransformer. |
||
348 | * |
||
349 | * @param array $typeConfig |
||
350 | * @param ContainerBuilder $container |
||
351 | * @param string $indexName |
||
352 | * @param string $typeName |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | 10 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
381 | |||
382 | /** |
||
383 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
384 | * |
||
385 | * @param array $typeConfig |
||
386 | * @param ContainerBuilder $container |
||
387 | * @param string $indexName |
||
388 | * @param string $typeName |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | 10 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
411 | |||
412 | /** |
||
413 | * Creates and loads an object persister for a type. |
||
414 | * |
||
415 | * @param array $typeConfig |
||
416 | * @param Reference $typeRef |
||
417 | * @param ContainerBuilder $container |
||
418 | * @param string $indexName |
||
419 | * @param string $typeName |
||
420 | * @param string $transformerId |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 10 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
460 | |||
461 | /** |
||
462 | * Loads a provider for a type. |
||
463 | * |
||
464 | * @param array $typeConfig |
||
465 | * @param ContainerBuilder $container |
||
466 | * @param string $objectPersisterId |
||
467 | * @param string $indexName |
||
468 | * @param string $typeName |
||
469 | * |
||
470 | * @return string |
||
471 | */ |
||
472 | 5 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
495 | |||
496 | /** |
||
497 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
498 | * |
||
499 | * @param array $typeConfig |
||
500 | * @param ContainerBuilder $container |
||
501 | * @param string $objectPersisterId |
||
502 | * @param string $indexName |
||
503 | * @param string $typeName |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | 5 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
553 | |||
554 | /** |
||
555 | * Map Elastica to Doctrine events for the current driver. |
||
556 | */ |
||
557 | 5 | private function getDoctrineEvents(array $typeConfig) |
|
589 | |||
590 | /** |
||
591 | * Loads a Type specific Finder. |
||
592 | * |
||
593 | * @param array $typeConfig |
||
594 | * @param ContainerBuilder $container |
||
595 | * @param string $elasticaToModelId |
||
596 | * @param Reference $typeRef |
||
597 | * @param string $indexName |
||
598 | * @param string $typeName |
||
599 | * |
||
600 | * @return string |
||
601 | */ |
||
602 | 5 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
624 | |||
625 | /** |
||
626 | * Loads the index manager. |
||
627 | * |
||
628 | * @param ContainerBuilder $container |
||
629 | **/ |
||
630 | private function loadIndexManager(ContainerBuilder $container) |
||
639 | |||
640 | /** |
||
641 | * Makes sure a specific driver has been loaded. |
||
642 | * |
||
643 | * @param ContainerBuilder $container |
||
644 | * @param string $driver |
||
645 | */ |
||
646 | 9 | private function loadDriver(ContainerBuilder $container, $driver) |
|
656 | |||
657 | /** |
||
658 | * Loads and configures the serializer prototype. |
||
659 | * |
||
660 | * @param array $config |
||
661 | * @param ContainerBuilder $container |
||
662 | */ |
||
663 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
675 | |||
676 | /** |
||
677 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
678 | * |
||
679 | * @param string $defaultManager |
||
680 | * @param ContainerBuilder $container |
||
681 | */ |
||
682 | 14 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
698 | |||
699 | /** |
||
700 | * Returns a reference to a client given its configured name. |
||
701 | * |
||
702 | * @param string $clientName |
||
703 | * |
||
704 | * @return Reference |
||
705 | * |
||
706 | * @throws \InvalidArgumentException |
||
707 | */ |
||
708 | 2 | private function getClient($clientName) |
|
716 | } |
||
717 |