Completed
Push — master ( bb7259...dc298a )
by Sam
03:00
created

Validator::validate()   C

Complexity

Conditions 11
Paths 19

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 5.3305
cc 11
eloc 15
nc 19
nop 0
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
	];
33
34
35
	/**
36
	 * Validator constructor.
37
	 *
38
	 * @param array $configuration
39
	 */
40 11
	public function __construct(array $configuration)
41
	{
42 11
		$this->_configuration = $configuration;
43 11
	}
44
45
46
	/**
47
	 * @throws InvalidConfigurationException
48
	 */
49 11
	public function validate()
50
	{
51
		// Check that all mandatory values are defined
52 11
		foreach (self::$mandatoryValues as $mandatoryValue)
53 11
			if (!isset($this->_configuration[$mandatoryValue]) || ($this->_configuration[$mandatoryValue] != '0' && empty($this->_configuration[$mandatoryValue])))
54 11
				throw new InvalidConfigurationException('Mandatory configuration value "' . $mandatoryValue . '" is missing');
55
56 10
		if (!is_readable($this->_configuration['database_path']))
57 10
			throw new InvalidConfigurationException('The database path does not exist or is not writable');
58
59
		// Attempt to create the log path if it doesn't exist, fail if it is not writable
60 9
		if (!file_exists($this->_configuration['log_path']))
61 9
			touch($this->_configuration['log_path']);
62
63 8
		if (!is_writable($this->_configuration['log_path']))
64 8
			throw new InvalidConfigurationException('The log path does not exist or is not writable');
65
66 8
		if (intval($this->_configuration['update_interval']) < 1)
67 8
			throw new InvalidConfigurationException('Update interval cannot be lower than 1 second');
68
69 5
		$listenPort = intval($this->_configuration['listen_port']);
70 5
		if ($listenPort < 1 || $listenPort > 65535)
71 5
			throw new InvalidConfigurationException('Listen port must be between 1 and 65535');
72 1
	}
73
74
}
75