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

RedisTransportFactory::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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