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