Passed
Push — master ( a3b46f...abd2be )
by BENOIT
03:03 queued 48s
created

HubFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
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))
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