Completed
Push — master ( 8ecc5c...250cbd )
by Nicolas
02:58
created

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