|
16 | 16 | use Kint\Renderer\TextRenderer; |
17 | 17 | use Kint\Renderer\RichRenderer; |
18 | 18 | use UniFi_API\Client as ApiClient; |
| 19 | +use UniFi_API\Exceptions\CurlExtensionNotLoadedException; |
| 20 | +use UniFi_API\Exceptions\CurlGeneralErrorException; |
| 21 | +use UniFi_API\Exceptions\CurlTimeoutException; |
| 22 | +use UniFi_API\Exceptions\InvalidBaseUrlException; |
| 23 | +use UniFi_API\Exceptions\InvalidSiteNameException; |
| 24 | +use UniFi_API\Exceptions\JsonDecodeException; |
| 25 | +use UniFi_API\Exceptions\LoginFailedException; |
| 26 | +use UniFi_API\Exceptions\LoginRequiredException; |
19 | 27 |
|
20 | 28 | /** |
21 | 29 | * Load the configuration file if readable. |
|
127 | 135 | /** |
128 | 136 | * Create an instance of the Unifi API client class, log in to the controller and pull the requested data. |
129 | 137 | */ |
130 | | - $unifi_connection = new ApiClient( |
131 | | - trim($controller['user']), |
132 | | - trim($controller['password']), |
133 | | - trim(rtrim($controller['url'], "/")), |
134 | | - $site_id |
135 | | - ); |
136 | 138 |
|
137 | | - $login_results = $unifi_connection->login(); |
| 139 | + try { |
| 140 | + $unifi_connection = new ApiClient( |
| 141 | + trim($controller['user']), |
| 142 | + trim($controller['password']), |
| 143 | + trim(rtrim($controller['url'], "/")), |
| 144 | + $site_id |
| 145 | + ); |
| 146 | + |
| 147 | + $login_results = $unifi_connection->login(); |
| 148 | + } catch (CurlExtensionNotLoadedException $e) { |
| 149 | + $results['state'] = 'error'; |
| 150 | + $results['message'] = 'cURL is not available in your PHP installation!'; |
| 151 | + return; |
| 152 | + } catch (CurlGeneralErrorException $e) { |
| 153 | + $results['state'] = 'error'; |
| 154 | + $results['message'] = 'We have encountered a general cURL error! Please check the logs'; |
| 155 | + return; |
| 156 | + } catch (CurlTimeoutException $e) { |
| 157 | + $results['state'] = 'error'; |
| 158 | + $results['message'] = 'UniFi controller connection timeout!'; |
| 159 | + return; |
| 160 | + } catch (InvalidBaseUrlException $e) { |
| 161 | + $results['state'] = 'error'; |
| 162 | + $results['message'] = 'UniFi controller login failure, base URL is invalid!'; |
| 163 | + return; |
| 164 | + } catch (InvalidSiteNameException $e) { |
| 165 | + $results['state'] = 'error'; |
| 166 | + $results['message'] = 'UniFi controller login failure, site name is invalid!'; |
| 167 | + return; |
| 168 | + } catch (LoginFailedException $e) { |
| 169 | + $results['state'] = 'error'; |
| 170 | + $results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!'; |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * We can safely continue. |
| 176 | + */ |
| 177 | + $time_1 = microtime(true); |
| 178 | + $time_after_login = $time_1 - $time_start; |
138 | 179 |
|
139 | 180 | /** |
140 | | - * Check for login errors. |
| 181 | + * We then determine which method is required and which parameters to pass. |
| 182 | + * https://stackoverflow.com/questions/1005857/how-to-call-a-function-from-a-string-stored-in-a-variable |
141 | 183 | */ |
142 | | - if ($login_results === 400) { |
| 184 | + try { |
| 185 | + if (count($params) === 0) { |
| 186 | + $request_results = $unifi_connection->{$method}(); |
| 187 | + } else { |
| 188 | + $request_results = $unifi_connection->{$method}(...$params); |
| 189 | + } |
| 190 | + } catch (JsonDecodeException $e) { |
143 | 191 | $results['state'] = 'error'; |
144 | | - $results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!'; |
145 | | - } else { |
| 192 | + $results['message'] = 'JSON decode error!'; |
| 193 | + return; |
| 194 | + } catch (LoginRequiredException $e) { |
| 195 | + $results['state'] = 'error'; |
| 196 | + $results['message'] = 'Login is required for this endpoint'; |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + if (!empty($request_results)) { |
146 | 201 | /** |
147 | | - * We can safely continue. |
| 202 | + * Count the array items and inject $data_array into $results. |
148 | 203 | */ |
149 | | - $time_1 = microtime(true); |
150 | | - $time_after_login = $time_1 - $time_start; |
| 204 | + if (is_array($request_results)) { |
| 205 | + $results['count'] = count($request_results); |
| 206 | + } |
151 | 207 |
|
152 | 208 | /** |
153 | | - * We then determine which method is required and which parameters to pass. |
154 | | - * https://stackoverflow.com/questions/1005857/how-to-call-a-function-from-a-string-stored-in-a-variable |
| 209 | + * For results returned from API v2, the $request_results are an object, and we need to check for the |
| 210 | + * 'data' property and count items in that array. |
155 | 211 | */ |
156 | | - if (count($params) === 0) { |
157 | | - $request_results = $unifi_connection->{$method}(); |
158 | | - } else { |
159 | | - $request_results = $unifi_connection->{$method}(...$params); |
| 212 | + if (is_object($request_results) && property_exists($request_results, 'data',)) { |
| 213 | + $results['count'] = count($request_results->data); |
160 | 214 | } |
161 | 215 |
|
162 | | - if (!empty($request_results)) { |
163 | | - /** |
164 | | - * Count the array items and inject $data_array into $results. |
165 | | - */ |
166 | | - if (is_array($request_results)) { |
167 | | - $results['count'] = count($request_results); |
168 | | - } |
| 216 | + if ($debug) { |
| 217 | + error_log('DEBUG: ' . $results['count'] . ' objects collected'); |
| 218 | + } |
169 | 219 |
|
| 220 | + if ($output_method === 'kint') { |
170 | 221 | /** |
171 | | - * For results returned from API v2, the $request_results are an object, and we need to check for the |
172 | | - * 'data' property and count items in that array. |
| 222 | + * For Kint, we need to return the results in a slightly different manner. |
| 223 | + * |
| 224 | + * @note using Rich render mode |
173 | 225 | */ |
174 | | - if(is_object($request_results) && property_exists($request_results, 'data', )) { |
175 | | - $results['count'] = count($request_results->data); |
176 | | - } |
177 | | - |
178 | | - if ($debug) { |
179 | | - error_log('DEBUG: ' . $results['count'] . ' objects collected'); |
180 | | - } |
181 | | - |
182 | | - if ($output_method === 'kint') { |
| 226 | + Kint::$display_called_from = false; |
| 227 | + RichRenderer::$folder = false; |
| 228 | + $results['data'] = @d($request_results); |
| 229 | + } else { |
| 230 | + if ($output_method === 'kint_plain') { |
183 | 231 | /** |
184 | | - * For Kint, we need to return the results in a slightly different manner. |
185 | | - * |
186 | | - * @note using Rich render mode |
| 232 | + * @note using Plain render mode |
187 | 233 | */ |
188 | 234 | Kint::$display_called_from = false; |
189 | 235 | RichRenderer::$folder = false; |
190 | | - $results['data'] = @d($request_results); |
| 236 | + TextRenderer::$decorations = false; |
| 237 | + $results['data'] = @s($request_results); |
191 | 238 | } else { |
192 | | - if ($output_method === 'kint_plain') { |
193 | | - /** |
194 | | - * @note using Plain render mode |
195 | | - */ |
196 | | - Kint::$display_called_from = false; |
197 | | - RichRenderer::$folder = false; |
198 | | - TextRenderer::$decorations = false; |
199 | | - $results['data'] = @s($request_results); |
200 | | - } else { |
201 | | - $results['data'] = $request_results; |
202 | | - } |
| 239 | + $results['data'] = $request_results; |
203 | 240 | } |
204 | 241 | } |
| 242 | + } |
205 | 243 |
|
206 | | - /** |
207 | | - * Execute timing of data collection from UniFi controller. |
208 | | - */ |
209 | | - $time_2 = microtime(true); |
210 | | - $time_after_load = $time_2 - $time_start; |
| 244 | + /** |
| 245 | + * Execute timing of data collection from UniFi controller. |
| 246 | + */ |
| 247 | + $time_2 = microtime(true); |
| 248 | + $time_after_load = $time_2 - $time_start; |
211 | 249 |
|
212 | | - /** |
213 | | - * Calculate all the timings/percentages. |
214 | | - */ |
215 | | - $time_end = microtime(true); |
216 | | - $time_total = $time_end - $time_start; |
217 | | - $login_percentage = ($time_after_login / $time_total) * 100; |
218 | | - $load_percentage = (($time_after_load - $time_after_login) / $time_total) * 100; |
219 | | - |
220 | | - $results['timings']['login'] = $time_after_login; |
221 | | - $results['timings']['load'] = $time_after_load; |
222 | | - $results['timings']['login_perc'] = $login_percentage; |
223 | | - $results['timings']['load_perc'] = $load_percentage; |
224 | | - } |
| 250 | + /** |
| 251 | + * Calculate all the timings/percentages. |
| 252 | + */ |
| 253 | + $time_end = microtime(true); |
| 254 | + $time_total = $time_end - $time_start; |
| 255 | + $login_percentage = ($time_after_login / $time_total) * 100; |
| 256 | + $load_percentage = (($time_after_load - $time_after_login) / $time_total) * 100; |
| 257 | + |
| 258 | + $results['timings']['login'] = $time_after_login; |
| 259 | + $results['timings']['load'] = $time_after_load; |
| 260 | + $results['timings']['login_perc'] = $login_percentage; |
| 261 | + $results['timings']['load_perc'] = $load_percentage; |
225 | 262 | } |
226 | 263 | } |
227 | 264 |
|
|
0 commit comments