1<?php
2
3namespace Elastica\Cluster\Health;
4
5/**
6 * Wraps status information for a shard.
7 *
8 * @author Ray Ward <ray.ward@bigcommerce.com>
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
11 */
12class Shard
13{
14    /**
15     * @var int the shard index/number
16     */
17    protected $_shardNumber;
18
19    /**
20     * @var array the shard health data
21     */
22    protected $_data;
23
24    /**
25     * @param int   $shardNumber the shard index/number
26     * @param array $data        the shard health data
27     */
28    public function __construct(int $shardNumber, array $data)
29    {
30        $this->_shardNumber = $shardNumber;
31        $this->_data = $data;
32    }
33
34    /**
35     * Gets the index/number of this shard.
36     */
37    public function getShardNumber(): int
38    {
39        return $this->_shardNumber;
40    }
41
42    /**
43     * Gets the status of this shard.
44     *
45     * @return string green, yellow or red
46     */
47    public function getStatus(): string
48    {
49        return $this->_data['status'];
50    }
51
52    /**
53     * Is the primary active?
54     */
55    public function isPrimaryActive(): bool
56    {
57        return $this->_data['primary_active'];
58    }
59
60    /**
61     * Is this shard active?
62     */
63    public function isActive(): bool
64    {
65        return 1 === $this->_data['active_shards'];
66    }
67
68    /**
69     * Is this shard relocating?
70     */
71    public function isRelocating(): bool
72    {
73        return 1 === $this->_data['relocating_shards'];
74    }
75
76    /**
77     * Is this shard initialized?
78     */
79    public function isInitialized(): bool
80    {
81        return 1 === $this->_data['initializing_shards'];
82    }
83
84    /**
85     * Is this shard unassigned?
86     */
87    public function isUnassigned(): bool
88    {
89        return 1 === $this->_data['unassigned_shards'];
90    }
91}
92