| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Facile\MongoDbBundle\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Facile\MongoDbBundle\DataCollector\MongoDbDataCollector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Facile\MongoDbBundle\Services\ClientRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Facile\MongoDbBundle\Services\ConnectionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Facile\MongoDbBundle\Services\Loggers\MongoLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use MongoDB\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class MongoDbBundleExtension. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class MongoDbBundleExtension extends Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var ContainerBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $containerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |     public function load(array $configs, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 4 |  |         $this->containerBuilder = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 4 |  |         $configuration = new Configuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |         $config = $this->processConfiguration($configuration, $configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |         $this->defineLoggers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         if ($container->getParameter("kernel.environment") === 'dev' && class_exists(WebProfilerBundle::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |             $this->defineDataCollector(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |         $this->defineClientRegistry($config['clients'], $container->getParameter("kernel.environment")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 4 |  |         $this->defineConnectionFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |         $this->defineConnections($config['connections']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 4 |  |     private function defineLoggers() | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 4 |  |         $loggerDefinition = new Definition(MongoLogger::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 4 |  |         $loggerDefinition->setPublic(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 4 |  |         $this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 56 | 2 | View Code Duplication |     private function defineDataCollector() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |         $dataCollectorDefinition = new Definition(MongoDbDataCollector::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |         $dataCollectorDefinition->addMethodCall('setLogger', [new Reference('facile_mongo_db.logger')]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         $dataCollectorDefinition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         $this->containerBuilder->setDefinition('facile_mongo_db.data_collector', $dataCollectorDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param array  $clientsConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 69 | 4 | View Code Duplication |     private function defineClientRegistry(array $clientsConfig, string $environment) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |         $clientRegistryDefinition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |             ClientRegistry::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |                 new Reference('facile_mongo_db.logger'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |                 $environment, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |         $clientRegistryDefinition->addMethodCall('addClientsConfigurations', [$clientsConfig]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |         $clientRegistryDefinition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         $this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |     private function defineConnectionFactory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         $factoryDefinition = new Definition(ConnectionFactory::class, [new Reference('mongo.client_registry')]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         $factoryDefinition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |         $this->containerBuilder->setDefinition('mongo.connection_factory', $factoryDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param array $connections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |     private function defineConnections(array $connections) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |         foreach ($connections as $name => $conf) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  |             $connectionDefinition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 4 |  |                 Database::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  |                     $conf['client_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  |                     $conf['database_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |             $connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  |             $this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |         $this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.'.array_keys($connections)[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 4 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                        
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.