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 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 Stats|null Node Stats
38     */
39    protected $_stats;
40
41    /**
42     * Node info.
43     *
44     * @var Info|null Node info
45     */
46    protected $_info;
47
48    public function __construct(string $id, Client $client)
49    {
50        $this->_client = $client;
51        $this->setId($id);
52    }
53
54    /**
55     * Returns the unique node id, this can also be name if the id does not exist.
56     */
57    public function getId(): string
58    {
59        return $this->_id;
60    }
61
62    public function setId(string $id): self
63    {
64        $this->_id = $id;
65        $this->refresh();
66
67        return $this;
68    }
69
70    /**
71     * Get the name of the node.
72     */
73    public function getName(): string
74    {
75        if (null === $this->_name) {
76            $this->_name = $this->getInfo()->getName();
77        }
78
79        return $this->_name;
80    }
81
82    /**
83     * Returns the current client object.
84     */
85    public function getClient(): Client
86    {
87        return $this->_client;
88    }
89
90    /**
91     * Return stats object of the current node.
92     *
93     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html
94     */
95    public function getStats(): Stats
96    {
97        if (!$this->_stats) {
98            $this->_stats = new Stats($this);
99        }
100
101        return $this->_stats;
102    }
103
104    /**
105     * Return info object of the current node.
106     *
107     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
108     */
109    public function getInfo(): Info
110    {
111        if (!$this->_info) {
112            $this->_info = new Info($this);
113        }
114
115        return $this->_info;
116    }
117
118    /**
119     * Refreshes all node information.
120     *
121     * This should be called after updating a node to refresh all information
122     */
123    public function refresh(): void
124    {
125        $this->_stats = null;
126        $this->_info = null;
127    }
128}
129