Config   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameter() 0 3 1
A __construct() 0 9 3
A setParameter() 0 3 1
A get() 0 3 1
1
<?php
2
3
namespace PhpEarth\Stats;
4
5
use Symfony\Component\Yaml\Exception\ParseException;
6
use Symfony\Component\Yaml\Parser;
7
8
/**
9
 * Configuration which reads values from YAML files.
10
 */
11
class Config
12
{
13
    /**
14
     * @var array
15
     */
16
    private $values = [];
17
18
    /**
19
     * Constructor. Sets up config parameters from YAML file(s). Later files
20
     * overwrite config values.
21
     *
22
     * @param array $files YAML configuration file(s)
23
     * @throws \Exception
24
     */
25
    public function __construct(array $files)
26
    {
27
        foreach ($files as $file) {
28
            $parser = new Parser();
29
            try {
30
                $values = $parser->parse(file_get_contents($file), false, false, false);
31
                $this->values = array_merge($this->values, $values);
32
            } catch (ParseException $e) {
33
                throw new \Exception('Unable to parse the YAML string: '.$e->getMessage());
34
            }
35
        }
36
    }
37
38
    /**
39
     * Returns configuration value by key.
40
     *
41
     * @param string $key
42
     *
43
     * @return mixed
44
     */
45
    public function get($key)
46
    {
47
        return $this->values[$key];
48
    }
49
50
    /**
51
     * Returns configuration value by key from parameters configuration.
52
     *
53
     * @param string $key
54
     *
55
     * @return mixed
56
     */
57
    public function getParameter($key)
58
    {
59
        return $this->values['parameters'][$key];
60
    }
61
62
    /**
63
     * Manually set the configuration parameter value by key.
64
     *
65
     * @param string $key
66
     * @param mixed $value
67
     */
68
    public function setParameter($key, $value)
69
    {
70
        $this->values['parameters'][$key] = $value;
71
    }
72
}
73