GeoApi   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 35
ccs 0
cts 14
cp 0
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A findGeoLocation() 0 28 1
1
<?php
2
3
namespace Anax\Models;
4
5
class GeoApi extends IpValidator
6
{
7
    /**
8
     * model class for finding coordinates matching the ip adress
9
     * using the geolocation api 'ipstack'
10
     * child class to IpValidator
11
     */
12
    public function findGeoLocation($ipAdress)
13
    {
14
        global $di;
15
16
        // get the secret api key
17
        $config = $di->get("configuration")->load("api_keys.php") ?? null;
18
        $apiKey = $config["config"]["ipStack"]["apiKey"] ?? null;
19
20
        // make curl api call with ip address and api key
21
        $ch1 = curl_init('http://api.ipstack.com/'.$ipAdress.'?access_key='.$apiKey.'');
22
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

22
        curl_setopt(/** @scrutinizer ignore-type */ $ch1, CURLOPT_RETURNTRANSFER, true);
Loading history...
23
        $json = curl_exec($ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

23
        $json = curl_exec(/** @scrutinizer ignore-type */ $ch1);
Loading history...
24
        curl_close($ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

24
        curl_close(/** @scrutinizer ignore-type */ $ch1);
Loading history...
25
26
        /**
27
         * decode the json result
28
         * for usage in both view and rest-api controller
29
         */
30
        $result = json_decode($json, true);
31
32
        $data = [
33
            "city" => $result['city'] ?? "-",
34
            "country_name" => $result['country_name'] ?? "-",
35
            "longitude" => $result['longitude'] ?? "-",
36
            "latitude" => $result['latitude'] ?? "-"
37
        ];
38
39
        return $data;
40
    }
41
}
42