Completed
Push — master ( 4d48d9...570cf9 )
by Tomáš
04:48
created

ConfigArrayBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 53
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mergeOverByConfigs() 0 8 2
A mergeOverByConfig() 0 6 1
A fromConfigs() 0 4 1
A mergeDefaultConfig() 0 6 1
A build() 0 9 2
A mergeConfigs() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\SymfonyProoph\Common;
4
5
class ConfigArrayBuilder
6
{
7
	private $configs = [];
8
9 16
	private function __construct(array $configs)
10
	{
11 16
		$this->mergeOverByConfigs($configs);
12 16
	}
13
14 16
	public function mergeOverByConfigs(array $configs): self
15
	{
16 16
		foreach ($configs as $config) {
17 16
			$this->mergeOverByConfig($config);
18
		}
19
20 16
		return $this;
21
	}
22
23 16
	public function mergeOverByConfig(array $config): self
24
	{
25 16
		$this->configs[] = $config;
26
27 16
		return $this;
28
	}
29
30 16
	public static function fromConfigs(array $configs = []): self
31
	{
32 16
		return new self($configs);
33
	}
34
35 16
	public function mergeDefaultConfig(array $config): self
36
	{
37 16
		array_unshift($this->configs, $config);
38
39 16
		return $this;
40
	}
41
42 16
	public function build(): array
43
	{
44 16
		$builtConfig = [];
45 16
		foreach ($this->configs as $overridingConfig) {
46 16
			$builtConfig = $this->mergeConfigs($overridingConfig, $builtConfig);
47
		}
48
49 16
		return $builtConfig;
50
	}
51
52 16
	private function mergeConfigs(array $overridingConfig, array $overriddenConfig): array
53
	{
54 16
		return array_replace_recursive($overriddenConfig, $overridingConfig);
55
	}
56
57
}
58