1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Configuration; |
4
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Configuration\Reader\ReaderInterface; |
6
|
|
|
use Jalle19\StatusManager\Exception\InvalidConfigurationException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Parser |
10
|
|
|
* @package Jalle19\StatusManager\Configuration |
11
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
12
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
13
|
|
|
*/ |
14
|
|
|
class Parser |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parses the application configuration |
19
|
|
|
* |
20
|
|
|
* @param ReaderInterface $reader |
21
|
|
|
* |
22
|
|
|
* @return Configuration the parsed configuration |
23
|
|
|
* @throws InvalidConfigurationException if the configuration couldn't be parsed and validated |
24
|
|
|
*/ |
25
|
1 |
|
public static function parseConfiguration(ReaderInterface $reader) |
26
|
|
|
{ |
27
|
1 |
|
$configuration = $reader->readConfiguration(); |
28
|
|
|
|
29
|
|
|
// Validate the configuration |
30
|
1 |
|
$validator = new Validator($configuration); |
31
|
1 |
|
$validator->validate(); |
32
|
|
|
|
33
|
|
|
// Create the configuration object |
34
|
1 |
|
$config = new Configuration(); |
35
|
1 |
|
$config->setDatabasePath($configuration['database_path']) |
36
|
1 |
|
->setLogPath($configuration['log_path']) |
37
|
1 |
|
->setInstances(self::parseInstances($configuration)) |
38
|
1 |
|
->setAccessToken($configuration['access_token']) |
39
|
1 |
|
->setUpdateInterval($configuration['update_interval']) |
40
|
1 |
|
->setListenAddress($configuration['listen_address']) |
41
|
1 |
|
->setListenPort($configuration['listen_port']); |
42
|
|
|
|
43
|
1 |
|
return $config; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $configuration |
49
|
|
|
* |
50
|
|
|
* @return Instance[] the instances |
51
|
|
|
*/ |
52
|
1 |
|
private static function parseInstances($configuration) |
53
|
|
|
{ |
54
|
1 |
|
$instances = []; |
55
|
|
|
|
56
|
1 |
|
foreach ($configuration['instances'] as $name => $options) |
57
|
1 |
|
$instances[] = self::parseInstance($name, $options); |
58
|
|
|
|
59
|
1 |
|
return $instances; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name the name of the instance |
65
|
|
|
* @param array $options |
66
|
|
|
* |
67
|
|
|
* @return Instance |
68
|
|
|
*/ |
69
|
1 |
|
private static function parseInstance($name, $options) |
70
|
|
|
{ |
71
|
1 |
|
$address = $options['address']; |
72
|
1 |
|
$port = intval($options['port']); |
73
|
|
|
|
74
|
1 |
|
$instance = new Instance($name, $address, $port); |
75
|
|
|
|
76
|
|
|
// Optionally set ignored users |
77
|
1 |
|
if (isset($options['ignoredUsers'])) |
78
|
1 |
|
$instance->setIgnoredUsers($options['ignoredUsers']); |
79
|
|
|
|
80
|
|
|
// Optionally set credentials |
81
|
1 |
|
if (isset($options['username']) && isset($options['password'])) |
82
|
1 |
|
$instance->setCredentials($options['username'], $options['password']); |
83
|
|
|
|
84
|
1 |
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|