Passed
Push — master ( a4037f...e3e9e9 )
by Christopher
01:15
created

src/ElasticsearchIndex.php (1 issue)

Labels
Severity
1
<?php
2
namespace Triadev\Es;
3
4
use Elasticsearch\Client;
5
use Triadev\Es\Business\Helper\Version;
6
use Triadev\Es\Contract\ElasticsearchClientContract;
7
use Triadev\Es\Contract\ElasticsearchIndexContract;
8
use Triadev\Es\Exception\Index\IndexFoundException;
9
use Triadev\Es\Exception\Index\IndexNotFoundException;
10
11
class ElasticsearchIndex implements ElasticsearchIndexContract
12
{
13
    use Version;
14
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
    
20
    /**
21
     * ElasticsearchIndex constructor.
22
     * @param ElasticsearchClientContract $clientBuilder
23
     */
24 19
    public function __construct(ElasticsearchClientContract $clientBuilder)
25
    {
26 19
        $this->client = $clientBuilder->getEsClient();
27 19
    }
28
    
29
    /**
30
     * Create index
31
     *
32
     * @param string $index
33
     * @param array $params
34
     * @param string|null $version
35
     * @return array
36
     * @throws IndexFoundException
37
     */
38 19
    public function createIndex(string $index, array $params, string $version = null) : array
39
    {
40 19
        if (!$this->existIndex([$index], $version)) {
41 16
            $params['index'] = $this->createIndexWithVersion($index, $version);
42
43 16
            return $this->client->indices()->create($params);
44
        }
45
46 4
        throw new IndexFoundException($index, $version);
47
    }
48
49
    /**
50
     * Delete index
51
     *
52
     * @param array $index
53
     * @param string|null $version
54
     * @return array
55
     * @throws IndexNotFoundException
56
     */
57 1
    public function deleteIndex(array $index, string $version = null) : array
58
    {
59 1
        $indices = [];
60
61 1
        foreach ($index as $i) {
62 1
            if ($this->existIndex([$i], $version)) {
63 1
                $indices[] = $this->createIndexWithVersion($i, $version);
64
            }
65
        }
66
67 1
        if (!empty($indices)) {
68 1
            return $this->client->indices()->delete([
69 1
                'index' => implode(',', $indices)
70
            ]);
71
        }
72
73
        throw new IndexNotFoundException(implode(',', $index), $version);
74
    }
75
76
    /**
77
     * Delete all indexes
78
     *
79
     * @return array
80
     */
81 14
    public function deleteAllIndexes() : array
82
    {
83 14
        return $this->client->indices()->delete([
84 14
            'index' => '_all'
85
        ]);
86
    }
87
88
    /**
89
     * Exist index
90
     *
91
     * @param array $index
92
     * @param string|null $version
93
     * @return bool
94
     */
95 19
    public function existIndex(array $index, ?string $version = null) : bool
96
    {
97 19
        $indices = [];
98
99 19
        foreach ($index as $i) {
100 19
            $indices[] = $this->createIndexWithVersion($i, $version);
101
        }
102
103 19
        return $this->client->indices()->exists([
104 19
            'index' => implode(',', $indices)
105
        ]);
106
    }
107
108
    /**
109
     * Get versioned indices
110
     *
111
     * @param string $index
112
     * @return array
113
     */
114 2
    public function getVersionedIndices(string $index) : array
115
    {
116 2
        return array_keys($this->client->indices()->get([
0 ignored issues
show
$this->client->indices()...rintf('%s_*', $index))) of type boolean is incompatible with the type array expected by parameter $input of array_keys(). ( Ignorable by Annotation )

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

116
        return array_keys(/** @scrutinizer ignore-type */ $this->client->indices()->get([
Loading history...
117 2
            'index' => sprintf("%s_*", $index)
118
        ]));
119
    }
120
121
    /**
122
     * Reindex
123
     *
124
     * @param string $index
125
     * @param string $from_version
126
     * @param string $to_version
127
     * @param array $params
128
     * @param array|null $source
129
     * @return array
130
     */
131
    public function reindex(
132
        string $index,
133
        string $from_version,
134
        string $to_version,
135
        array $params = [],
136
        ?array $source = null
137
    ) : array {
138
        $params['body'] = [
139
            'source' => [
140
                'index' => $this->createIndexWithVersion($index, $from_version)
141
            ],
142
            'dest' => [
143
                'index' => $this->createIndexWithVersion($index, $to_version)
144
            ]
145
        ];
146
147
        if (is_array($source)) {
148
            $params['body']['source']['_source'] = $source;
149
        }
150
151
        return $this->client->reindex($params);
152
    }
153
}
154