Passed
Push — master ( 5204b5...bf9fba )
by Alice
37s
created

ConfigurationValidator::validateRoot()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 4
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
		$this->validateClass($service);
69 5
		$this->validateArguments($service);
70 4
		$this->validateMethods($service);
71 2
	}
72
73
	/**
74
	 * @param $service
75
	 * @throws InvalidConfigFormatException
76
	 */
77 7
	private function validateClass($service)
78
	{
79 7
		if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) {
80 1
			throw new InvalidConfigFormatException(
81 1
				self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required'
82
			);
83
		}
84
85 6
		if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) {
86 1
			throw new InvalidConfigFormatException(
87 1
				self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string'
88
			);
89
		}
90 5
	}
91
92
	/**
93
	 * @param $service
94
	 * @throws InvalidConfigFormatException
95
	 */
96 5
	private function validateArguments($service)
97
	{
98 5
		if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) &&
99 5
			false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME])
100
		) {
101 1
			throw new InvalidConfigFormatException(
102 1
				self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME .
103 1
				'" must be an array or null'
104
			);
105
		}
106 4
	}
107
108
	/**
109
	 * @param $service
110
	 * @throws InvalidConfigFormatException
111
	 */
112 4
	private function validateMethods($service)
113
	{
114 4
		if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) &&
115 4
			false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME])
116
		) {
117 1
			throw new InvalidConfigFormatException(
118 1
				self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME .
119 1
				'" must be an array or null'
120
			);
121
		}
122
123 3
		if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) {
124 2
			return;
125
		}
126
127 3
		foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) {
128 3
			$this->validateCalls($methodName, $call);
129
		}
130 2
	}
131
132
	/**
133
	 * @param $methodName
134
	 * @param $call
135
	 * @throws InvalidConfigFormatException
136
	 */
137 3
	private function validateCalls($methodName, $call)
138
	{
139 3
		if (false === is_array($call)) {
140 1
			throw new InvalidConfigFormatException(
141 1
				self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array'
142
			);
143
		}
144 2
	}
145
146
}
147