1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\MercurePHP\Hub; |
4
|
|
|
|
5
|
|
|
use BenTools\MercurePHP\Configuration\Configuration; |
6
|
|
|
use BenTools\MercurePHP\Controller\HealthController; |
7
|
|
|
use BenTools\MercurePHP\Controller\PublishController; |
8
|
|
|
use BenTools\MercurePHP\Controller\SubscribeController; |
9
|
|
|
use BenTools\MercurePHP\Helpers\LoggerAwareTrait; |
10
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerFactory; |
11
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerFactoryInterface; |
12
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerInterface; |
13
|
|
|
use BenTools\MercurePHP\Metrics\PHP\PHPMetricsHandler; |
14
|
|
|
use BenTools\MercurePHP\Security\Authenticator; |
15
|
|
|
use BenTools\MercurePHP\Storage\StorageFactory; |
16
|
|
|
use BenTools\MercurePHP\Storage\StorageFactoryInterface; |
17
|
|
|
use BenTools\MercurePHP\Storage\StorageInterface; |
18
|
|
|
use BenTools\MercurePHP\Transport\TransportFactory; |
19
|
|
|
use BenTools\MercurePHP\Transport\TransportFactoryInterface; |
20
|
|
|
use BenTools\MercurePHP\Transport\TransportInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Psr\Log\NullLogger; |
23
|
|
|
use React\EventLoop; |
24
|
|
|
|
25
|
|
|
use function Clue\React\Block\await; |
26
|
|
|
|
27
|
|
|
final class HubFactory |
28
|
|
|
{ |
29
|
|
|
use LoggerAwareTrait; |
30
|
|
|
|
31
|
|
|
private array $config; |
32
|
|
|
private TransportFactoryInterface $transportFactory; |
33
|
|
|
private StorageFactoryInterface $storageFactory; |
34
|
|
|
private MetricsHandlerFactoryInterface $metricsHandlerFactory; |
35
|
|
|
|
36
|
2 |
|
public function __construct( |
37
|
|
|
array $config, |
38
|
|
|
?LoggerInterface $logger = null, |
39
|
|
|
?TransportFactoryInterface $transportFactory = null, |
40
|
|
|
?StorageFactoryInterface $storageFactory = null, |
41
|
|
|
?MetricsHandlerFactoryInterface $metricsHandlerFactory = null |
42
|
|
|
) { |
43
|
2 |
|
$this->config = $config; |
44
|
2 |
|
$this->logger = $logger ?? new NullLogger(); |
|
|
|
|
45
|
2 |
|
$this->transportFactory = $transportFactory ?? TransportFactory::default($config, $this->logger); |
|
|
|
|
46
|
2 |
|
$this->storageFactory = $storageFactory ?? StorageFactory::default($config, $this->logger); |
|
|
|
|
47
|
2 |
|
$this->metricsHandlerFactory = $metricsHandlerFactory ?? MetricsHandlerFactory::default($config, $this->logger); |
|
|
|
|
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
public function create(EventLoop\LoopInterface $loop): Hub |
51
|
|
|
{ |
52
|
2 |
|
$transport = $this->createTransport($loop); |
53
|
1 |
|
$storage = $this->createStorage($loop); |
54
|
1 |
|
$metricsHandler = $this->createMetricsHandler($loop); |
55
|
|
|
|
56
|
1 |
|
$subscriberAuthenticator = Authenticator::createSubscriberAuthenticator($this->config); |
57
|
1 |
|
$publisherAuthenticator = Authenticator::createPublisherAuthenticator($this->config); |
58
|
|
|
|
59
|
|
|
$controllers = [ |
60
|
1 |
|
new HealthController(), |
61
|
1 |
|
(new SubscribeController($this->config, $subscriberAuthenticator)) |
62
|
1 |
|
->withLoop($loop) |
63
|
1 |
|
->withTransport($transport) |
64
|
1 |
|
->withStorage($storage) |
65
|
1 |
|
->withLogger($this->logger()) |
66
|
|
|
, |
67
|
1 |
|
(new PublishController($publisherAuthenticator)) |
68
|
1 |
|
->withTransport($transport) |
69
|
1 |
|
->withStorage($storage) |
70
|
1 |
|
->withLogger($this->logger()) |
71
|
|
|
, |
72
|
|
|
]; |
73
|
|
|
|
74
|
1 |
|
$requestHandler = new RequestHandler($controllers); |
75
|
|
|
|
76
|
1 |
|
return new Hub($this->config, $requestHandler, $metricsHandler, $this->logger()); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private function createTransport(EventLoop\LoopInterface $loop): TransportInterface |
80
|
|
|
{ |
81
|
2 |
|
$factory = $this->transportFactory; |
82
|
2 |
|
$dsn = $this->config[Configuration::TRANSPORT_URL]; |
83
|
|
|
|
84
|
2 |
|
if (!$factory->supports($dsn)) { |
85
|
1 |
|
throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn)); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return await($factory->create($dsn, $loop), $loop); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
private function createStorage(EventLoop\LoopInterface $loop): StorageInterface |
92
|
|
|
{ |
93
|
1 |
|
$factory = $this->storageFactory; |
94
|
1 |
|
$dsn = $this->config[Configuration::STORAGE_URL] |
95
|
1 |
|
?? $this->config[Configuration::TRANSPORT_URL]; |
96
|
|
|
|
97
|
1 |
|
if (!$factory->supports($dsn)) { |
98
|
|
|
throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn)); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return await($factory->create($dsn, $loop), $loop); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
private function createMetricsHandler(EventLoop\LoopInterface $loop): MetricsHandlerInterface |
105
|
|
|
{ |
106
|
1 |
|
$factory = $this->metricsHandlerFactory; |
107
|
1 |
|
$dsn = $this->config[Configuration::METRICS_URL] |
108
|
|
|
?? $this->config[Configuration::STORAGE_URL] |
109
|
1 |
|
?? $this->config[Configuration::TRANSPORT_URL]; |
110
|
|
|
|
111
|
1 |
|
if (!$factory->supports($dsn)) { |
112
|
|
|
throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn)); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return await($factory->create($dsn, $loop), $loop); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.