RedisTransportFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 74
ccs 34
cts 42
cp 0.8095
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 3 1
A create() 0 58 2
1
<?php
2
3
namespace BenTools\MercurePHP\Transport\Redis;
4
5
use BenTools\MercurePHP\Helpers\RedisHelper;
6
use BenTools\MercurePHP\Transport\TransportFactoryInterface;
7
use Clue\React\Redis\Client as AsynchronousClient;
8
use Clue\React\Redis\Factory;
9
use Psr\Log\LoggerInterface;
10
use React\EventLoop\LoopInterface;
11
use React\Promise\PromiseInterface;
12
13
use function React\Promise\all;
14
15
final class RedisTransportFactory implements TransportFactoryInterface
16
{
17
    private LoopInterface $loop;
18
    private LoggerInterface $logger;
19
20 6
    public function __construct(LoopInterface $loop, LoggerInterface $logger)
21
    {
22 6
        $this->loop = $loop;
23 6
        $this->logger = $logger;
24
    }
25 5
26
    public function supports(string $dsn): bool
27 5
    {
28
        return RedisHelper::isRedisDSN($dsn);
29
    }
30 1
31
    public function create(string $dsn): PromiseInterface
32 1
    {
33
        $factory = new Factory($this->loop);
34 1
        $promises = [
35 1
            'subscriber' => $factory->createClient($dsn)
36
                ->then(
37 1
                    function (AsynchronousClient $client) {
38 1
                        $client->on(
39
                            'close',
40
                            function () {
41
                                $this->logger->error('Connection closed.');
42 1
                                $this->loop->stop();
43
                            }
44
                        );
45 1
46 1
                        return $client;
47
                    },
48
                    function (\Exception $exception) {
49
                        $this->loop->stop();
50 1
                        $this->logger->error($exception->getMessage());
51
                    }
52 1
                ),
53 1
            'publisher' => $factory->createClient($dsn)
54
                ->then(
55 1
                    function (AsynchronousClient $client) {
56 1
                        $client->on(
57
                            'close',
58
                            function () {
59
                                $this->logger->error('Connection closed.');
60 1
                                $this->loop->stop();
61
                            }
62
                        );
63 1
64 1
                        return $client;
65
                    },
66
                    function (\Exception $exception) {
67
                        $this->loop->stop();
68 1
                        $this->logger->error($exception->getMessage());
69
                    }
70
                ),
71
        ];
72 1
73 1
        return all($promises)
74
            ->then(
75 1
                function (iterable $results): array {
76 1
                    $clients = [];
77 1
                    foreach ($results as $key => $client) {
78
                        $clients[$key] = $client;
79
                    }
80
81 1
                    RedisHelper::testAsynchronousClient($clients['subscriber'], $this->loop, $this->logger);
82 1
                    RedisHelper::testAsynchronousClient($clients['publisher'], $this->loop, $this->logger);
83
84 1
                    return $clients;
85 1
                }
86
            )
87 1
            ->then(
88 1
                fn (array $clients) => new RedisTransport($clients['subscriber'], $clients['publisher'])
89 1
            );
90 1
    }
91
}
92