Completed
Pull Request — master (#2)
by René
06:17 queued 04:15
created

Configuration::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Configuration;
5
6
use Zortje\MVC\Configuration\Exception\ConfigurationNonexistentException;
7
8
/**
9
 * Class Configuration
10
 *
11
 * @package Zortje\MVC\Configuration
12
 */
13
class Configuration
14
{
15
16
    /**
17
     * @var array Internal configurations
18
     */
19
    protected $configurations = [];
20
21
    /**
22
     * Configuration constructor.
23
     *
24
     * @param array $config
25
     */
26
    public function __construct(array $config)
27
    {
28
        foreach ($config as $key => $value) {
29
            $this->set($key, $value);
30
        }
31
    }
32
33
    /**
34
     * Set configuration
35
     *
36
     * @param string $key   Configuration name
37
     * @param mixed  $value Configuration value
38
     */
39
    public function set(string $key, $value)
40
    {
41
        $this->configurations[$key] = $value;
42
    }
43
44
    /**
45
     * Check if configuration exists
46
     *
47
     * @param string $key Configuration name
48
     *
49
     * @return bool TRUE if configuration exists, otherwise FALSE
50
     */
51
    public function exists(string $key)
52
    {
53
        return isset($this->configurations[$key]);
54
    }
55
56
    /**
57
     * Get configuration
58
     *
59
     * @param string $key Configuration name
60
     *
61
     * @return mixed Configuration value
62
     *
63
     * @throws ConfigurationNonexistentException If configuration has not been set
64
     */
65
    public function get(string $key)
66
    {
67
        if (isset($this->configurations[$key]) === false) {
68
            throw new ConfigurationNonexistentException([$key]);
69
        }
70
71
        return $this->configurations[$key];
72
    }
73
}
74