Completed
Push — master ( f8e5e4...589232 )
by Marcel
01:31
created

RedisCanBeAccessed   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
B check() 0 22 6
A message() 0 6 1
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Support\Facades\Redis;
6
7
class RedisCanBeAccessed implements Check
8
{
9
    private $message;
10
11
    /**
12
     * The name of the check.
13
     *
14
     * @param array $config
15
     * @return string
16
     */
17
    public function name(array $config): string
18
    {
19
        return trans('self-diagnosis::checks.redis_can_be_accessed.name');
20
    }
21
22
    /**
23
     * Perform the actual verification of this check.
24
     *
25
     * @param array $config
26
     * @return bool
27
     */
28
    public function check(array $config): bool
29
    {
30
        try {
31
            if (array_get($config, 'default_connection', true)) {
32
                if (!Redis::connection()->isConnected()) {
33
                    return false;
34
                }
35
            }
36
37
            foreach (array_get($config, 'connections', []) as $connection) {
38
                if (!Redis::connection($connection)->isConnected()) {
39
                    return false;
40
                }
41
            }
42
43
            return true;
44
        } catch (\Exception $e) {
45
            $this->message = $e->getMessage();
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * The error message to display in case the check does not pass.
53
     *
54
     * @param array $config
55
     * @return string
56
     */
57
    public function message(array $config): string
58
    {
59
        return trans('self-diagnosis::checks.redis_can_be_accessed.message', [
60
            'error' => $this->message,
61
        ]);
62
    }
63
}
64