Completed
Push — master ( 3e720f...f6b7fd )
by Mr
02:14
created

Config::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 25
ccs 4
cts 4
cp 1
crap 2
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace Resova;
4
5
use ErrorException;
6
7
class Config
8
{
9
    /**
10
     * List of allowed parameters
11
     */
12
    public const ALLOWED = [
13
        'api_key',
14
        'proxy',
15
        'base_uri',
16
        'user_agent',
17
        'timeout',
18
        'tries',
19
        'seconds',
20
        'debug',
21
        'track_redirects'
22
    ];
23
24
    /**
25
     * List of minimal required parameters
26
     */
27
    public const REQUIRED = [
28
        'user_agent',
29
        'base_uri',
30
        'timeout',
31
        'api_key',
32
    ];
33
34
    /**
35
     * List of configured parameters
36
     *
37
     * @var array
38
     */
39
    private $_parameters;
40
41
    /**
42
     * Config constructor.
43
     *
44
     * @param array $parameters List of parameters which can be set on object creation stage
45
     * @throws \ErrorException
46
     */
47 15
    public function __construct(array $parameters = [])
48
    {
49
        // Set default parameters of client
50 15
        $this->_parameters = [
51
            // Errors must be disabled by default, because we need to get error codes
52
            // @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
53
            'http_errors'     => false,
54
55
            // Wrapper settings
56
            'tries'           => 2,  // Count of tries
57
            'seconds'         => 10, // Waiting time per each try
58
59
            // Optional parameters
60
            'debug'           => false,
61
            'track_redirects' => false,
62
63
            // Main parameters
64
            'timeout'         => 20,
65
            'user_agent'      => 'Resova PHP Client',
66
            'base_uri'        => 'https://api.resova.eu/v1'
67
        ];
68
69
        // Overwrite parameters by client input
70 15
        foreach ($parameters as $key => $value) {
71 11
            $this->set($key, $value);
72
        }
73 14
    }
74
75
    /**
76
     * Magic setter parameter by name
77
     *
78
     * @param string               $name  Name of parameter
79
     * @param string|bool|int|null $value Value of parameter
80
     *
81
     * @throws \ErrorException
82
     */
83 1
    public function __set(string $name, $value)
84
    {
85 1
        $this->set($name, $value);
86 1
    }
87
88
    /**
89
     * Check if parameter if available
90
     *
91
     * @param string $name Name of parameter
92
     *
93
     * @return bool
94
     */
95 11
    public function __isset($name): bool
96
    {
97 11
        return isset($this->_parameters[$name]);
98
    }
99
100
    /**
101
     * Get parameter via magic call
102
     *
103
     * @param string $name Name of parameter
104
     *
105
     * @return bool|int|string|null
106
     * @throws ErrorException
107
     */
108
    public function __get($name)
109
    {
110
        return $this->get($name);
111
    }
112
113
    /**
114
     * Remove parameter from array
115
     *
116
     * @param string $name Name of parameter
117
     */
118
    public function __unset($name)
119
    {
120
        unset($this->_parameters[$name]);
121
    }
122
123
    /**
124
     * Set parameter by name
125
     *
126
     * @param string               $name  Name of parameter
127
     * @param string|bool|int|null $value Value of parameter
128
     *
129
     * @return $this
130
     * @throws ErrorException
131
     */
132 12
    public function set(string $name, $value): self
133
    {
134 12
        if (!\in_array($name, self::ALLOWED, false)) {
135 1
            throw new \ErrorException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']');
136
        }
137
138
        // Add parameters into array
139 11
        $this->_parameters[$name] = $value;
140 11
        return $this;
141
    }
142
143
    /**
144
     * Get available parameter by name
145
     *
146
     * @param string $name Name of parameter
147
     *
148
     * @return bool|int|string|null
149
     * @throws ErrorException
150
     */
151 12
    public function get(string $name)
152
    {
153 12
        if (!isset($this->_parameters[$name])) {
154 1
            throw new \ErrorException("Parameter \"$name\" is not in set");
155
        }
156
157 12
        return $this->_parameters[$name];
158
    }
159
160
    /**
161
     * Get all available parameters
162
     *
163
     * @return array
164
     */
165 1
    public function all(): array
166
    {
167 1
        return $this->_parameters;
168
    }
169
170
    /**
171
     * Return all ready for Guzzle parameters
172
     *
173
     * @return array
174
     * @throws ErrorException
175
     */
176 11
    public function guzzle(): array
177
    {
178
        $options = [
179
            // 'base_uri'        => $this->get('base_uri'), // By some reasons base_uri option is not work anymore
180 11
            'timeout'         => $this->get('timeout'),
181 11
            'track_redirects' => $this->get('track_redirects'),
182 11
            'debug'           => $this->get('debug'),
183
            'headers'         => [
184 11
                'User-Agent' => $this->get('user_agent'),
185 11
                'X-API-KEY'  => $this->get('api_key'),
186
            ]
187
        ];
188
189
        // Proxy is optional
190 11
        if (isset($this->proxy)) {
0 ignored issues
show
Bug Best Practice introduced by
The property proxy does not exist on Resova\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
            $options['proxy'] = $this->proxy;
192
        }
193
194 11
        return $options;
195
    }
196
}
197