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

ElasticsearchPingCheck   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 7
c 1
b 1
f 1
lcom 1
cbo 3
dl 0
loc 62
ccs 0
cts 28
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A performCheck() 0 15 4
A getClient() 0 4 1
A setClient() 0 4 1
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