Completed
Push — master ( a3c64e...df0f49 )
by Christofer
02:30
created

Location::getLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chai17\Models;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Chai17\Models;
8
9
class Location implements ContainerInjectableInterface
10
{
11
12
    use ContainerInjectableTrait;
13
14
    private $location = [];
15
16
    public function setLocation($ipNumber)
17
    {
18
        $location = $this->location;
0 ignored issues
show
Unused Code introduced by
The assignment to $location is dead and can be removed.
Loading history...
19
        $api = $this->di->get("ipstack");
20
        $ipstack = $api["config"];
21
        $api = $this->di->get("mapquest");
22
        $mapquest = $api["config"];
23
        $validateIP = new Models\ValidateIP;
24
        $test = $validateIP->validate($ipNumber);
25
        if ($test[0]) {
26
            $location = $getJson->cUrl($ipstack["url"]. $ipNumber. '?access_key='. $ipstack["key"]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $getJson seems to be never defined.
Loading history...
27
            $location = json_decode($location, true);
28
            $latitude = $location["latitude"];
29
            $longitude = $location["longitude"];
30
            $location = [$location["city"], $latitude, $longitude];
31
        } else {
32
            $search = array('å','ä','ö');
33
            $replace = array('a','a','o');
34
            $ipNumber = str_replace($search, $replace, $ipNumber);
35
            $location = $getJson->cUrl($mapquest["url"]. $mapquest["key"]. $mapquest["extra"]. $ipNumber);
36
            $location = json_decode($location, true);
37
            $latitude = $location["results"][0]["locations"][0]["latLng"]["lat"];
38
            $longitude = $location["results"][0]["locations"][0]["latLng"]["lng"];
39
            $city = $location["results"][0]["locations"][0]["adminArea5"] ?? "Ingen ort";
40
            $location = [$city, $latitude, $longitude];
41
        }
42
43
        $this->location = $location;
44
45
    }
46
47
    public function getLocation()
48
    {
49
        return $this->location;
50
    }
51
}
52