RedisTransportFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 2.0438

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 37
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 58
ccs 28
cts 36
cp 0.7778
crap 2.0438
rs 9.328

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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