Passed
Push — master ( abd2be...1dc290 )
by BENOIT
02:33
created

HubFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 26
ccs 17
cts 17
cp 1
crap 1
rs 9.7
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();
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

44
        /** @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...
45 2
        $this->transportFactory = $transportFactory ?? TransportFactory::default($config, $this->logger);
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

45
        $this->transportFactory = $transportFactory ?? TransportFactory::default($config, /** @scrutinizer ignore-deprecated */ $this->logger);

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...
Bug introduced by
It seems like $this->logger can also be of type null; however, parameter $logger of BenTools\MercurePHP\Tran...sportFactory::default() does only seem to accept Psr\Log\LoggerInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $this->transportFactory = $transportFactory ?? TransportFactory::default($config, /** @scrutinizer ignore-type */ $this->logger);
Loading history...
46 2
        $this->storageFactory = $storageFactory ?? StorageFactory::default($config, $this->logger);
0 ignored issues
show
Bug introduced by
It seems like $this->logger can also be of type null; however, parameter $logger of BenTools\MercurePHP\Stor...orageFactory::default() does only seem to accept Psr\Log\LoggerInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

46
        $this->storageFactory = $storageFactory ?? StorageFactory::default($config, /** @scrutinizer ignore-type */ $this->logger);
Loading history...
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

46
        $this->storageFactory = $storageFactory ?? StorageFactory::default($config, /** @scrutinizer ignore-deprecated */ $this->logger);

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...
47 2
        $this->metricsHandlerFactory = $metricsHandlerFactory ?? MetricsHandlerFactory::default($config, $this->logger);
0 ignored issues
show
Bug introduced by
It seems like $this->logger can also be of type null; however, parameter $logger of BenTools\MercurePHP\Metr...ndlerFactory::default() does only seem to accept Psr\Log\LoggerInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

47
        $this->metricsHandlerFactory = $metricsHandlerFactory ?? MetricsHandlerFactory::default($config, /** @scrutinizer ignore-type */ $this->logger);
Loading history...
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

47
        $this->metricsHandlerFactory = $metricsHandlerFactory ?? MetricsHandlerFactory::default($config, /** @scrutinizer ignore-deprecated */ $this->logger);

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...
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, $loop))
62 1
                ->withTransport($transport)
63 1
                ->withStorage($storage)
64 1
                ->withLogger($this->logger())
65
            ,
66 1
            (new PublishController($publisherAuthenticator))
67 1
                ->withTransport($transport)
68 1
                ->withStorage($storage)
69 1
                ->withLogger($this->logger())
70
            ,
71
        ];
72
73 1
        $requestHandler = new RequestHandler($controllers);
74
75 1
        return new Hub($this->config, $requestHandler, $metricsHandler, $this->logger());
76
    }
77
78 2
    private function createTransport(EventLoop\LoopInterface $loop): TransportInterface
79
    {
80 2
        $factory = $this->transportFactory;
81 2
        $dsn = $this->config[Configuration::TRANSPORT_URL];
82
83 2
        if (!$factory->supports($dsn)) {
84 1
            throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn));
85
        }
86
87 1
        return await($factory->create($dsn, $loop), $loop);
88
    }
89
90 1
    private function createStorage(EventLoop\LoopInterface $loop): StorageInterface
91
    {
92 1
        $factory = $this->storageFactory;
93 1
        $dsn = $this->config[Configuration::STORAGE_URL]
94 1
            ?? $this->config[Configuration::TRANSPORT_URL];
95
96 1
        if (!$factory->supports($dsn)) {
97
            throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn));
98
        }
99
100 1
        return await($factory->create($dsn, $loop), $loop);
101
    }
102
103 1
    private function createMetricsHandler(EventLoop\LoopInterface $loop): MetricsHandlerInterface
104
    {
105 1
        $factory = $this->metricsHandlerFactory;
106 1
        $dsn = $this->config[Configuration::METRICS_URL]
107
            ?? $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 metrics handler DSN %s', $dsn));
112
        }
113
114 1
        return await($factory->create($dsn, $loop), $loop);
115
    }
116
}
117