Passed
Push — master ( 8e7e6d...9f9e2a )
by Dispositif
03:39
created

StatsRedis::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 14
rs 9.9666
cc 4
nc 3
nop 0
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure\Monitor;
11
12
use Exception;
13
use Redis;
14
use RuntimeException;
15
16
class StatsRedis implements StatsInterface
17
{
18
    protected Redis $redis;
19
20
    public function __construct()
21
    {
22
        if (!class_exists('Redis')) {
23
            throw new Exception('Stats ERROR : Redis NOT supported.');
24
        }
25
        $redis = new Redis();
26
        $redis->connect(
27
            getenv('REDIS_HOST') ?: '127.0.0.1',
28
            (int) getenv('REDIS_PORT')
29
        );
30
        if (!$redis->isConnected()) {
31
            throw new RuntimeException('Redis not connected');
32
        }
33
        $this->redis = $redis;
34
    }
35
36
    public function increment(string $tag): bool
37
    {
38
        $this->redis->incr('monthly.'.date('Ym.').$tag);
39
        $this->redis->incr('daily.'.date('Ymd.').$tag);
40
41
        return $this->redis->incr('total.'.$tag) !== false;
42
    }
43
}