ElasticsearchCanBeAccessed   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
ccs 0
cts 15
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A check() 0 22 3
A message() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
6
7
use BeyondCode\SelfDiagnosis\Checks\Check;
8
use Throwable;
9
10
class ElasticsearchCanBeAccessed implements Check
11
{
12
    private $message;
13
14
    /**
15
     * The name of the check.
16
     *
17
     * @param array $config
18
     * @return string
19
     */
20
    public function name(array $config): string
21
    {
22
        return trans('lodash::checks.elasticsearch_can_be_accessed.name');
23
    }
24
25
    /**
26
     * Perform the actual verification of this check.
27
     *
28
     * @param array $config
29
     * @return bool
30
     */
31
    public function check(array $config): bool
32
    {
33
        try {
34
35
            /** @var \Longman\LaravelLodash\Elasticsearch\ElasticsearchManagerContract $elasticsearch */
36
            $elasticsearch = app($config['client']);
37
38
            if (! $elasticsearch->getClient()->ping()) {
39
                $this->message = trans('lodash::checks.elasticsearch_can_be_accessed.message.not_accessible', [
40
                    'error' => 'Ping command was failed',
41
                ]);
42
43
                return false;
44
            }
45
        } catch (Throwable $e) {
46
            $this->message = $e->getMessage();
47
48
            return false;
49
        }
50
51
        return true;
52
    }
53
54
    /**
55
     * The error message to display in case the check does not pass.
56
     *
57
     * @param array $config
58
     * @return string
59
     */
60
    public function message(array $config): string
61
    {
62
        return trans('lodash::checks.elasticsearch_can_be_accessed.message.not_accessible', [
63
            'error' => $this->message,
64
        ]);
65
    }
66
}
67