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
|
|||||
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
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
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. ![]() |
|||||
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
|
|||||
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 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths