RedisCanBeAccessed   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
B check() 0 28 6
A message() 0 6 1
A testConnection() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
6
7
use BeyondCode\SelfDiagnosis\Checks\Check;
8
use Illuminate\Contracts\Redis\Factory as RedisFactory;
9
use Illuminate\Redis\Connections\PhpRedisClusterConnection;
10
use Illuminate\Redis\Connections\PhpRedisConnection;
11
use Illuminate\Support\Arr;
12
use Throwable;
13
14
class RedisCanBeAccessed implements Check
15
{
16
    private $message;
17
18
    /**
19
     * The name of the check.
20
     *
21
     * @param array $config
22
     * @return string
23
     */
24
    public function name(array $config): string
25
    {
26
        return trans('lodash::checks.redis_can_be_accessed.name');
27
    }
28
29
    /**
30
     * Perform the actual verification of this check.
31
     *
32
     * @param array $config
33
     * @return bool
34
     */
35
    public function check(array $config): bool
36
    {
37
        try {
38
            if (Arr::get($config, 'default_connection', true)) {
39
                if (! $this->testConnection()) {
40
                    $this->message = trans('lodash::checks.redis_can_be_accessed.message.default_cache');
41
42
                    return false;
43
                }
44
            }
45
46
            foreach (Arr::get($config, 'connections', []) as $connection) {
47
                if (! $this->testConnection($connection)) {
48
                    $this->message = trans('lodash::checks.redis_can_be_accessed.message.named_cache', [
49
                        'name' => $connection,
50
                    ]);
51
52
                    return false;
53
                }
54
            }
55
        } catch (Throwable $e) {
56
            $this->message = $e->getMessage();
57
58
            return false;
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * The error message to display in case the check does not pass.
66
     *
67
     * @param array $config
68
     * @return string
69
     */
70
    public function message(array $config): string
71
    {
72
        return trans('lodash::checks.redis_can_be_accessed.message.not_accessible', [
73
            'error' => $this->message,
74
        ]);
75
    }
76
77
    /**
78
     * Tests a redis connection and returns whether the connection is opened or not.
79
     *
80
     * @param string|null $name
81
     * @return bool
82
     */
83
    private function testConnection(?string $name = null): bool
84
    {
85
        $redis = app(RedisFactory::class);
86
        $connection = $redis->connection($name);
87
88
        // PHPRedis connects automatically
89
        if ($connection instanceof PhpRedisConnection || $connection instanceof PhpRedisClusterConnection) {
90
            return ! empty($connection->info());
91
        }
92
93
        $connection->connect();
94
95
        return $connection->isConnected();
96
    }
97
}
98