Completed
Push — master ( 7b686b...b265e2 )
by Avtandil
09:58
created

ElasticsearchCanBeAccessed::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
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 Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
13
14
use BeyondCode\SelfDiagnosis\Checks\Check;
15
16
class ElasticsearchCanBeAccessed implements Check
17
{
18
    private $message;
19
20
    /**
21
     * The name of the check.
22
     *
23
     * @param array $config
24
     * @return string
25
     */
26
    public function name(array $config): string
27
    {
28
        return trans('lodash::checks.elasticsearch_can_be_accessed.name');
29
    }
30
31
    /**
32
     * Perform the actual verification of this check.
33
     *
34
     * @param array $config
35
     * @return bool
36
     */
37
    public function check(array $config): bool
38
    {
39
        try {
40
41
            /** @var \Longman\LaravelLodash\ElasticSearch\ElasticSearchManagerContract $elasticsearch */
42
            $elasticsearch = app($config['client']);
43
44
            if (! $elasticsearch->getClient()->ping()) {
45
                $this->message = trans('lodash::checks.elasticsearch_can_be_accessed.message.not_accessible', [
46
                    'error' => 'Ping command was failed',
47
                ]);
48
49
                return false;
50
            }
51
        } catch (\Throwable $e) {
52
            $this->message = $e->getMessage();
53
54
            return false;
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * The error message to display in case the check does not pass.
62
     *
63
     * @param array $config
64
     * @return string
65
     */
66
    public function message(array $config): string
67
    {
68
        return trans('lodash::checks.elasticsearch_can_be_accessed.message.not_accessible', [
69
            'error' => $this->message,
70
        ]);
71
    }
72
}
73