Completed
Pull Request — 3.6 (#1940)
by Sander
12:35 queued 11s
created

ElasticSearchUtil::getESVersionInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Helper;
4
5
use Elasticsearch\ClientBuilder;
6
use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
7
8
/**
9
 * Class ElasticSearchUtil
10
 */
11
final class ElasticSearchUtil
12
{
13
    /** @var array */
14
    private static $esClientInfo;
15
16
    /**
17
     * @return bool
18
     */
19
    public static function useVersion6()
20
    {
21
        if (PHP_MAJOR_VERSION < 7 || !class_exists('\Elastica\Tool\CrossIndex')) {
22
            return false;
23
        }
24
25
        $info = self::getESVersionInfo();
26
27
        if (null !== $info) {
28
            $versionParts = explode('.', $info['version']['number']);
29
            $majorVersion = $versionParts[0];
30
31
            return ($majorVersion > 2);
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    private static function getESVersionInfo()
41
    {
42
        try {
43
            if (null === self::$esClientInfo) {
44
                $client = ClientBuilder::create()->build();
45
                self::$esClientInfo = $client->info();
46
            }
47
        } catch (NoNodesAvailableException $e) {
48
            self::$esClientInfo = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $esClientInfo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        }
50
51
        return self::$esClientInfo;
52
    }
53
}
54