Passed
Pull Request — master (#8)
by BENOIT
01:53
created

HubFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 112
ccs 42
cts 45
cp 0.9333
rs 10
c 1
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultTransportFactory() 0 5 1
A __construct() 0 12 1
A createMetricsHandler() 0 12 2
A getDefaultStorageFactory() 0 6 1
A createStorage() 0 11 2
A create() 0 26 1
A getDefaultMetricsHandlerFactory() 0 5 1
A createTransport() 0 10 2
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\PHPMetricsHandlerFactory;
14
use BenTools\MercurePHP\Metrics\Redis\RedisMetricsHandlerFactory;
15
use BenTools\MercurePHP\Security\Authenticator;
16
use BenTools\MercurePHP\Storage\NullStorage\NullStorageFactory;
17
use BenTools\MercurePHP\Storage\PHP\PHPStorageFactory;
18
use BenTools\MercurePHP\Storage\Redis\RedisStorageFactory;
0 ignored issues
show
Bug introduced by
The type BenTools\MercurePHP\Stor...dis\RedisStorageFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use BenTools\MercurePHP\Storage\StorageFactory;
20
use BenTools\MercurePHP\Storage\StorageFactoryInterface;
21
use BenTools\MercurePHP\Storage\StorageInterface;
22
use BenTools\MercurePHP\Transport\PHP\PHPTransportFactory;
23
use BenTools\MercurePHP\Transport\Redis\RedisTransportFactory;
24
use BenTools\MercurePHP\Transport\TransportFactory;
25
use BenTools\MercurePHP\Transport\TransportFactoryInterface;
26
use BenTools\MercurePHP\Transport\TransportInterface;
27
use Psr\Log\LoggerInterface;
28
use Psr\Log\NullLogger;
29
use React\EventLoop\LoopInterface;
30
31
use function Clue\React\Block\await;
32
33
final class HubFactory
34
{
35
    use LoggerAwareTrait;
36 2
37
    private array $config;
38
    private ?TransportFactoryInterface $transportFactory;
39
    private ?StorageFactoryInterface $storageFactory;
40
    private ?MetricsHandlerFactoryInterface $metricsHandlerFactory;
41
42
    public function __construct(
43 2
        array $config,
44 2
        ?LoggerInterface $logger = null,
45 2
        ?TransportFactoryInterface $transportFactory = null,
46 2
        ?StorageFactoryInterface $storageFactory = null,
47 2
        ?MetricsHandlerFactoryInterface $metricsHandlerFactory = null
48 2
    ) {
49
        $this->config = $config;
50 2
        $this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Deprecated Code introduced by
The property BenTools\MercurePHP\Help...ggerAwareTrait::$logger has been deprecated: - Please call $this->logger() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
        /** @scrutinizer ignore-deprecated */ $this->logger = $logger ?? new NullLogger();

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.

Loading history...
51
        $this->transportFactory = $transportFactory;
52 2
        $this->storageFactory = $storageFactory;
53 1
        $this->metricsHandlerFactory = $metricsHandlerFactory;
54 1
    }
55
56 1
    public function create(LoopInterface $loop): Hub
57 1
    {
58
        $transport = $this->createTransport($loop);
59
        $storage = $this->createStorage($loop);
60 1
        $metricsHandler = $this->createMetricsHandler($loop);
61 1
62 1
        $subscriberAuthenticator = Authenticator::createSubscriberAuthenticator($this->config);
63 1
        $publisherAuthenticator = Authenticator::createPublisherAuthenticator($this->config);
64 1
65
        $controllers = [
66 1
            new HealthController(),
67 1
            (new SubscribeController($this->config, $subscriberAuthenticator, $loop))
68 1
                ->withTransport($transport)
69 1
                ->withStorage($storage)
70
                ->withLogger($this->logger())
71
            ,
72
            (new PublishController($publisherAuthenticator))
73 1
                ->withTransport($transport)
74
                ->withStorage($storage)
75 1
                ->withLogger($this->logger())
76
            ,
77
        ];
78 2
79
        $requestHandler = new RequestHandler($controllers);
80 2
81 2
        return new Hub($this->config, $requestHandler, $metricsHandler, $this->logger());
82
    }
83 2
84 1
    private function createTransport(LoopInterface $loop): TransportInterface
85
    {
86
        $factory = $this->transportFactory ?? $this->getDefaultTransportFactory($loop);
87 1
        $dsn = $this->config[Configuration::TRANSPORT_URL];
88
89
        if (!$factory->supports($dsn)) {
90 1
            throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn));
91
        }
92 1
93 1
        return await($factory->create($dsn), $loop);
94 1
    }
95
96 1
    private function getDefaultTransportFactory(LoopInterface $loop): TransportFactory
97
    {
98
        return new TransportFactory([
99
            new RedisTransportFactory($loop, $this->logger()),
100 1
            new PHPTransportFactory(),
101
        ]);
102
    }
103 1
104
    private function createStorage(LoopInterface $loop): StorageInterface
105 1
    {
106 1
        $factory = $this->storageFactory ?? $this->getDefaultStorageFactory($loop);
107
        $dsn = $this->config[Configuration::STORAGE_URL]
108 1
            ?? $this->config[Configuration::TRANSPORT_URL];
109
110 1
        if (!$factory->supports($dsn)) {
111
            throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn));
112
        }
113
114 1
        return await($factory->create($dsn), $loop);
115
    }
116
117
    private function getDefaultStorageFactory(LoopInterface $loop): StorageFactory
118
    {
119
        return new StorageFactory([
120
            new RedisStorageFactory($loop, $this->logger()),
121
            new PHPStorageFactory(),
122
            new NullStorageFactory(),
123
        ]);
124
    }
125
126
    private function createMetricsHandler(LoopInterface $loop): MetricsHandlerInterface
127
    {
128
        $factory = $this->metricsHandlerFactory ?? $this->getDefaultMetricsHandlerFactory($loop);
129
        $dsn = $this->config[Configuration::METRICS_URL]
130
            ?? $this->config[Configuration::STORAGE_URL]
131
            ?? $this->config[Configuration::TRANSPORT_URL];
132
133
        if (!$factory->supports($dsn)) {
134
            throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn));
135
        }
136
137
        return await($factory->create($dsn), $loop);
138
    }
139
140
    private function getDefaultMetricsHandlerFactory(LoopInterface $loop): MetricsHandlerFactory
141
    {
142
        return new MetricsHandlerFactory([
143
            new PHPMetricsHandlerFactory(),
144
            new RedisMetricsHandlerFactory($loop, $this->logger()),
145
        ]);
146
    }
147
}
148