Completed
Push — master ( b2dfe1...1412d2 )
by Sam
02:32
created

Configuration::getInstances()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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