1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Configuration; |
4
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Exception\InvalidConfigurationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Validator |
9
|
|
|
* @package Jalle19\StatusManager\Configuration |
10
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
11
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
12
|
|
|
*/ |
13
|
|
|
class Validator |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $_configuration; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $mandatoryValues = [ |
25
|
|
|
'database_path', |
26
|
|
|
'log_path', |
27
|
|
|
'access_token', |
28
|
|
|
'instances', |
29
|
|
|
'update_interval', |
30
|
|
|
'listen_address', |
31
|
|
|
'listen_port', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validator constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $configuration |
39
|
|
|
*/ |
40
|
11 |
|
public function __construct(array $configuration) |
41
|
|
|
{ |
42
|
11 |
|
$this->_configuration = $configuration; |
43
|
11 |
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws InvalidConfigurationException |
48
|
|
|
*/ |
49
|
11 |
|
public function validate() |
50
|
|
|
{ |
51
|
|
|
// Check that all mandatory values are defined |
52
|
11 |
|
foreach (self::$mandatoryValues as $mandatoryValue) |
53
|
11 |
|
if (!isset($this->_configuration[$mandatoryValue]) || ($this->_configuration[$mandatoryValue] != '0' && empty($this->_configuration[$mandatoryValue]))) |
54
|
11 |
|
throw new InvalidConfigurationException('Mandatory configuration value "' . $mandatoryValue . '" is missing'); |
55
|
|
|
|
56
|
10 |
|
if (!is_readable($this->_configuration['database_path'])) |
57
|
10 |
|
throw new InvalidConfigurationException('The database path does not exist or is not writable'); |
58
|
|
|
|
59
|
|
|
// Attempt to create the log path if it doesn't exist, fail if it is not writable |
60
|
9 |
|
if (!file_exists($this->_configuration['log_path'])) |
61
|
9 |
|
touch($this->_configuration['log_path']); |
62
|
|
|
|
63
|
8 |
|
if (!is_writable($this->_configuration['log_path'])) |
64
|
8 |
|
throw new InvalidConfigurationException('The log path does not exist or is not writable'); |
65
|
|
|
|
66
|
8 |
|
if (intval($this->_configuration['update_interval']) < 1) |
67
|
8 |
|
throw new InvalidConfigurationException('Update interval cannot be lower than 1 second'); |
68
|
|
|
|
69
|
5 |
|
$listenPort = intval($this->_configuration['listen_port']); |
70
|
5 |
|
if ($listenPort < 1 || $listenPort > 65535) |
71
|
5 |
|
throw new InvalidConfigurationException('Listen port must be between 1 and 65535'); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|