1<?php
2
3namespace Elastica;
4
5use Elastica\Cluster\Health;
6use Elastica\Cluster\Settings;
7use Elastica\Exception\NotImplementedException;
8use Elasticsearch\Endpoints\Cluster\State;
9
10/**
11 * Cluster information for elasticsearch.
12 *
13 * @author Nicolas Ruflin <spam@ruflin.com>
14 *
15 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html
16 */
17class Cluster
18{
19    /**
20     * Client.
21     *
22     * @var \Elastica\Client Client object
23     */
24    protected $_client;
25
26    /**
27     * Cluster state response.
28     *
29     * @var \Elastica\Response
30     */
31    protected $_response;
32
33    /**
34     * Cluster state data.
35     *
36     * @var array
37     */
38    protected $_data;
39
40    /**
41     * Creates a cluster object.
42     *
43     * @param \Elastica\Client $client Connection client object
44     */
45    public function __construct(Client $client)
46    {
47        $this->_client = $client;
48        $this->refresh();
49    }
50
51    /**
52     * Refreshes all cluster information (state).
53     */
54    public function refresh()
55    {
56        $this->_response = $this->_client->requestEndpoint(new State());
57        $this->_data = $this->getResponse()->getData();
58    }
59
60    /**
61     * Returns the response object.
62     *
63     * @return \Elastica\Response Response object
64     */
65    public function getResponse()
66    {
67        return $this->_response;
68    }
69
70    /**
71     * Return list of index names.
72     *
73     * @return array List of index names
74     */
75    public function getIndexNames()
76    {
77        return \array_keys($this->_data['metadata']['indices']);
78    }
79
80    /**
81     * Returns the full state of the cluster.
82     *
83     * @return array State array
84     *
85     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
86     */
87    public function getState()
88    {
89        return $this->_data;
90    }
91
92    /**
93     * Returns a list of existing node names.
94     *
95     * @return array List of node names
96     */
97    public function getNodeNames()
98    {
99        $data = $this->getState();
100        $nodeNames = [];
101        foreach ($data['nodes'] as $node) {
102            $nodeNames[] = $node['name'];
103        }
104
105        return $nodeNames;
106    }
107
108    /**
109     * Returns all nodes of the cluster.
110     *
111     * @return \Elastica\Node[]
112     */
113    public function getNodes()
114    {
115        $nodes = [];
116        $data = $this->getState();
117
118        foreach ($data['nodes'] as $id => $name) {
119            $nodes[] = new Node($id, $this->getClient());
120        }
121
122        return $nodes;
123    }
124
125    /**
126     * Returns the client object.
127     *
128     * @return \Elastica\Client Client object
129     */
130    public function getClient()
131    {
132        return $this->_client;
133    }
134
135    /**
136     * Returns the cluster information (not implemented yet).
137     *
138     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
139     *
140     * @param array $args Additional arguments
141     *
142     * @throws \Elastica\Exception\NotImplementedException
143     */
144    public function getInfo(array $args)
145    {
146        throw new NotImplementedException('not implemented yet');
147    }
148
149    /**
150     * Return Cluster health.
151     *
152     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
153     *
154     * @return \Elastica\Cluster\Health
155     */
156    public function getHealth()
157    {
158        return new Health($this->getClient());
159    }
160
161    /**
162     * Return Cluster settings.
163     *
164     * @return \Elastica\Cluster\Settings
165     */
166    public function getSettings()
167    {
168        return new Settings($this->getClient());
169    }
170}
171