Completed
Push — master ( 45188d...35e2cb )
by Sam
02:25
created

Configuration::getInstanceByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Jalle19\StatusManager\Configuration;
4
5
/**
6
 * Class Configuration
7
 * @package   Jalle19\StatusManager\Configuration
8
 * @copyright Copyright &copy; Sam Stenvall 2015-
9
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
10
 */
11
class Configuration
12
{
13
14
	const SECTION_TYPE_INSTANCE = 'instance';
15
16
	const OPTION_UPDATE_INTERVAL = 'updateInterval';
17
	const OPTION_LISTEN_ADDRESS  = 'listenAddress';
18
	const OPTION_LISTEN_PORT     = 'listenPort';
19
20
	const DEFAULT_UPDATE_INTERVAL = 2;
21
	const DEFAULT_LISTEN_ADDRESS  = '0.0.0.0';
22
	const DEFAULT_LISTEN_PORT     = 9333;
23
24
	/**
25
	 * @var string the database path
26
	 */
27
	private $_databasePath;
28
29
	/**
30
	 * @var string the log file path
31
	 */
32
	private $_logPath;
33
34
	/**
35
	 * @var Instance[] the instances
36
	 */
37
	private $_instances;
38
39
	/**
40
	 * @var float the status update interval (in seconds)
41
	 */
42
	private $_updateInterval = self::DEFAULT_UPDATE_INTERVAL;
43
44
	/**
45
	 * @var string the address to listen on
46
	 */
47
	private $_listenAddress = self::DEFAULT_LISTEN_ADDRESS;
48
49
	/**
50
	 * @var int the port to listen on
51
	 */
52
	private $_listenPort = self::DEFAULT_LISTEN_PORT;
53
54
55
	/**
56
	 * @param string     $databasePath
57
	 * @param Instance[] $_instances
58
	 */
59
	public function __construct($databasePath, array $_instances)
60
	{
61
		$this->_databasePath = $databasePath;
62
		$this->_instances    = $_instances;
63
	}
64
65
66
	/**
67
	 * @return string
68
	 */
69
	public function getDatabasePath()
70
	{
71
		return $this->_databasePath;
72
	}
73
74
75
	/**
76
	 * @return string
77
	 */
78
	public function getLogPath()
79
	{
80
		return $this->_logPath;
81
	}
82
83
84
	/**
85
	 * @param string $logPath
86
	 */
87
	public function setLogPath($logPath)
88
	{
89
		$this->_logPath = $logPath;
90
	}
91
92
93
	/**
94
	 * @return Instance[]
95
	 */
96
	public function getInstances()
97
	{
98
		return $this->_instances;
99
	}
100
101
102
	/**
103
	 * @param string $name
104
	 *
105
	 * @return Instance|null
106
	 */
107
	public function getInstanceByName($name)
108
	{
109
		foreach ($this->_instances as $instance)
110
			if ($instance->getName() === $name)
111
				return $instance;
112
113
		return null;
114
	}
115
116
117
	/**
118
	 * @return float
119
	 */
120
	public function getUpdateInterval()
121
	{
122
		return $this->_updateInterval;
123
	}
124
125
126
	/**
127
	 * @param $updateInterval
128
	 *
129
	 * @throws \RuntimeException
130
	 */
131
	public function setUpdateInterval($updateInterval)
132
	{
133
		if ($updateInterval <= 0)
134
			throw new \RuntimeException('Invalid update interval specified');
135
136
		$this->_updateInterval = $updateInterval;
137
	}
138
139
140
	/**
141
	 * @return string
142
	 */
143
	public function getListenAddress()
144
	{
145
		return $this->_listenAddress;
146
	}
147
148
149
	/**
150
	 * @param string $listenAddress
151
	 */
152
	public function setListenAddress($listenAddress)
153
	{
154
		$this->_listenAddress = $listenAddress;
155
	}
156
157
158
	/**
159
	 * @return int
160
	 */
161
	public function getListenPort()
162
	{
163
		return $this->_listenPort;
164
	}
165
166
167
	/**
168
	 * @param int $listenPort
169
	 *
170
	 * @throws \RuntimeException
171
	 */
172
	public function setListenPort($listenPort)
173
	{
174
		if ($listenPort < 1 || $listenPort > 65535)
175
			throw new \RuntimeException('Invalid port specified');
176
177
		$this->_listenPort = $listenPort;
178
	}
179
180
}
181