Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/Node/Info.php (4 issues)

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
3
namespace Elastica\Node;
4
5
use Elastica\Node as BaseNode;
6
7
/**
8
 * Elastica cluster node object.
9
 *
10
 * @author Nicolas Ruflin <[email protected]>
11
 *
12
 * @see 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()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
     * @see 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
        $endpoint = new \Elasticsearch\Endpoints\Cluster\Nodes\Info();
212
        $endpoint->setNodeID($this->getNode()->getId());
213
214
        if (!empty($params)) {
215
            $endpoint->setMetric($params);
216
        }
217
218
        $this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint);
219
        $data = $this->getResponse()->getData();
220
221
        $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...
222
        $this->_id = key($data['nodes']);
223
        $this->getNode()->setId($this->getId());
224
    }
225
}
226