1 | <?php |
||
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 | 'http_listen_port', |
||
33 | 'http_username', |
||
34 | 'http_password', |
||
35 | ]; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Validator constructor. |
||
40 | * |
||
41 | * @param array $configuration |
||
42 | */ |
||
43 | 12 | public function __construct(array $configuration) |
|
47 | |||
48 | |||
49 | /** |
||
50 | * @throws InvalidConfigurationException |
||
51 | */ |
||
52 | 12 | public function validate() |
|
53 | { |
||
54 | // Check that all mandatory values are defined |
||
55 | 12 | foreach (self::$mandatoryValues as $mandatoryValue) |
|
56 | 12 | if (!isset($this->_configuration[$mandatoryValue]) || ($this->_configuration[$mandatoryValue] != '0' && empty($this->_configuration[$mandatoryValue]))) |
|
57 | 12 | throw new InvalidConfigurationException('Mandatory configuration value "' . $mandatoryValue . '" is missing'); |
|
58 | |||
59 | 11 | if (!is_readable($this->_configuration['database_path'])) |
|
60 | 1 | throw new InvalidConfigurationException('The database path does not exist or is not writable'); |
|
61 | |||
62 | // Attempt to create the log path if it doesn't exist |
||
63 | 10 | if (!file_exists($this->_configuration['log_path']) && is_writable(dirname($this->_configuration['log_path']))) |
|
64 | touch($this->_configuration['log_path']); |
||
65 | |||
66 | 10 | if (!is_writable($this->_configuration['log_path'])) |
|
67 | 1 | throw new InvalidConfigurationException('The log path does not exist or is not writable'); |
|
68 | |||
69 | 9 | if (intval($this->_configuration['update_interval']) < 1) |
|
70 | 3 | throw new InvalidConfigurationException('Update interval cannot be lower than 1 second'); |
|
71 | |||
72 | 6 | $listenPort = intval($this->_configuration['listen_port']); |
|
73 | 6 | $httpListenPort = intval($this->_configuration['http_listen_port']); |
|
74 | |||
75 | 6 | $this->validatePort($listenPort); |
|
76 | 2 | $this->validatePort($httpListenPort); |
|
77 | |||
78 | // Check that the listen ports aren't equal |
||
79 | 2 | if ($listenPort === $httpListenPort) |
|
80 | 1 | throw new InvalidConfigurationException('listen_port and http_listen_port cannot be equal'); |
|
81 | 1 | } |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @param int $port |
||
86 | * |
||
87 | * @throws InvalidConfigurationException |
||
88 | */ |
||
89 | 6 | private function validatePort($port) |
|
94 | |||
95 | } |
||
96 |