Passed
Pull Request — master (#8)
by BENOIT
01:53
created

RedisTransportFactory::create()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 2.0404

Importance

Changes 0
Metric Value
cc 2
eloc 37
nc 1
nop 1
dl 0
loc 58
ccs 29
cts 37
cp 0.7838
crap 2.0404
rs 9.328
c 0
b 0
f 0

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\LoggerAwareTrait;
6
use BenTools\MercurePHP\Helpers\RedisHelper;
7
use BenTools\MercurePHP\Transport\TransportFactoryInterface;
8
use Clue\React\Redis\Client as AsynchronousClient;
9
use Clue\React\Redis\Factory;
10
use Psr\Log\LoggerInterface;
11
use React\EventLoop\LoopInterface;
12
use React\Promise\PromiseInterface;
13
14
use function React\Promise\all;
15
16
final class RedisTransportFactory implements TransportFactoryInterface
17
{
18
    use LoggerAwareTrait;
19
20 6
    private LoopInterface $loop;
21
22 6
    public function __construct(LoopInterface $loop, ?LoggerInterface $logger = null)
23 6
    {
24
        $this->loop = $loop;
25 5
        $this->logger = $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

25
        /** @scrutinizer ignore-deprecated */ $this->logger = $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...
26
    }
27 5
28
    public function supports(string $dsn): bool
29
    {
30 1
        return RedisHelper::isRedisDSN($dsn);
31
    }
32 1
33
    public function create(string $dsn): PromiseInterface
34 1
    {
35 1
        $factory = new Factory($this->loop);
36
        $promises = [
37 1
            'subscriber' => $factory->createClient($dsn)
38 1
                ->then(
39
                    function (AsynchronousClient $client) {
40
                        $client->on(
41
                            'close',
42 1
                            function () {
43
                                $this->logger()->error('Connection closed.');
44
                                $this->loop->stop();
45 1
                            }
46 1
                        );
47
48
                        return $client;
49
                    },
50 1
                    function (\Exception $exception) {
51
                        $this->loop->stop();
52 1
                        $this->logger->error($exception->getMessage());
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

52
                        /** @scrutinizer ignore-deprecated */ $this->logger->error($exception->getMessage());

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
The method error() does not exist on null. ( Ignorable by Annotation )

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

52
                        $this->logger->/** @scrutinizer ignore-call */ 
53
                                       error($exception->getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53 1
                    }
54
                ),
55 1
            'publisher' => $factory->createClient($dsn)
56 1
                ->then(
57
                    function (AsynchronousClient $client) {
58
                        $client->on(
59
                            'close',
60 1
                            function () {
61
                                $this->logger()->error('Connection closed.');
62
                                $this->loop->stop();
63 1
                            }
64 1
                        );
65
66
                        return $client;
67
                    },
68 1
                    function (\Exception $exception) {
69
                        $this->loop->stop();
70
                        $this->logger()->error($exception->getMessage());
71
                    }
72 1
                ),
73 1
        ];
74
75 1
        return all($promises)
76 1
            ->then(
77 1
                function (iterable $results): array {
78
                    $clients = [];
79
                    foreach ($results as $key => $client) {
80
                        $clients[$key] = $client;
81 1
                    }
82 1
83
                    RedisHelper::testAsynchronousClient($clients['subscriber'], $this->loop, $this->logger());
84 1
                    RedisHelper::testAsynchronousClient($clients['publisher'], $this->loop, $this->logger());
85 1
86
                    return $clients;
87 1
                }
88 1
            )
89 1
            ->then(
90 1
                fn (array $clients) => new RedisTransport($clients['subscriber'], $clients['publisher'])
91
            );
92 1
    }
93
}
94