Completed
Pull Request — master (#10)
by Alice
02:26
created

ConfigurationValidator::validateService()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 8
nop 2
dl 0
loc 44
rs 7.6666
c 0
b 0
f 0
ccs 24
cts 24
cp 1
crap 10

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 Wonderland\Container\Configuration\Validator;
4
5
use Wonderland\Container\Configuration\Config\Fields;
6
use Wonderland\Container\Exception\InvalidConfigFormatException;
7
8
/**
9
 * TODO find a better way to validate later
10
 * Class ServiceValidator
11
 * @package Wonderland\Container\Configuration\Validator
12
 * @author Alice Praud <[email protected]>
13
 */
14
class ConfigurationValidator
15
{
16
	private const ERROR_PREFIX = 'The service configuration';
17
18
	/**
19
	 * @param array $definitionList
20
	 * @throws InvalidConfigFormatException
21
	 */
22 12
	public function validateDefinitions(array $definitionList)
23
	{
24 12
		$this->validateRoot($definitionList);
25
26 10
		if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) {
27 2
			return;
28
		}
29
30 9
		foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $service) {
31 9
			$this->validateService($serviceName, $service);
32
		}
33 2
	}
34
35
	/**
36
	 * @param array $definitionList
37
	 * @throws InvalidConfigFormatException
38
	 */
39 12
	private function validateRoot(array $definitionList)
40
	{
41 12
		if (false === array_key_exists(Fields::ROOT_CONFIG_NAME, $definitionList)) {
42 1
			throw new InvalidConfigFormatException(
43 1
				self::ERROR_PREFIX . ' need the root config key "' . Fields::ROOT_CONFIG_NAME . '"'
44
			);
45
		}
46
47 11
		$field = $definitionList[Fields::ROOT_CONFIG_NAME];
48 11
		if (false === is_array($field) && false === is_null($field)) {
49 1
			throw new InvalidConfigFormatException(
50 1
				self::ERROR_PREFIX . ' key "' . Fields::ROOT_CONFIG_NAME . '" must be an associative array'
51
			);
52
		}
53 10
	}
54
55
	/**
56
	 * @param $serviceName
57
	 * @param $service
58
	 * @throws InvalidConfigFormatException
59
	 */
60 9
	private function validateService($serviceName, $service)
61
	{
62 9
		if (false === is_array($service)) {
63 2
			throw new InvalidConfigFormatException(
64 2
				self::ERROR_PREFIX . ' key "' . $serviceName . '" must be an associative array'
65
			);
66
		}
67
68 7
		if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) {
69 1
			throw new InvalidConfigFormatException(
70 1
				self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required'
71
			);
72
		}
73
74 6
		if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) {
75 1
			throw new InvalidConfigFormatException(
76 1
				self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string'
77
			);
78
		}
79
80 5
		if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) &&
81 5
			false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME])
82
		) {
83 1
			throw new InvalidConfigFormatException(
84 1
				self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME .
85 1
				'" must be an array or null'
86
			);
87
		}
88
89 4
		if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) &&
90 4
			false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME])
91
		) {
92 1
			throw new InvalidConfigFormatException(
93 1
				self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME .
94 1
				'" must be an array or null'
95
			);
96
		}
97
98 3
		if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) {
99 2
			return;
100
		}
101
102 3
		foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) {
103 3
			$this->validateCalls($methodName, $call);
104
		}
105 2
	}
106
107
	/**
108
	 * @param $methodName
109
	 * @param $call
110
	 * @throws InvalidConfigFormatException
111
	 */
112 3
	private function validateCalls($methodName, $call)
113
	{
114 3
		if (false === is_array($call)) {
115 1
			throw new InvalidConfigFormatException(
116 1
				self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array'
117
			);
118
		}
119 2
	}
120
121
}
122