| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Stitcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pageon\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Stitcher\Application\DevelopmentServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Stitcher\Application\ProductionServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Stitcher\Exception\InvalidConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Stitcher\Exception\InvalidPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class App | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var ContainerBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected static $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 3 |  |     public static function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 3 |  |         Config::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 3 |  |         self::$container = new ContainerBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |         self::loadConfig(Config::all()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         self::loadServices('services.yaml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |         self::loadPlugins(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |     public static function get(string $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |         return self::$container->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 40 |  | View Code Duplication |     public static function developmentServer(): DevelopmentServer | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             /** @var DevelopmentServer $server */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             $server = self::get(DevelopmentServer::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             return $server; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } catch (ParameterNotFoundException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             throw InvalidConfiguration::missingParameter($e->getKey()); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |     public static function productionServer(): ProductionServer | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             /** @var ProductionServer $server */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $server = self::get(ProductionServer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return $server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } catch (ParameterNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             throw InvalidConfiguration::missingParameter($e->getKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |     protected static function loadConfig(array $config): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |         foreach ($config as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |             self::$container->setParameter($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |     protected static function loadServices(string $servicesPath): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |         $loader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 3 |  |         $loader->load($servicesPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         /** @var Definition $definition */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |         foreach (self::$container->getDefinitions() as $id => $definition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |             self::$container->setAlias($definition->getClass(), $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |     protected static function loadPlugins(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         foreach (Config::plugins() as $pluginClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |             if (!class_implements($pluginClass, Plugin::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 throw InvalidPlugin::doesntImplementPluginInterface($pluginClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |             self::loadPluginConfiguration($pluginClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |             self::loadPluginServices($pluginClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |             self::registerPluginDefinition($pluginClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |     protected static function loadPluginConfiguration(string $pluginClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |         $configurationPath = forward_static_call([$pluginClass, 'getConfigurationPath']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |         if (!$configurationPath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (!file_exists($configurationPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             throw InvalidPlugin::configurationFileNotFound($pluginClass, $configurationPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $pluginConfiguration = require $configurationPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if (!is_array($pluginConfiguration)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             throw InvalidPlugin::configurationMustBeArray($pluginClass, $configurationPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         self::loadConfig(Arr::dot($pluginConfiguration)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 3 |  |     protected static function loadPluginServices(string $pluginClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 3 |  |         $servicesPath = forward_static_call([$pluginClass, 'getServicesPath']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 3 |  |         if (!$servicesPath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (!file_exists($servicesPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             throw InvalidPlugin::serviceFileNotFound($pluginClass, $servicesPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         self::loadServices($servicesPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  |     protected static function registerPluginDefinition(string $pluginClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |         $definition = new Definition($pluginClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |         $definition->setAutowired(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 3 |  |         self::$container->setDefinition($pluginClass, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  | 
            
                        
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.