Completed
Push — master ( c44ec8...683f7d )
by Nicolas
02:49
created

Cluster::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Elastica;
3
4
use Elastica\Cluster\Health;
5
use Elastica\Cluster\Settings;
6
use Elastica\Exception\NotImplementedException;
7
8
/**
9
 * Cluster information for elasticsearch.
10
 *
11
 * @author Nicolas Ruflin <[email protected]>
12
 *
13
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html
14
 */
15
class Cluster
16
{
17
    /**
18
     * Client.
19
     *
20
     * @var \Elastica\Client Client object
21
     */
22
    protected $_client;
23
24
    /**
25
     * Cluster state response.
26
     *
27
     * @var \Elastica\Response
28
     */
29
    protected $_response;
30
31
    /**
32
     * Cluster state data.
33
     *
34
     * @var array
35
     */
36
    protected $_data;
37
38
    /**
39
     * Creates a cluster object.
40
     *
41
     * @param \Elastica\Client $client Connection client object
42
     */
43
    public function __construct(Client $client)
44
    {
45
        $this->_client = $client;
46
        $this->refresh();
47
    }
48
49
    /**
50
     * Refreshes all cluster information (state).
51
     */
52
    public function refresh()
53
    {
54
        $path = '_cluster/state';
55
        $this->_response = $this->_client->request($path, Request::GET);
56
        $this->_data = $this->getResponse()->getData();
57
    }
58
59
    /**
60
     * Returns the response object.
61
     *
62
     * @return \Elastica\Response Response object
63
     */
64
    public function getResponse()
65
    {
66
        return $this->_response;
67
    }
68
69
    /**
70
     * Return list of index names.
71
     *
72
     * @return array List of index names
73
     */
74
    public function getIndexNames()
75
    {
76
        return array_keys($this->_data['metadata']['indices']);
77
    }
78
79
    /**
80
     * Returns the full state of the cluster.
81
     *
82
     * @return array State array
83
     *
84
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
85
     */
86
    public function getState()
87
    {
88
        return $this->_data;
89
    }
90
91
    /**
92
     * Returns a list of existing node names.
93
     *
94
     * @return array List of node names
95
     */
96
    public function getNodeNames()
97
    {
98
        $data = $this->getState();
99
        $nodeNames = [];
100
        foreach ($data['nodes'] as $node) {
101
            $nodeNames[] = $node['name'];
102
        }
103
104
        return $nodeNames;
105
    }
106
107
    /**
108
     * Returns all nodes of the cluster.
109
     *
110
     * @return \Elastica\Node[]
111
     */
112
    public function getNodes()
113
    {
114
        $nodes = [];
115
        $data = $this->getState();
116
117
        foreach ($data['nodes'] as $id => $name) {
118
            $nodes[] = new Node($id, $this->getClient());
119
        }
120
121
        return $nodes;
122
    }
123
124
    /**
125
     * Returns the client object.
126
     *
127
     * @return \Elastica\Client Client object
128
     */
129
    public function getClient()
130
    {
131
        return $this->_client;
132
    }
133
134
    /**
135
     * Returns the cluster information (not implemented yet).
136
     *
137
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
138
     *
139
     * @param array $args Additional arguments
140
     *
141
     * @throws \Elastica\Exception\NotImplementedException
142
     */
143
    public function getInfo(array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        throw new NotImplementedException('not implemented yet');
146
    }
147
148
    /**
149
     * Return Cluster health.
150
     *
151
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
152
     *
153
     * @return \Elastica\Cluster\Health
154
     */
155
    public function getHealth()
156
    {
157
        return new Health($this->getClient());
158
    }
159
160
    /**
161
     * Return Cluster settings.
162
     *
163
     * @return \Elastica\Cluster\Settings
164
     */
165
    public function getSettings()
166
    {
167
        return new Settings($this->getClient());
168
    }
169
}
170