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

LocationService::setUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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