IpGeoWeather::getJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Anax\IpGeo;
4
use Anax\Commons\ContainerInjectableInterface;
5
use Anax\Commons\ContainerInjectableTrait;
6
7
8
/**
9
 * A model class retrievieng data from an external server.
10
 *
11
 * @SuppressWarnings(PHPMD.ShortVariable)
12
 */
13
class IpGeoWeather implements ContainerInjectableInterface
14
{
15
    use ContainerInjectableTrait;
16
    // load the config file of key
17
    private $apikey = "";
18
19 2
    public function setKey($key) : void
20
    {
21 2
        $this->apikey = $key;
22 2
    }
23
24
25 2
    public function getJson(string $ipAdd)
26
    {
27 2
        $url = "http://api.ipstack.com/$ipAdd?access_key={$this->apikey}&hostname=1";
28 2
        $res = file_get_contents($url);
29 2
        $ipinfo = json_decode($res, true);
30 2
        $lat = $ipinfo["latitude"];
31 2
        $lng =  $ipinfo["longitude"];
32 2
        $ipinfo['link'] = "https://www.openstreetmap.org/?mlat=$lat&mlon=$lng#map=15/$lat/$lng";
33
        // var_dump($ipinfo);
34 2
        return $ipinfo;
35
    }
36
}
37