Completed
Push — issue#824 ( 975739 )
by Guilherme
07:04
created

RedisServiceCheck::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Diagnostics;
12
13
use Predis\Client;
14
use Predis\PredisException;
15
use ZendDiagnostics\Check\CheckInterface;
16
use ZendDiagnostics\Result\Failure;
17
use ZendDiagnostics\Result\Success;
18
19
class RedisServiceCheck implements CheckInterface
20
{
21
    private const KEY_PREFIX = 'redis_check_';
22
23
    /**
24
     * @var Client
25
     */
26
    private $redis;
27
28
    /**
29
     * RedisServiceCheck constructor.
30
     * @param Client $redis
31
     */
32
    public function __construct(Client $redis = null)
33
    {
34
        $this->redis = $redis;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function check()
41
    {
42
        if ($this->redis === null) {
43
            return new Success('Redis is not configured. Nothing to test...');
44
        }
45
46
        $key = self::KEY_PREFIX.random_int(0, PHP_INT_MAX);
47
        $value = random_bytes(random_int(10, 255));
48
49
        try {
50
            $this->redis->set($key, $value);
51
            $response = $this->redis->get($key);
52
            $this->redis->del([$key]);
53
54
            if ($response === $value) {
55
                return new Success("Redis is working. Tested SET, GET and DEL");
56
            }
57
58
            return new Failure("Redis is not working properly. GET didn't return expected value.");
59
        } catch (PredisException $e) {
60
            return new Failure("Redis is not working properly. Exception: {$e->getMessage()}");
61
        }
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getLabel()
68
    {
69
        return 'Predis Redis';
70
    }
71
}
72