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 |
||
14 | class FOSElasticaExtension extends Extension |
||
15 | { |
||
16 | /** |
||
17 | * Definition of elastica clients as configured by this extension. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $clients = array(); |
||
22 | |||
23 | /** |
||
24 | * An array of indexes as configured by the extension. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $indexConfigs = array(); |
||
29 | |||
30 | /** |
||
31 | * If we've encountered a type mapped to a specific persistence driver, it will be loaded |
||
32 | * here. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $loadedDrivers = array(); |
||
37 | |||
38 | 14 | public function load(array $configs, ContainerBuilder $container) |
|
82 | |||
83 | /** |
||
84 | * @param array $config |
||
85 | * @param ContainerBuilder $container |
||
86 | * |
||
87 | * @return Configuration |
||
88 | */ |
||
89 | 14 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
93 | |||
94 | /** |
||
95 | * Loads the configured clients. |
||
96 | * |
||
97 | * @param array $clients An array of clients configurations |
||
98 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 14 | private function loadClients(array $clients, ContainerBuilder $container) |
|
125 | |||
126 | /** |
||
127 | * Loads the configured indexes. |
||
128 | * |
||
129 | * @param array $indexes An array of indexes configurations |
||
130 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
131 | * |
||
132 | * @throws \InvalidArgumentException |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 14 | private function loadIndexes(array $indexes, ContainerBuilder $container) |
|
179 | |||
180 | /** |
||
181 | * Loads the configured index finders. |
||
182 | * |
||
183 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
184 | * @param string $name The index name |
||
185 | * @param Reference $index Reference to the related index |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | private function loadIndexFinder(ContainerBuilder $container, $name, Reference $index) |
||
205 | |||
206 | /** |
||
207 | * Loads the configured types. |
||
208 | * |
||
209 | * @param array $types |
||
210 | * @param ContainerBuilder $container |
||
211 | * @param array $indexConfig |
||
212 | * @param array $indexableCallbacks |
||
213 | */ |
||
214 | 14 | private function loadTypes(array $types, ContainerBuilder $container, array $indexConfig, array &$indexableCallbacks) |
|
302 | |||
303 | /** |
||
304 | * Loads the optional provider and finder for a type. |
||
305 | * |
||
306 | * @param array $typeConfig |
||
307 | * @param ContainerBuilder $container |
||
308 | * @param Reference $typeRef |
||
309 | * @param string $indexName |
||
310 | * @param string $typeName |
||
311 | */ |
||
312 | 10 | private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuilder $container, Reference $typeRef, $indexName, $typeName) |
|
313 | { |
||
314 | 10 | if (isset($typeConfig['driver'])) { |
|
315 | 10 | $this->loadDriver($container, $typeConfig['driver']); |
|
316 | } |
||
317 | |||
318 | 10 | $elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($typeConfig, $container, $indexName, $typeName); |
|
319 | 10 | $modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($typeConfig, $container, $indexName, $typeName); |
|
320 | 10 | $objectPersisterId = $this->loadObjectPersister($typeConfig, $typeRef, $container, $indexName, $typeName, $modelToElasticaTransformerId); |
|
321 | |||
322 | 10 | if (isset($typeConfig['provider'])) { |
|
323 | 10 | $this->loadTypeProvider($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
324 | } |
||
325 | 10 | if (isset($typeConfig['finder'])) { |
|
326 | 10 | $this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName); |
|
327 | } |
||
328 | 10 | if (isset($typeConfig['listener'])) { |
|
329 | 10 | $this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName); |
|
330 | } |
||
331 | 10 | } |
|
332 | |||
333 | /** |
||
334 | * Creates and loads an ElasticaToModelTransformer. |
||
335 | * |
||
336 | * @param array $typeConfig |
||
337 | * @param ContainerBuilder $container |
||
338 | * @param string $indexName |
||
339 | * @param string $typeName |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 10 | private function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
368 | |||
369 | /** |
||
370 | * Creates and loads a ModelToElasticaTransformer for an index/type. |
||
371 | * |
||
372 | * @param array $typeConfig |
||
373 | * @param ContainerBuilder $container |
||
374 | * @param string $indexName |
||
375 | * @param string $typeName |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | 10 | private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName) |
|
398 | |||
399 | /** |
||
400 | * Creates and loads an object persister for a type. |
||
401 | * |
||
402 | * @param array $typeConfig |
||
403 | * @param Reference $typeRef |
||
404 | * @param ContainerBuilder $container |
||
405 | * @param string $indexName |
||
406 | * @param string $typeName |
||
407 | * @param string $transformerId |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 10 | private function loadObjectPersister(array $typeConfig, Reference $typeRef, ContainerBuilder $container, $indexName, $typeName, $transformerId) |
|
412 | { |
||
413 | 10 | if (isset($typeConfig['persister']['service'])) { |
|
414 | 1 | return $typeConfig['persister']['service']; |
|
415 | } |
||
416 | |||
417 | $arguments = array( |
||
418 | 9 | $typeRef, |
|
419 | 9 | new Reference($transformerId), |
|
420 | 9 | $typeConfig['model'], |
|
421 | ); |
||
422 | |||
423 | 9 | if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
|
424 | 3 | $abstractId = 'fos_elastica.object_serializer_persister'; |
|
425 | 3 | $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['reference'], $typeName); |
|
426 | 3 | $arguments[] = array(new Reference($callbackId), 'serialize'); |
|
427 | } else { |
||
428 | 6 | $abstractId = 'fos_elastica.object_persister'; |
|
429 | 6 | $mapping = $this->indexConfigs[$indexName]['types'][$typeName]['mapping']; |
|
430 | 6 | $argument = $mapping['properties']; |
|
431 | 6 | if (isset($mapping['_parent'])) { |
|
432 | 1 | $argument['_parent'] = $mapping['_parent']; |
|
433 | } |
||
434 | 6 | $arguments[] = $argument; |
|
435 | } |
||
436 | |||
437 | 9 | $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); |
|
438 | 9 | $serviceDef = new DefinitionDecorator($abstractId); |
|
439 | 9 | foreach ($arguments as $i => $argument) { |
|
440 | 9 | $serviceDef->replaceArgument($i, $argument); |
|
441 | } |
||
442 | |||
443 | 9 | $container->setDefinition($serviceId, $serviceDef); |
|
444 | |||
445 | 9 | return $serviceId; |
|
446 | } |
||
447 | |||
448 | /** |
||
449 | * Loads a provider for a type. |
||
450 | * |
||
451 | * @param array $typeConfig |
||
452 | * @param ContainerBuilder $container |
||
453 | * @param string $objectPersisterId |
||
454 | * @param string $indexName |
||
455 | * @param string $typeName |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | 10 | private function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
482 | |||
483 | /** |
||
484 | * Loads doctrine listeners to handle indexing of new or updated objects. |
||
485 | * |
||
486 | * @param array $typeConfig |
||
487 | * @param ContainerBuilder $container |
||
488 | * @param string $objectPersisterId |
||
489 | * @param string $indexName |
||
490 | * @param string $typeName |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | 10 | private function loadTypeListener(array $typeConfig, ContainerBuilder $container, $objectPersisterId, $indexName, $typeName) |
|
547 | |||
548 | /** |
||
549 | * Map Elastica to Doctrine events for the current driver. |
||
550 | */ |
||
551 | 10 | private function getDoctrineEvents(array $typeConfig) |
|
586 | |||
587 | /** |
||
588 | * Loads a Type specific Finder. |
||
589 | * |
||
590 | * @param array $typeConfig |
||
591 | * @param ContainerBuilder $container |
||
592 | * @param string $elasticaToModelId |
||
593 | * @param Reference $typeRef |
||
594 | * @param string $indexName |
||
595 | * @param string $typeName |
||
596 | * |
||
597 | * @return string |
||
598 | */ |
||
599 | 10 | private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, Reference $typeRef, $indexName, $typeName) |
|
626 | |||
627 | /** |
||
628 | * Loads the index manager. |
||
629 | * |
||
630 | * @param ContainerBuilder $container |
||
631 | **/ |
||
632 | private function loadIndexManager(ContainerBuilder $container) |
||
641 | |||
642 | /** |
||
643 | * Makes sure a specific driver has been loaded. |
||
644 | * |
||
645 | * @param ContainerBuilder $container |
||
646 | * @param string $driver |
||
647 | */ |
||
648 | 10 | private function loadDriver(ContainerBuilder $container, $driver) |
|
658 | |||
659 | /** |
||
660 | * Loads and configures the serializer prototype. |
||
661 | * |
||
662 | * @param array $config |
||
663 | * @param ContainerBuilder $container |
||
664 | */ |
||
665 | 3 | private function loadSerializer($config, ContainerBuilder $container) |
|
677 | |||
678 | /** |
||
679 | * Creates a default manager alias for defined default manager or the first loaded driver. |
||
680 | * |
||
681 | * @param string $defaultManager |
||
682 | * @param ContainerBuilder $container |
||
683 | */ |
||
684 | 14 | private function createDefaultManagerAlias($defaultManager, ContainerBuilder $container) |
|
700 | |||
701 | /** |
||
702 | * Returns a reference to a client given its configured name. |
||
703 | * |
||
704 | * @param string $clientName |
||
705 | * |
||
706 | * @return Reference |
||
707 | * |
||
708 | * @throws \InvalidArgumentException |
||
709 | */ |
||
710 | 2 | private function getClient($clientName) |
|
718 | } |
||
719 |