1<?php
2
3namespace Elastica;
4
5use Elastica\Exception\ResponseException;
6use Elasticsearch\Endpoints\Indices\Alias\Get;
7use Elasticsearch\Endpoints\Indices\Stats;
8
9/**
10 * Elastica general status.
11 *
12 * @author Nicolas Ruflin <spam@ruflin.com>
13 *
14 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
15 */
16class Status
17{
18    /**
19     * Contains all status infos.
20     *
21     * @var \Elastica\Response Response object
22     */
23    protected $_response;
24
25    /**
26     * Data.
27     *
28     * @var array Data
29     */
30    protected $_data;
31
32    /**
33     * Client object.
34     *
35     * @var \Elastica\Client Client object
36     */
37    protected $_client;
38
39    /**
40     * Constructs Status object.
41     *
42     * @param \Elastica\Client $client Client object
43     */
44    public function __construct(Client $client)
45    {
46        $this->_client = $client;
47    }
48
49    /**
50     * Returns status data.
51     *
52     * @return array Status data
53     */
54    public function getData()
55    {
56        if (\is_null($this->_data)) {
57            $this->refresh();
58        }
59
60        return $this->_data;
61    }
62
63    /**
64     * Returns a list of the existing index names.
65     *
66     * @return array Index names list
67     */
68    public function getIndexNames()
69    {
70        $data = $this->getData();
71
72        return \array_keys($data['indices']);
73    }
74
75    /**
76     * Checks if the given index exists.
77     *
78     * @param string $name Index name to check
79     *
80     * @return bool True if index exists
81     */
82    public function indexExists($name)
83    {
84        return \in_array($name, $this->getIndexNames());
85    }
86
87    /**
88     * Checks if the given alias exists.
89     *
90     * @param string $name Alias name
91     *
92     * @return bool True if alias exists
93     */
94    public function aliasExists($name)
95    {
96        return \count($this->getIndicesWithAlias($name)) > 0;
97    }
98
99    /**
100     * Returns an array with all indices that the given alias name points to.
101     *
102     * @param string $alias Alias name
103     *
104     * @return array|\Elastica\Index[] List of Elastica\Index
105     */
106    public function getIndicesWithAlias($alias)
107    {
108        $endpoint = new Get();
109        $endpoint->setName($alias);
110
111        $response = null;
112
113        try {
114            $response = $this->_client->requestEndpoint($endpoint);
115        } catch (ResponseException $e) {
116            // 404 means the index alias doesn't exist which means no indexes have it.
117            if (404 === $e->getResponse()->getStatus()) {
118                return [];
119            }
120            // If we don't have a 404 then this is still unexpected so rethrow the exception.
121            throw $e;
122        }
123        $indices = [];
124        foreach ($response->getData() as $name => $unused) {
125            $indices[] = new Index($this->_client, $name);
126        }
127
128        return $indices;
129    }
130
131    /**
132     * Returns response object.
133     *
134     * @return \Elastica\Response Response object
135     */
136    public function getResponse()
137    {
138        if (\is_null($this->_response)) {
139            $this->refresh();
140        }
141
142        return $this->_response;
143    }
144
145    /**
146     * Return shards info.
147     *
148     * @return array Shards info
149     */
150    public function getShards()
151    {
152        $data = $this->getData();
153
154        return $data['shards'];
155    }
156
157    /**
158     * Refresh status object.
159     */
160    public function refresh()
161    {
162        $this->_response = $this->_client->requestEndpoint(new Stats());
163        $this->_data = $this->getResponse()->getData();
164    }
165}
166