Completed
Pull Request — master (#74)
by Marcel
19:29
created

Configuration::extractMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace PHPSemVerChecker\Configuration;
4
5
use Noodlehaus\Config;
6
use PHPSemVerChecker\SemanticVersioning\Level;
7
8
class Configuration
9
{
10
	/**
11
	 * @var int[]
12
	 */
13
	protected $mapping = [];
14
	/**
15
	 * @var \Noodlehaus\Config
16
	 */
17
	protected $config;
18
19
	/**
20
	 * @param string|array $file
21
	 * @return \PHPSemVerChecker\Configuration\Configuration
22
	 */
23
	public static function fromFile($file)
24
	{
25
		return new Configuration($file);
26
	}
27
28
	/**
29
	 * @return \PHPSemVerChecker\Configuration\Configuration
30
	 */
31
	public static function defaults()
32
	{
33
		return self::fromFile(['?php-semver-checker.yml.dist', '?php-semver-checker.yml']);
34
	}
35
36
	/**
37
	 * @param string|array $path
38
	 */
39
	public function __construct($path)
40
	{
41
		$this->config = new Config($path);
42
		$this->extractMapping($this->get('level.mapping', []));
43
	}
44
45
	/**
46
	 * @return array
47
	 */
48
	public function getLevelMapping()
49
	{
50
		return $this->mapping;
51
	}
52
53
	/**
54
	 * @see \Noodlehaus\Config::get
55
	 * @param string $key
56
	 * @param mixed|null $default
57
	 * @return array|mixed|null
58
	 */
59
	public function get($key, $default = null)
60
	{
61
		return $this->config->get($key, $default);
62
	}
63
64
	/**
65
	 * @see \Noodlehaus\Config::set
66
	 * @param string $key
67
	 * @param mixed $value
68
	 */
69
	public function set($key, $value)
70
	{
71
		$this->config->set($key, $value);
72
	}
73
74
	/**
75
	 * Merge this configuration with an associative array.
76
	 *
77
	 * Note that dot notation is used for keys.
78
	 *
79
	 * @param array $data
80
	 */
81
	public function merge($data)
82
	{
83
		foreach ($data as $key => $value) {
84
			$this->set($key, $value);
85
		}
86
	}
87
88
	/**
89
	 * @param array $mapping
90
	 */
91
	protected function extractMapping(array $mapping)
92
	{
93
		foreach ($mapping as &$level) {
94
			$level = Level::fromString($level);
95
		}
96
		$this->mapping = $mapping;
97
	}
98
}
99