Passed
Push — master ( fc2f89...038734 )
by compolom
02:33
created

Config   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 140
ccs 24
cts 25
cp 0.96
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A guzzle() 0 10 1
A get() 0 3 1
A all() 0 3 1
A set() 0 9 2
A __construct() 0 25 2
A validate() 0 8 3
1
<?php
2
3
namespace Resova;
4
5
class Config
6
{
7
    /**
8
     * List of allowed parameters
9
     */
10
    public const ALLOWED = [
11
        'user_agent',
12
        'base_uri',
13
        'api_key',
14
        'timeout',
15
        'tries',
16
        'seconds',
17
        'debug',
18
        'track_redirects'
19
    ];
20
21
    /**
22
     * List of minimal required parameters
23
     */
24
    public const REQUIRED = [
25
        'user_agent',
26
        'base_uri',
27
        'timeout',
28
        'api_key',
29
    ];
30
31
    /**
32
     * List of configured parameters
33
     *
34
     * @var array
35
     */
36
    private $_parameters;
37
38
    /**
39
     * Config constructor.
40
     *
41
     * @param array $parameters List of parameters which can be set on object creation stage
42
     * @throws \ErrorException
43
     */
44 14
    public function __construct(array $parameters = [])
45
    {
46
        // Set default parameters of client
47 14
        $this->_parameters = [
48
            // Errors must be disabled by default, because we need to get error codes
49
            // @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
50
            'http_errors'     => false,
51
52
            // Wrapper settings
53
            'tries'           => 2,  // Count of tries
54
            'seconds'         => 10, // Waiting time per each try
55
56
            // Optional parameters
57
            'debug'           => false,
58
            'track_redirects' => false,
59
60
            // Main parameters
61
            'timeout'         => 20,
62
            'user_agent'      => 'Resova PHP Client',
63
            'base_uri'        => 'https://api.resova.eu/v1'
64
        ];
65
66
        // Overwrite parameters by client input
67 14
        foreach ($parameters as $key => $value) {
68 10
            $this->set($key, $value);
69
        }
70 14
    }
71
72
    /**
73
     * Set parameter by name
74
     *
75
     * @param string               $name  Name of parameter
76
     * @param string|bool|int|null $value Value of parameter
77
     * @return \Resova\Config
78
     * @throws \ErrorException
79
     */
80 10
    private function set($name, $value): self
81
    {
82 10
        if (!\in_array($name, self::ALLOWED, false)) {
83 1
            throw new \ErrorException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']');
84
        }
85
86
        // Add parameters into array
87 10
        $this->_parameters[$name] = $value;
88 10
        return $this;
89
    }
90
91
    /**
92
     * Get available parameter by name
93
     *
94
     * @param string $name
95
     * @return string|bool|int|null
96
     */
97 11
    public function get(string $name)
98
    {
99 11
        return $this->_parameters[$name] ?? null;
100
    }
101
102
    /**
103
     * Get all available parameters
104
     *
105
     * @return array
106
     */
107 1
    public function all(): array
108
    {
109 1
        return $this->_parameters;
110
    }
111
112
    /**
113
     * Return all ready for Guzzle parameters
114
     *
115
     * @return array
116
     */
117 10
    public function guzzle(): array
118
    {
119
        return [
120
            // 'base_uri'        => $this->get('base_uri'), // By some reasons base_uri option is not work anymore
121 10
            'timeout'         => $this->get('timeout'),
122 10
            'track_redirects' => $this->get('track_redirects'),
123 10
            'debug'           => $this->get('debug'),
124
            'headers'         => [
125 10
                'User-Agent' => $this->get('user_agent'),
126 10
                'X-API-KEY'  => $this->get('api_key'),
127
            ]
128
        ];
129
    }
130
131
132
    /**
133
     * Validate preconfigured parameters
134
     *
135
     * @return bool
136
     */
137 1
    public function validate(): bool
138
    {
139 1
        foreach (self::REQUIRED as $item) {
140 1
            if (false === array_key_exists($item, $this->_parameters)) {
141
                return false;
142
            }
143
        }
144 1
        return true;
145
    }
146
}
147