| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Noxlogic\RateLimitBundle\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\DependencyInjection\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This is the class that loads and manages your bundle configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class NoxlogicRateLimitExtension extends Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 5 |  |     public function load(array $configs, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 5 |  |         $configuration = new Configuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 5 |  |         $config = $this->processConfiguration($configuration, $configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 5 |  |         $this->loadServices($container, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 5 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 29 | 5 |  |     private function loadServices(ContainerBuilder $container, array $config) | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 31 | 5 |  |         $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | 
            
                                                        
            
                                    
            
            
                | 32 | 5 |  |         $loader->load('services.xml'); | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 | 5 |  |         $container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']); | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 | 5 |  |         $container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']); | 
            
                                                        
            
                                    
            
            
                | 37 | 5 |  |         $container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']); | 
            
                                                        
            
                                    
            
            
                | 38 | 5 |  |         $container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']); | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 | 5 |  |         $container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']); | 
            
                                                        
            
                                    
            
            
                | 41 | 5 |  |         $container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']); | 
            
                                                        
            
                                    
            
            
                | 42 | 5 |  |         $container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']); | 
            
                                                        
            
                                    
            
            
                | 43 | 5 |  |         $container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']); | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 | 5 |  |         $container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']); | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 | 5 |  |         switch ($config['storage_engine']) { | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 48 | 5 | View Code Duplication |             case 'memcache': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache'); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                 if (isset($config['memcache_client'])) { | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                     $service = 'memcache.' . $config['memcache_client']; | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |                 } else { | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                     $service = $config['memcache_service']; | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |                     new Reference($service) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |                 break; | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 60 | 5 | View Code Duplication |             case 'redis': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 61 | 3 |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis'); | 
            
                                                        
            
                                    
            
            
                | 62 | 3 |  |                 if (isset($config['redis_service'])) { | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |                     $service = $config['redis_service']; | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |                 } else { | 
            
                                                        
            
                                    
            
            
                | 65 | 3 |  |                     $service = 'snc_redis.' . $config['redis_client']; | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 67 | 3 |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 68 | 3 |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 69 | 3 |  |                     new Reference($service) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 71 | 3 |  |                 break; | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 72 | 2 | View Code Duplication |             case 'doctrine': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 73 | 2 |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache'); | 
            
                                                        
            
                                    
            
            
                | 74 | 2 |  |                 if (isset($config['doctrine_provider'])) { | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |                     $service = 'doctrine_cache.providers.' . $config['doctrine_provider']; | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |                 } else { | 
            
                                                        
            
                                    
            
            
                | 77 | 1 |  |                     $service = $config['doctrine_service']; | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 79 | 2 |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 80 | 2 |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 81 | 2 |  |                     new Reference($service) | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 83 | 2 |  |                 break; | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 84 |  | View Code Duplication |             case 'php_redis': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedis'); | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |                     new Reference($config['php_redis_service']) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                 break; | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |             case 'php_redis_cluster': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedisCluster'); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |                     new Reference($config['php_redis_service']) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             case 'simple_cache': | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache'); | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |                     new Reference($config['simple_cache_service']) | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             case 'cache': | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PsrCache'); | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |                 $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument( | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |                     new Reference($config['cache_service']) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 | 5 |  |         if ($config['fos_oauth_key_listener']) { | 
            
                                                        
            
                                    
            
            
                | 115 | 5 |  |             $tokenStorageReference = new Reference('security.token_storage'); | 
            
                                                        
            
                                    
            
            
                | 116 | 5 |  |             $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference); | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |             $container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener'); | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 120 | 5 |  |     } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
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.