Passed
Push — master ( 44e0d6...4ae575 )
by Bret R.
01:51 queued 17s
created

ElasticsearchCheck::checkStatus()   B

Complexity

Conditions 9
Paths 35

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 35
nop 0
dl 0
loc 34
rs 8.0555
c 0
b 0
f 0
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
use Exception;
6
use function PHPUnit\Framework\isEmpty;
7
8
class ElasticsearchCheck extends AbstractCheck
9
{
10
11
    /**
12
     * @var \Elasticsearch\Client|\Elastic\Elasticsearch\Client
0 ignored issues
show
Bug introduced by
The type Elasticsearch\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
     */
14
    protected $client;
15
16
    /**
17
     * @var array
18
     */
19
    protected $indices;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param string $label
25
     * @param \Elasticsearch\Client|\Elastic\Elasticsearch\Client $client
26
     * @param array $indices Indices to check for
27
     */
28
    public function __construct(string $label, \Elasticsearch\Client|\Elastic\Elasticsearch\Client $client, array $indices = [])
29
    {
30
        parent::__construct($label);
31
32
        $this->client = $client;
33
        $this->indices = $indices;
34
    }
35
36
    /**
37
     * Check callback
38
     *
39
     * @return Result
40
     */
41
    public function checkStatus(): Result
42
    {
43
        $result = new Result($this->label);
44
        try {
45
            $info = $this->client->info();
46
            $versionParts = explode('.', $info['version']['number']);
47
            $esMajorVersion = (int)array_shift($versionParts);
48
            if ($esMajorVersion >= 8) {
49
                if ($this->client->ping()->asBool() !== true) {
0 ignored issues
show
Bug introduced by
The method asBool() does not exist on Http\Promise\Promise. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                if ($this->client->ping()->/** @scrutinizer ignore-call */ asBool() !== true) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
                    $result->setError("Elasticsearch is not reachable (ping failed)");
51
                    return $result;
52
                }
53
            } else {
54
                if ($this->client->ping() !== true) {
0 ignored issues
show
introduced by
The condition $this->client->ping() !== true is always true.
Loading history...
55
                    $result->setError("Elasticsearch is not reachable (ping failed)");
56
                    return $result;
57
                }
58
            }
59
60
            foreach ($this->indices as $index) {
61
                if ($esMajorVersion >= 8) {
62
                    if (!$this->client->indices()->exists(['index' => $index])->asBool()) {
63
                        $result->setError("Index '$index' does not exist");
64
                    }
65
                } else {
66
                    if (!$this->client->indices()->exists(['index' => $index])) {
67
                        $result->setError("Index '$index' does not exist");
68
                    }
69
                }
70
            }
71
        } catch (Exception $e) {
72
            $result->setError($e->getMessage());
73
        }
74
        return $result;
75
    }
76
}
77