Completed
Push — develop ( 66c29c...10a2c8 )
by Stan
02:47
created

RedisRepository::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Krenor\Prometheus\Storage\Repositories;
4
5
use Predis\Client as Redis;
6
use Tightenco\Collect\Support\Collection;
7
use Krenor\Prometheus\Contracts\Repository;
8
9
class RedisRepository implements Repository
10
{
11
    /**
12
     * @var Redis
13
     */
14
    protected $redis;
15
16
    /**
17
     * RedisRepository constructor.
18
     *
19
     * @param Redis $redis
20
     */
21 6
    public function __construct(Redis $redis)
22
    {
23 6
        $this->redis = $redis;
24 6
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 6
    public function get(string $key): Collection
30
    {
31 6
        return new Collection(
32 6
            strpos($key, ':VALUES') === false
33 6
                ? $this->redis->hgetall($key)
34 6
                : $this->redis->lrange($key, 0, -1)
35
        );
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 4
    public function increment(string $key, string $field, float $value): void
42
    {
43 4
        $this->redis->hincrbyfloat($key, $field, $value);
44 4
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function decrement(string $key, string $field, float $value): void
50
    {
51 1
        $this->increment($key, $field, -abs($value));
52 1
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function set(string $key, string $field, $value, $override = true): void
58
    {
59 2
        $override
60 1
            ? $this->redis->hset($key, $field, $value)
61 1
            : $this->redis->hsetnx($key, $field, $value);
62 2
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function push(string $key, float $value): void
68
    {
69 1
        $this->redis->rpush($key, $value);
70 1
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 6
    public function flush(): bool
76
    {
77
        /** @var \Predis\Response\Status $response */
78 6
        $response = $this->redis->flushdb();
79
80 6
        return $response->getPayload() === 'OK';
81
    }
82
}
83