Passed
Push — main ( c42d70...1b364a )
by Åsa
02:29
created

LocationService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
c 1
b 0
f 1
dl 0
loc 65
ccs 25
cts 27
cp 0.9259
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A curlIpApi() 0 28 5
A setUrl() 0 3 1
A getKey() 0 3 1
A setKey() 0 3 1
A getUrl() 0 3 1
1
<?php
2
3
namespace Asti\Weather;
4
5
class LocationService
6
{
7
8
9
    /*
10
     *
11
     * curls api, get_file_contents
12
     * sends response to model
13
     *
14
     */
15
16
    private $key = null;
17
    private $url = null;
18
19 2
    public function setKey($key)
20
    {
21 2
        $this->key = $key;
22 2
    }
23
24 2
    public function setUrl($url): void
25
    {
26 2
        $this->url = $url;
27 2
    }
28
29 2
    public function getKey(): string
30
    {
31 2
        return $this->key;
32
    }
33
34 2
    public function getUrl(): string
35
    {
36 2
        return $this->url;
37
    }
38
39
40
41
42 2
    public function curlIpApi($ipAdr): array
43
    {
44 2
        $curl = new CurlService();
45 2
        if ($ipAdr!= "") {
46 2
            $res = $curl->getDataThroughCurl($this->getUrl() . $ipAdr . "?access_key=" . $this->getKey());
47
48 2
            if ($res["type"] == null) {
49
                $json = [
50 1
                    "Message" => "IP-adressen är fel. Försök igen!"
51
                ];
52 1
                return $json;
53
            }
54
            $json = [
55 2
                "Type" => $res["type"],
56 2
                "Valid" => $res["type"] ? "ipv4" || "ipv6" : "not valid",
57 2
                "UserInput" => $res["ip"],
58 2
                "Latitude" => $res["latitude"],
59 2
                "Longitude" => $res["longitude"],
60 2
                "City" => $res["city"],
61 2
                "Country" => $res["country_name"],
62
            ];
63 2
            return [$json];
64
        }
65
        else {
66
            $json = [
67
                "Message" => "IP-adressen är tom. Försök igen"
68
            ];
69
            return $json;
70
        }
71
    }
72
}
73