Completed
Push — master ( 4e2c90...d09505 )
by Dmitry
07:02
created

ElasticsearchPingCheck::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace TonicHealthCheck\Check\Elasticsearch\Ping;
4
5
use Elasticsearch\Client as ElasticsearchClient;
6
use Elasticsearch\Common\Exceptions\ElasticsearchException;
7
use Exception;
8
use TonicHealthCheck\Check\Elasticsearch\AbstractElasticsearchCheck;
9
10
/**
11
 * Class ElasticsearchPingCheck.
12
 */
13
class ElasticsearchPingCheck extends AbstractElasticsearchCheck
14
{
15
    const CHECK = 'elasticsearch-ping-check';
16
17
    /**
18
     * @var ElasticsearchClient
19
     */
20
    protected $client;
21
22
    /**
23
     * @var string
24
     */
25
    protected $index;
26
27
    /**
28
     * @param string              $checkNode
29
     * @param ElasticsearchClient $client
30
     */
31
    public function __construct($checkNode, ElasticsearchClient $client)
32
    {
33
        parent::__construct($checkNode);
34
        $this->setClient($client);
35
    }
36
37
    /**
38
     * Check elasticsearch client ping.
39
     *
40
     * @throws ElasticsearchPingCheckException
41
     * @throws Exception
42
     */
43
    public function performCheck()
44
    {
45
        try {
46
            if (!$this->getClient()->ping()) {
47
                throw ElasticsearchPingCheckException::pingFailed(
48
                    $this->getClient()->transport->lastConnection->getHost()
49
                );
50
            }
51
        } catch (Exception $e) {
52
            if (!$e instanceof ElasticsearchException) {
53
                throw $e;
54
            }
55
            throw ElasticsearchPingCheckException::internalGetProblem($e);
56
        }
57
    }
58
59
    /**
60
     * @return ElasticsearchClient
61
     */
62
    public function getClient()
63
    {
64
        return $this->client;
65
    }
66
67
    /**
68
     * @param ElasticsearchClient $client
69
     */
70
    protected function setClient(ElasticsearchClient $client)
71
    {
72
        $this->client = $client;
73
    }
74
}
75