Issues (2963)

app/ApiClients/GeocodingHelper.php (1 issue)

Severity
1
<?php
2
/**
3
 * GeocodingHelper.php *
4
 * -Description-
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * @link       https://www.librenms.org
20
 *
21
 * @copyright  2018 Tony Murray
22
 * @author     Tony Murray <[email protected]>
23
 */
24
25
namespace App\ApiClients;
26
27
use Exception;
28
use LibreNMS\Config;
29
use Log;
30
31
trait GeocodingHelper
32
{
33
    /**
34
     * From BaseApi...
35
     *
36
     * @return \GuzzleHttp\Client
37
     */
38
    abstract protected function getClient();
39
40
    /**
41
     * Try to get the coordinates of a given address.
42
     * If unsuccessful, the returned array will be empty
43
     *
44
     * @param  string  $address
45
     * @return array ['lat' => 0, 'lng' => 0]
46
     */
47
    public function getCoordinates($address)
48
    {
49
        if (! Config::get('geoloc.latlng', true)) {
50
            Log::debug('Geocoding disabled');
51
52
            return [];
53
        }
54
55
        try {
56
            $options = $this->buildGeocodingOptions($address);
57
58
            $response = $this->getClient()->get($this->geocoding_uri, $options);
59
            $response_data = json_decode($response->getBody(), true);
60
            if ($this->checkResponse($response, $response_data)) {
61
                return $this->parseLatLng($response_data);
62
            } else {
63
                Log::error('Geocoding failed.', ['response' => $response_data]);
64
            }
65
        } catch (Exception $e) {
66
            Log::error('Geocoding failed: ' . $e->getMessage());
67
        }
68
69
        return [];
70
    }
71
72
    /**
73
     * Checks if the request was a success
74
     *
75
     * @param  \Psr\Http\Message\ResponseInterface  $response
76
     * @param  array  $data  decoded response data
77
     * @return bool
78
     */
79
    protected function checkResponse($response, $data)
0 ignored issues
show
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    protected function checkResponse($response, /** @scrutinizer ignore-unused */ $data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        return $response->getStatusCode() == 200;
82
    }
83
84
    /**
85
     * Get latitude and longitude from geocode response
86
     *
87
     * @param  array  $data
88
     * @return array
89
     */
90
    abstract protected function parseLatLng($data);
91
92
    /**
93
     * Build Guzzle request option array
94
     *
95
     * @param  string  $address
96
     * @return array
97
     *
98
     * @throws \Exception you may throw an Exception if validation fails
99
     */
100
    abstract protected function buildGeocodingOptions($address);
101
}
102