GeoApi::findGeoLocation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 14
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 28
ccs 0
cts 14
cp 0
crap 2
rs 9.7998
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