Completed
Push — master ( eeb5e0...9f7a88 )
by Sam
04:08
created

Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 77
ccs 30
cts 30
cp 1
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseConfiguration() 0 23 1
A parseInstances() 0 9 2
A parseInstance() 0 17 4
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 1
			$instance->setIgnoredUsers($options['ignoredUsers']);
82
83
		// Optionally set credentials
84 1
		if (isset($options['username']) && isset($options['password']))
85 1
			$instance->setCredentials($options['username'], $options['password']);
86
87 1
		return $instance;
88
	}
89
90
}
91