Completed
Push — master ( 600e8c...3dfec0 )
by Sam
02:51
created

Configuration::parseInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 2
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
	 * @return float
104
	 */
105
	public function getUpdateInterval()
106
	{
107
		return $this->_updateInterval;
108
	}
109
110
111
	/**
112
	 * @param $updateInterval
113
	 *
114
	 * @throws \RuntimeException
115
	 */
116
	public function setUpdateInterval($updateInterval)
117
	{
118
		if ($updateInterval <= 0)
119
			throw new \RuntimeException('Invalid update interval specified');
120
121
		$this->_updateInterval = $updateInterval;
122
	}
123
124
125
	/**
126
	 * @return string
127
	 */
128
	public function getListenAddress()
129
	{
130
		return $this->_listenAddress;
131
	}
132
133
134
	/**
135
	 * @param string $listenAddress
136
	 */
137
	public function setListenAddress($listenAddress)
138
	{
139
		$this->_listenAddress = $listenAddress;
140
	}
141
142
143
	/**
144
	 * @return int
145
	 */
146
	public function getListenPort()
147
	{
148
		return $this->_listenPort;
149
	}
150
151
152
	/**
153
	 * @param int $listenPort
154
	 *
155
	 * @throws \RuntimeException
156
	 */
157
	public function setListenPort($listenPort)
158
	{
159
		if ($listenPort < 1 || $listenPort > 65535)
160
			throw new \RuntimeException('Invalid port specified');
161
162
		$this->_listenPort = $listenPort;
163
	}
164
165
}
166