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

Hub::init()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 0
dl 0
loc 32
ccs 18
cts 21
cp 0.8571
crap 5.0729
rs 9.2728
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\Helpers\LoggerAwareTrait;
7
use BenTools\MercurePHP\Metrics\MetricsHandlerInterface;
8
use BenTools\MercurePHP\Security\CORS;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
use React\EventLoop\LoopInterface;
15
use React\Http;
16
use React\Promise\PromiseInterface;
17
use React\Socket;
18
19
use function React\Promise\resolve;
20
21
final class Hub implements RequestHandlerInterface
22
{
23
    use LoggerAwareTrait;
24
25
    private array $config;
26
    private RequestHandlerInterface $requestHandler;
27
    private CORS $cors;
28
    private MetricsHandlerInterface $metricsHandler;
29
30 1
    public function __construct(
31
        array $config,
32
        RequestHandlerInterface $requestHandler,
33
        MetricsHandlerInterface $metricsHandler,
34
        ?LoggerInterface $logger = null
35
    ) {
36 1
        $this->config = $config;
37 1
        $this->requestHandler = $requestHandler;
38 1
        $this->metricsHandler = $metricsHandler;
39 1
        $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

39
        /** @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...
40 1
        $this->cors = new CORS($config);
41 1
    }
42
43
    public function run(LoopInterface $loop): void
44
    {
45
        $localAddress = $this->config[Configuration::ADDR];
46
        $socket = new Socket\Server($localAddress, $loop);
47
        $this->metricsHandler->resetUsers($localAddress);
48
        $loop->addPeriodicTimer(
49
            15,
50
            fn() => $this->metricsHandler->getNbUsers()->then(
51
                function (int $nbUsers) {
52
                    $memory = \memory_get_usage(true) / 1024 / 1024;
53
                    $this->logger()->debug("Users: {$nbUsers} - Memory: {$memory}MB");
54
                }
55
            )
56
        );
57
        $socket->on(
58
            'connection',
59
            function (Socket\ConnectionInterface $connection) use ($localAddress) {
60
                $this->metricsHandler->incrementUsers($localAddress);
61
                $connection->on('close', fn() => $this->metricsHandler->decrementUsers($localAddress));
62
            }
63
        );
64
        $server = new Http\Server($this);
65
        $server->listen($socket);
66
        $this->logger()->info("Server running at http://" . $localAddress);
67
        $loop->run();
68
    }
69
70 4
    public function handle(ServerRequestInterface $request): ResponseInterface
71
    {
72 4
        return $this->cors->decorateResponse(
73 4
            $request,
74 4
            $this->requestHandler->handle($request)
75
        );
76
    }
77
78 4
    public function __invoke(ServerRequestInterface $request): PromiseInterface
79
    {
80 4
        return resolve($this->handle($request));
81
    }
82
}
83