| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * File containing the LegacyStorageEnginePass class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (C) eZ Systems AS. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license For full copyright and license information view LICENSE file distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\DependencyInjection\Exception\LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * This compiler pass will register eZ Publish field types. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class LegacyStorageEnginePass implements CompilerPassInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function process(ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (!$container->hasDefinition('ezpublish.api.storage_engine.legacy.factory')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $legacyStorageEngineDef = $container->getDefinition('ezpublish.api.storage_engine.legacy.factory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // Field types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // Alias attribute is the field type string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         foreach ($container->findTaggedServiceIds('ezpublish.fieldType') as $id => $attributes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             foreach ($attributes as $attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 if (!isset($attribute['alias'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     throw new \LogicException('ezpublish.fieldType service tag needs an "alias" attribute to identify the field type. None given.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $legacyStorageEngineDef->addMethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     'registerFieldType', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         // Only pass the service Id since field types will be lazy loaded via the service container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                         $attribute['alias'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 51 |  | View Code Duplication |         foreach ($container->findTaggedServiceIds('ezpublish.storageEngine.legacy.converter') as $id => $attributes) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             foreach ($attributes as $attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 if (!isset($attribute['alias'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     throw new LogicException('ezpublish.storageEngine.legacy.converter service tag needs an "alias" attribute to identify the field type. None given.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 if (isset($attribute['lazy']) && $attribute['lazy'] === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     if (!isset($attribute['callback'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         throw new LogicException("Converter service '$id' is marked as lazy but no callback is provided! Please provide a callback."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     $converter = $attribute['callback']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     if (strpos($converter, '::') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                         $converter = $container->getDefinition($id)->getClass() . $converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $converter = new Reference($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $legacyStorageEngineDef->addMethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     'registerFieldTypeConverter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         $attribute['alias'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                         $converter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.