Passed
Pull Request — master (#22)
by
unknown
07:08
created

RedisMetricsHandler::decrementUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace BenTools\MercurePHP\Metrics\Redis;
4
5
use BenTools\MercurePHP\Metrics\MetricsHandlerInterface;
6
use Clue\React\Redis\Client as AsynchronousClient;
7
use React\Promise\PromiseInterface;
8
9
use function React\Promise\all;
10
11
final class RedisMetricsHandler implements MetricsHandlerInterface
12
{
13
    /**
14
     * @var AsynchronousClient
15
     */
16
    private AsynchronousClient $client;
17
18
    public function __construct(AsynchronousClient $client)
19
    {
20
        $this->client = $client;
21
    }
22
23
    public function resetUsers(string $localAddress): PromiseInterface
24
    {
25
        /** @phpstan-ignore-next-line */
26
        return $this->client->set('users:' . $localAddress, 0);
27
    }
28
29
    public function incrementUsers(string $localAddress): PromiseInterface
30
    {
31
        /** @phpstan-ignore-next-line */
32
        return $this->client->incr('users:' . $localAddress);
33
    }
34
35
    public function decrementUsers(string $localAddress): PromiseInterface
36
    {
37
        /** @phpstan-ignore-next-line */
38
        return $this->client->decr('users:' . $localAddress);
39
    }
40
41
    public function getNbUsers(): PromiseInterface
42
    {
43
        /** @phpstan-ignore-next-line */
44
        return $this->client->keys('users:*')
45
            ->then(
46
                function (array $keys) {
47
                    $promises = [];
48
                    foreach ($keys as $key) {
49
                        $promises[] = $this->client->get($key); /** @phpstan-ignore-line */
50
                    }
51
52
                    return all($promises)->then(fn (array $results): int => \array_sum($results));
53
                }
54
            );
55
    }
56
}
57