Completed
Push — master ( 0aa8ac...a01137 )
by Bret R.
01:24
created

ElasticsearchCheck::checkStatus()   A

Complexity

Conditions 5
Paths 11

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
nc 11
cc 5
nop 0
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
use Elasticsearch\Client;
6
use Exception;
7
8
class ElasticsearchCheck extends AbstractCheck
9
{
10
11
    /**
12
     * @var Client
13
     */
14
    protected $client;
15
16
    /**
17
     * @var array
18
     */
19
    protected $indices;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param $label
25
     * @param Client $client
26
     * @param array $indices Indices to check for
27
     */
28
    public function __construct(string $label, Client $client, array $indices = [])
29
    {
30
        parent::__construct($label);
31
        $this->client = $client;
32
        $this->indices = $indices;
33
    }
34
35
    /**
36
     * Check callback
37
     *
38
     * @return Result
39
     */
40
    public function checkStatus(): Result
41
    {
42
        $result = new Result($this->label);
43
        try {
44
            if ($this->client->ping() !== true) {
45
                $result->setSuccess(false);
46
                return $result;
47
            }
48
            foreach ($this->indices as $index) {
49
                if (!$this->client->indices()->exists(['index' => $index])) {
50
                    $result->setSuccess(false);
51
                    $result->setError("Index '$index' does not exist");
52
                }
53
            }
54
        } catch (Exception $e) {
55
            $result->setSuccess(false);
56
            $result->setError($e->getMessage());
57
        }
58
        return $result;
59
    }
60
}
61