Options   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 10 2
A getOptions() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
7
/**
8
 * Class to manage Options
9
 */
10
class Options
11
{
12
    /**
13
     * @const ERR_KEY_NOT_EXIST Exception code if a key not exist.
14
     */
15
    const ERR_KEY_NOT_EXIST = 1106001;
16
    
17
    /**
18
     * @var array $option option's list
19
     */
20
    protected $options = [];
21
22
    /**
23
     * Constructor
24
     * Merge default option with passed values
25
     * 
26
     * @param array $defaultOptions Default options
27
     * @param array $options Options from applications/users
28
     */
29
    public function __construct(array $defaultOptions, array $options)
30
    {
31
        $this->options = array_merge($defaultOptions, $options);
32
    }
33
    
34
    /**
35
     * Getter accessor to options property
36
     * 
37
     * @return array
38
     */
39
    public function getOptions(): array
40
    {
41
        return $this->options;
42
    }
43
44
    /**
45
     * Get the value for an option
46
     * 
47
     * @param string $optionKey The option key
48
     * 
49
     * @return mixed
50
     * 
51
     * @throws \Exception If the key not exists
52
     */
53
    public function getValue(string $optionKey)
54
    {
55
        if (!isset($this->options[$optionKey])) {
56
            throw new Exception(
57
                'Option key '.$optionKey.' not exist.',
58
                $this::ERR_KEY_NOT_EXIST
59
            );
60
        }
61
62
        return $this->options[$optionKey];
63
    }
64
}
65