Completed
Push — master ( 3a5b78...5c6f77 )
by Mr
13s queued 11s
created

Config   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 53.85%

Importance

Changes 5
Bugs 3 Features 2
Metric Value
eloc 19
c 5
b 3
f 2
dl 0
loc 88
ccs 14
cts 26
cp 0.5385
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
A __construct() 0 4 2
A getAllowed() 0 3 1
A get() 0 5 2
A getParameters() 0 12 4
1
<?php
2
3
namespace UON;
4
5
use UON\Exceptions\ConfigException;
6
use UON\Interfaces\ConfigInterface;
7
8
class Config implements ConfigInterface
9
{
10
    /**
11
     * List of configured parameters
12
     *
13
     * @var array
14
     */
15
    private $_parameters = [
16
        // Errors must be disabled by default, because we need to get error codes
17
        // @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
18
        'http_errors' => false
19
    ];
20
21
    /**
22
     * Config constructor.
23
     *
24
     * @param   array $parameters List of parameters which can be set on object creation stage
25
     * @throws  \UON\Exceptions\ConfigException
26
     * @since   1.9
27
     */
28
    public function __construct(array $parameters = [])
29
    {
30
        foreach ($parameters as $key => $value) {
31
            $this->set($key, $value);
32
        }
33
    }
34
35
    /**
36
     * Set parameter by name
37
     *
38
     * @param   string          $name
39
     * @param   string|bool|int $value
40
     * @return  \UON\Interfaces\ConfigInterface
41
     * @throws  \UON\Exceptions\ConfigException
42
     */
43
    public function set($name, $value)
44
    {
45
        if (!\in_array($name, $this->getAllowed(), false)) {
46
            throw new ConfigException("Parameter \"$name\" is not in available list [" . implode(',', $this->getAllowed()) . ']');
47
        }
48
49
        // Add parameters into array
50
        $this->_parameters[$name] = $value;
51
        return $this;
52
    }
53
54
    /**
55
     * Get all available parameters on only one
56
     *
57
     * @param   string $name
58
     * @return  string|bool|int
59
     */
60 2
    public function get($name)
61
    {
62 2
        return isset($this->_parameters[$name])
63 2
            ? $this->_parameters[$name]
64 2
            : false;
65
    }
66
67
    /**
68
     * Return all allowed parameters
69
     *
70
     * @return  array
71
     */
72
    public function getAllowed()
73
    {
74
        return self::ALLOWED;
75
    }
76
77
    /**
78
     * Return all preconfigured parameters
79
     *
80
     * @param   bool  $ignore       Ignore parameters which is not important for client
81
     * @param   array $ignore_items Which items should be excluded from array
82
     * @return  array
83
     */
84 2
    public function getParameters($ignore = false, array $ignore_items = ['token', 'tries', 'seconds', 'maxRequestTimeout'])
85
    {
86 2
        $parameters = $this->_parameters;
87
        // Remove ignored items from array
88 2
        if ($ignore) {
89 2
            foreach ($parameters as $name => $value) {
90 2
                if (in_array($name, $ignore_items, false)) {
91 2
                    unset($parameters[$name]);
92 2
                }
93 2
            }
94 2
        }
95 2
        return $parameters;
96
    }
97
}
98