ElasticSearch   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 25
c 1
b 0
f 0
dl 0
loc 57
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 32 5
A getUri() 0 6 1
1
<?php
2
namespace Health\Checks\Servers;
3
4
use Health\Checks\BaseCheck;
5
use Health\Checks\HealthCheckInterface;
6
use Health\Checks\Traits\HttpClientTrait;
7
8
/**
9
 * ElasticSearch Check
10
 */
11
class ElasticSearch extends BaseCheck implements HealthCheckInterface
12
{
13
14
    use HttpClientTrait;
15
16
    const DEFAULT_URL = "http://localhost:9200";
17
18
    /**
19
     *
20
     * {@inheritdoc}
21
     * @see \Health\Checks\HealthCheckInterface::call()
22
     */
23
    public function call()
24
    {
25
        $builder = $this->getBuilder();
26
27
        try {
28
            $uri = $this->getUri();
29
30
            /** @var \Psr\Http\Message\ResponseInterface $response */
31
            $response = $this->getHttpClient()->get($uri);
32
33
            $statusCode = $response->getStatusCode();
34
            $status = '';
35
36
            if ($response->getStatusCode() == 200) {
37
                $response = json_decode($response->getBody()->getContents());
38
                $status = $response->status ?? 'n/a';
39
40
                if ($status == 'green' || $status == 'yellow') {
41
                    $builder->up();
42
                } else {
43
                    $builder->down();
44
                }
45
            }
46
47
            $builder->withData('uri', $uri)
48
                ->withData('status', $status)
49
                ->withData('http_status', $statusCode);
50
        } catch (\Exception $exception) {
51
            $builder->down()->withData('error', $exception->getMessage());
52
        }
53
54
        return $builder->build();
55
    }
56
57
    /**
58
     * Get Cluster Health Uri
59
     *
60
     * @return string
61
     */
62
    private function getUri()
63
    {
64
        $uri = $this->getParam('uri', self::DEFAULT_URL);
65
        $uri = rtrim($uri, "/") . "/_cluster/health";
66
67
        return $uri;
68
    }
69
}
70