Completed
Push — master ( 035d35...799922 )
by Sam
03:35
created

Parser::parseInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
rs 9.2
c 2
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
crap 4.1755
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 &copy; 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 1
		       ->setHttpListenPort($configuration['http_listen_port'])
43 1
		       ->setHttpUsername($configuration['http_username'])
44 1
		       ->setHttpPassword($configuration['http_password']);
45
46 1
		return $config;
47
	}
48
49
50
	/**
51
	 * @param array $configuration
52
	 *
53
	 * @return Instance[] the instances
54
	 */
55 1
	private static function parseInstances($configuration)
56
	{
57 1
		$instances = [];
58
59 1
		foreach ($configuration['instances'] as $name => $options)
60 1
			$instances[] = self::parseInstance($name, $options);
61
62 1
		return $instances;
63
	}
64
65
66
	/**
67
	 * @param string $name the name of the instance
68
	 * @param array  $options
69
	 *
70
	 * @return Instance
71
	 */
72 1
	private static function parseInstance($name, $options)
73
	{
74 1
		$address = $options['address'];
75 1
		$port    = intval($options['port']);
76
77 1
		$instance = new Instance($name, $address, $port);
78
79
		// Optionally set ignored users
80 1
		if (isset($options['ignoredUsers']))
81
			$instance->setIgnoredUsers($options['ignoredUsers']);
82
83
		// Optionally set credentials
84 1
		if (isset($options['username']) && isset($options['password']))
85
			$instance->setCredentials($options['username'], $options['password']);
86
87 1
		return $instance;
88
	}
89
90
}
91