| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use As3\Bundle\ModlrBundle\DependencyInjection\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Loads metadata driver services. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author  Jacob Bare <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class MetadataDrivers implements ServiceLoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Creates the file locator service definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param   string  $modelDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param   string  $mixinDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param   string  $embedDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return  Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private function createFileLocator($modelDir, $mixinDir, $embedDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             Utility::getLibraryClass('Metadata\Driver\FileLocator'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             [$modelDir, $mixinDir, $embedDir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $definition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return $definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Creates the YAML metadata driver service definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param   string              $driverName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param   array               $driverConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return  Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private function createYmlDriver($driverName, array $driverConfig, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // Definition directories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         list($modelDir, $mixinDir, $embedDir) = $this->getDefinitionDirs($driverConfig, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // Set the directories to the dirs container parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         Utility::appendParameter('dirs', sprintf('%s.model_dir', $driverName), $modelDir, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         Utility::appendParameter('dirs', sprintf('%s.mixin_dir', $driverName), $mixinDir, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Utility::appendParameter('dirs', sprintf('%s.embed_dir', $driverName), $embedDir, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         // File locator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $locatorName = sprintf('%s.file_locator', $driverName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $locatorDef = $this->createFileLocator($modelDir, $mixinDir, $embedDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $container->setDefinition($locatorName, $locatorDef); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             Utility::getLibraryClass('Metadata\Driver\YamlFileDriver'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 new Reference($locatorName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 new Reference(Utility::getAliasedName('storage_manager')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Gets the directory for a definition type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param   string              $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param   array               $driverConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return  string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private function getDefinitionDir($type, array $driverConfig, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $defaultDir = sprintf('%s/Resources/As3ModlrBundle', $container->getParameter('kernel.root_dir')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $folder = sprintf('%ss', $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $key = sprintf('%s_dir', $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return isset($driverConfig['parameters'][$key]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ? Utility::locateResource($driverConfig['parameters'][$key], $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             : sprintf('%s/%s', $defaultDir, $folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Gets the definition directories for models and mixins and returns as a tuple. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param   array               $driverConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return  string[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     private function getDefinitionDirs(array $driverConfig, ContainerBuilder $container) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $modelDir = $this->getDefinitionDir('model', $driverConfig, $container); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $mixinDir = $this->getDefinitionDir('mixin', $driverConfig, $container); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $embedDir = $this->getDefinitionDir('embed', $driverConfig, $container); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         return [$modelDir, $mixinDir, $embedDir]; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 |  | View Code Duplication |     public function load(array $config, ContainerBuilder $container) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         foreach ($config['metadata']['drivers'] as $name => $driverConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $driverName = Utility::getAliasedName(sprintf('metadata.driver.%s', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             if (isset($driverConfig['service'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 // Custom persister. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $container->setAlias($driverName, Utility::cleanServiceName($driverConfig['service'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 // Built-in driver. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 switch ($driverConfig['type']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     case 'yml': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         $definition = $this->createYmlDriver($driverName, $driverConfig, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         throw new \RuntimeException(sprintf('Unable to create a metadata driver for type "%s"', $driverConfig['type'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 $definition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $container->setDefinition($driverName, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             // The library currently only supports one driver. Must break and set as default alias. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $container->setAlias(Utility::getAliasedName('metadata.default_driver'), $driverName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                        
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.