| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BenTools\MercurePHP\Hub; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BenTools\MercurePHP\Configuration\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BenTools\MercurePHP\Configuration\WithConfigTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BenTools\MercurePHP\Controller\AbstractController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BenTools\MercurePHP\Metrics\MetricsHandlerFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BenTools\MercurePHP\Metrics\MetricsHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BenTools\MercurePHP\Storage\StorageFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BenTools\MercurePHP\Storage\StorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BenTools\MercurePHP\Transport\TransportFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BenTools\MercurePHP\Transport\TransportInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use React\EventLoop\LoopInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function Clue\React\Block\await; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class HubFactory implements HubFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use WithConfigTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private LoopInterface $loop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private LoggerInterface $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private TransportFactoryInterface $transportFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private StorageFactoryInterface $storageFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private MetricsHandlerFactoryInterface $metricsHandlerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var iterable|AbstractController[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private iterable $controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Configuration $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         LoopInterface $loop, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         LoggerInterface $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         TransportFactoryInterface $transportFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         StorageFactoryInterface $storageFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         MetricsHandlerFactoryInterface $metricsHandlerFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         iterable $controllers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         $this->config = $config->asArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         $this->loop = $loop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         $this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $this->transportFactory = $transportFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->storageFactory = $storageFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->metricsHandlerFactory = $metricsHandlerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->controllers = $controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function create(): HubInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $transport = $this->createTransport(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $storage = $this->createStorage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $metricsHandler = $this->createMetricsHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $controllers = \array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             fn(AbstractController $controller) => $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |                 ->withTransport($transport) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |                 ->withStorage($storage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |                 ->withConfig($this->config), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             \iterable_to_array($this->controllers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         $requestHandler = new RequestHandler($controllers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         return new Hub($this->config, $this->loop, $requestHandler, $metricsHandler, $this->logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     private function createTransport(): TransportInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         $factory = $this->transportFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $dsn = $this->config[Configuration::TRANSPORT_URL]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!$factory->supports($dsn)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         return await($factory->create($dsn), $this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |     private function createStorage(): StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $factory = $this->storageFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $dsn = $this->config[Configuration::STORAGE_URL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             ?? $this->config[Configuration::TRANSPORT_URL]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (!$factory->supports($dsn)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         return await($factory->create($dsn), $this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |     private function createMetricsHandler(): MetricsHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $factory = $this->metricsHandlerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $dsn = $this->config[Configuration::METRICS_URL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |             ?? $this->config[Configuration::STORAGE_URL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ?? $this->config[Configuration::TRANSPORT_URL]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         if (!$factory->supports($dsn)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return await($factory->create($dsn), $this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  |