1<?php
2
3namespace Elastica;
4
5use Elastica\Node\Info;
6use Elastica\Node\Stats;
7
8/**
9 * Elastica cluster node object.
10 *
11 * @author Nicolas Ruflin <spam@ruflin.com>
12 */
13class Node
14{
15    /**
16     * Client.
17     *
18     * @var \Elastica\Client
19     */
20    protected $_client;
21
22    /**
23     * @var string Unique node id
24     */
25    protected $_id;
26
27    /**
28     * Node name.
29     *
30     * @var string Node name
31     */
32    protected $_name;
33
34    /**
35     * Node stats.
36     *
37     * @var \Elastica\Node\Stats|null Node Stats
38     */
39    protected $_stats;
40
41    /**
42     * Node info.
43     *
44     * @var \Elastica\Node\Info|null Node info
45     */
46    protected $_info;
47
48    /**
49     * Create a new node object.
50     *
51     * @param string           $id     Node id or name
52     * @param \Elastica\Client $client Node object
53     */
54    public function __construct($id, Client $client)
55    {
56        $this->_client = $client;
57        $this->setId($id);
58    }
59
60    /**
61     * @return string Unique node id. Can also be name if id not exists.
62     */
63    public function getId()
64    {
65        return $this->_id;
66    }
67
68    /**
69     * @param string $id Node id
70     *
71     * @return $this Refreshed object
72     */
73    public function setId($id)
74    {
75        $this->_id = $id;
76
77        return $this->refresh();
78    }
79
80    /**
81     * Get the name of the node.
82     *
83     * @return string Node name
84     */
85    public function getName()
86    {
87        if (empty($this->_name)) {
88            $this->_name = $this->getInfo()->getName();
89        }
90
91        return $this->_name;
92    }
93
94    /**
95     * Returns the current client object.
96     *
97     * @return \Elastica\Client Client
98     */
99    public function getClient()
100    {
101        return $this->_client;
102    }
103
104    /**
105     * Return stats object of the current node.
106     *
107     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html
108     *
109     * @return \Elastica\Node\Stats Node stats
110     */
111    public function getStats()
112    {
113        if (!$this->_stats) {
114            $this->_stats = new Stats($this);
115        }
116
117        return $this->_stats;
118    }
119
120    /**
121     * Return info object of the current node.
122     *
123     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
124     *
125     * @return \Elastica\Node\Info Node info object
126     */
127    public function getInfo()
128    {
129        if (!$this->_info) {
130            $this->_info = new Info($this);
131        }
132
133        return $this->_info;
134    }
135
136    /**
137     * Refreshes all node information.
138     *
139     * This should be called after updating a node to refresh all information
140     */
141    public function refresh()
142    {
143        $this->_stats = null;
144        $this->_info = null;
145    }
146}
147