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

lib/Elastica/Node/Stats.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 Stats
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
     * Create new stats for node.
39
     *
40
     * @param \Elastica\Node $node Elastica node object
41
     */
42
    public function __construct(BaseNode $node)
43
    {
44
        $this->_node = $node;
45
        $this->refresh();
46
    }
47
48
    /**
49
     * Returns all node stats as array based on the arguments.
50
     *
51
     * Several arguments can be use
52
     * get('index', 'test', 'example')
53
     *
54
     * @return array Node stats for the given field or null if not found
55
     */
56 View Code Duplication
    public function get()
57
    {
58
        $data = $this->getData();
59
60
        foreach (func_get_args() as $arg) {
61
            if (isset($data[$arg])) {
62
                $data = $data[$arg];
63
            } else {
64
                return;
65
            }
66
        }
67
68
        return $data;
69
    }
70
71
    /**
72
     * Returns all stats data.
73
     *
74
     * @return array Data array
75
     */
76
    public function getData()
77
    {
78
        return $this->_data;
79
    }
80
81
    /**
82
     * Returns node object.
83
     *
84
     * @return \Elastica\Node Node object
85
     */
86
    public function getNode()
87
    {
88
        return $this->_node;
89
    }
90
91
    /**
92
     * Returns response object.
93
     *
94
     * @return \Elastica\Response Response object
95
     */
96
    public function getResponse()
97
    {
98
        return $this->_response;
99
    }
100
101
    /**
102
     * Reloads all nodes information. Has to be called if informations changed.
103
     *
104
     * @return \Elastica\Response Response object
105
     */
106
    public function refresh()
107
    {
108
        $path = '_nodes/'.$this->getNode()->getName().'/stats';
109
        $this->_response = $this->getNode()->getClient()->request($path, Request::GET);
110
        $data = $this->getResponse()->getData();
111
        $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...
112
    }
113
}
114