Completed
Push — master ( 3a2d29...d0475e )
by Nicolas
02:57
created

lib/Elastica/Node/Info.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica\Node;
3
4
use Elastica\Node as BaseNode;
5
use Elastica\Request;
6
7
/**
8
 * Elastica cluster node object.
9
 *
10
 * @author Nicolas Ruflin <[email protected]>
11
 *
12
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
13
 */
14
class Info
15
{
16
    /**
17
     * Response.
18
     *
19
     * @var \Elastica\Response Response object
20
     */
21
    protected $_response;
22
23
    /**
24
     * Stats data.
25
     *
26
     * @var array stats data
27
     */
28
    protected $_data = [];
29
30
    /**
31
     * Node.
32
     *
33
     * @var \Elastica\Node Node object
34
     */
35
    protected $_node;
36
37
    /**
38
     * Query parameters.
39
     *
40
     * @var array
41
     */
42
    protected $_params = [];
43
44
    /**
45
     * Unique node id.
46
     *
47
     * @var string
48
     */
49
    protected $_id;
50
51
    /**
52
     * Create new info object for node.
53
     *
54
     * @param \Elastica\Node $node   Node object
55
     * @param array          $params List of params to return. Can be: settings, os, process, jvm, thread_pool, network, transport, http
56
     */
57
    public function __construct(BaseNode $node, array $params = [])
58
    {
59
        $this->_node = $node;
60
        $this->refresh($params);
61
    }
62
63
    /**
64
     * Returns the entry in the data array based on the params.
65
     * Several params possible.
66
     *
67
     * Example 1: get('os', 'mem', 'total') returns total memory of the system the
68
     * node is running on
69
     * Example 2: get('os', 'mem') returns an array with all mem infos
70
     *
71
     * @return mixed Data array entry or null if not found
72
     */
73 View Code Duplication
    public function get()
74
    {
75
        $data = $this->getData();
76
77
        foreach (func_get_args() as $arg) {
78
            if (isset($data[$arg])) {
79
                $data = $data[$arg];
80
            } else {
81
                return;
82
            }
83
        }
84
85
        return $data;
86
    }
87
88
    /**
89
     * Return port of the node.
90
     *
91
     * @return string Returns Node port
92
     */
93 View Code Duplication
    public function getPort()
94
    {
95
        // Returns string in format: inet[/192.168.1.115:9201]
96
        $data = $this->get('http_address');
97
        $data = substr($data, 6, strlen($data) - 7);
98
        $data = explode(':', $data);
99
100
        return $data[1];
101
    }
102
103
    /**
104
     * Return IP of the node.
105
     *
106
     * @return string Returns Node ip address
107
     */
108 View Code Duplication
    public function getIp()
109
    {
110
        // Returns string in format: inet[/192.168.1.115:9201]
111
        $data = $this->get('http_address');
112
        $data = substr($data, 6, strlen($data) - 7);
113
        $data = explode(':', $data);
114
115
        return $data[0];
116
    }
117
118
    /**
119
     * Return data regarding plugins installed on this node.
120
     *
121
     * @return array plugin data
122
     *
123
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
124
     */
125
    public function getPlugins()
126
    {
127
        if (!in_array('plugins', $this->_params)) {
128
            //Plugin data was not retrieved when refresh() was called last. Get it now.
129
            $this->_params[] = 'plugins';
130
            $this->refresh($this->_params);
131
        }
132
133
        return $this->get('plugins');
134
    }
135
136
    /**
137
     * Check if the given plugin is installed on this node.
138
     *
139
     * @param string $name plugin name
140
     *
141
     * @return bool true if the plugin is installed, false otherwise
142
     */
143
    public function hasPlugin($name)
144
    {
145
        foreach ($this->getPlugins() as $plugin) {
146
            if ($plugin['name'] == $name) {
147
                return true;
148
            }
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * Return all info data.
156
     *
157
     * @return array Data array
158
     */
159
    public function getData()
160
    {
161
        return $this->_data;
162
    }
163
164
    /**
165
     * Return node object.
166
     *
167
     * @return \Elastica\Node Node object
168
     */
169
    public function getNode()
170
    {
171
        return $this->_node;
172
    }
173
174
    /**
175
     * @return string Unique node id
176
     */
177
    public function getId()
178
    {
179
        return $this->_id;
180
    }
181
182
    /**
183
     * @return string Node name
184
     */
185
    public function getName()
186
    {
187
        return $this->_data['name'];
188
    }
189
190
    /**
191
     * Returns response object.
192
     *
193
     * @return \Elastica\Response Response object
194
     */
195
    public function getResponse()
196
    {
197
        return $this->_response;
198
    }
199
200
    /**
201
     * Reloads all nodes information. Has to be called if informations changed.
202
     *
203
     * @param array $params Params to return (default none). Possible options: settings, os, process, jvm, thread_pool, network, transport, http, plugin
204
     *
205
     * @return \Elastica\Response Response object
206
     */
207
    public function refresh(array $params = [])
208
    {
209
        $this->_params = $params;
210
211
        $path = '_nodes/'.$this->getNode()->getId();
212
213
        if (!empty($params)) {
214
            $path .= '/';
215
            foreach ($params as $param) {
216
                $path .= $param.',';
217
            }
218
        }
219
220
        $this->_response = $this->getNode()->getClient()->request($path, Request::GET);
221
        $data = $this->getResponse()->getData();
222
223
        $this->_data = reset($data['nodes']);
0 ignored issues
show
Documentation Bug introduced by
It seems like reset($data['nodes']) of type * is incompatible with the declared type array of property $_data.

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...
224
        $this->_id = key($data['nodes']);
225
        $this->getNode()->setId($this->getId());
226
    }
227
}
228