Validator::validate()   B
last analyzed

Complexity

Conditions 11
Paths 19

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 11

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 11
eloc 17
c 2
b 0
f 1
nc 19
nop 0
dl 0
loc 29
rs 7.3166
ccs 18
cts 18
cp 1
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 &copy; 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
		'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)
44
	{
45 12
		$this->_configuration = $configuration;
46 12
	}
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 11
			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 10
			touch($this->_configuration['log_path']);
65
66 10
		if (!is_writable($this->_configuration['log_path']))
67 10
			throw new InvalidConfigurationException('The log path does not exist or is not writable');
68
69 9
		if (intval($this->_configuration['update_interval']) < 1)
70 9
			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 2
			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)
90
	{
91 6
		if ($port < 1 || $port > 65535)
92 6
			throw new InvalidConfigurationException('Listen port must be between 1 and 65535');
93 2
	}
94
95
}
96