Geotag   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 97
ccs 24
cts 24
cp 1
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A renderMap() 0 23 1
A setApikey() 0 3 1
A getIPData() 0 5 1
A createUrl() 0 3 1
A getMap() 0 3 1
A setUrl() 0 3 1
1
<?php
2
3
namespace Nihl\RemoteService;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Nihl\RemoteService\Curl;
8
9
/**
10
 * Model class for fetching location data from an ip-address
11
 */
12
class Geotag implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
    /**
17
     * $ipDataUrl   Url to access IPData-service
18
     * $apikey      Apikey for IPData-service
19
     * $mapSiteUrl  Url for creating map, hardcoded to openstreetmap
20
     */
21
    private $ipDataUrl;
22
    private $apikey;
23
    private $mapSiteUrl = "https://www.openstreetmap.org";
24
25
    /**
26
     * Set ipDataUrl
27
     *
28
     * @param string $url
29
     */
30 38
    public function setUrl(string $url): void
31
    {
32 38
        $this->ipDataUrl = $url;
33 38
    }
34
35
    /**
36
     * Set apiKey
37
     *
38
     * @param string $key
39
     */
40 37
    public function setApikey(string $key): void
41
    {
42 37
        $this->apikey = $key;
43 37
    }
44
45
    /**
46
     * Get JSON-formatted data
47
     *
48
     * @param string $ipAddress
49
     *
50
     * @return array Array of data
51
     */
52 15
    public function getIPData(string $ipAddress = "")
53
    {
54 15
        $curl = $this->di->get("curl");
55 15
        $url = $this->createUrl($ipAddress);
56 15
        return json_decode($curl->doRequest($url), true);
57
    }
58
59
60
    // /**
61
    //  * Filters latitude and longitude from data
62
    //  *
63
    //  * @param string $ipAddress
64
    //  *
65
    //  * @return array Array of data
66
    //  */
67
    // public function getCoordinatesFromIP(string $ipAddress = "")
68
    // {
69
    //     $data = $this->getIPData($ipAddress);
70
    //     return [
71
    //         "lat" => $data["latitude"],
72
    //         "lon" => $data["longitude"]
73
    //     ];
74
    // }
75
76 17
    public function createUrl(string $ipAddress = "")
77
    {
78 17
        return "$this->ipDataUrl/$ipAddress?access_key=$this->apikey";
79
    }
80
81 15
    public function getMap($latitude, $longitude)
82
    {
83 15
        return "$this->mapSiteUrl/?mlat=$latitude&amp;mlon=$longitude#map=6/$latitude/$longitude";
84
    }
85
86 7
    public function renderMap($latitude, $longitude)
87
    {
88
        // Calculate map size
89 7
        $lat1 = $latitude * 0.998;
90 7
        $long1 = $longitude * 0.979;
91 7
        $lat2 = $latitude * 1.002;
92 7
        $long2 = $longitude * 1.017;
93
94
        // Create URLs for iframe src and a href
95 7
        $iframeUrl = "$this->mapSiteUrl/export/embed.html?bbox=$long1%2C$lat1%2C$long2%2C$lat2&amp;layer=mapnik&amp;marker=$latitude%2C$longitude";
96 7
        $mapUrl = $this->getMap($latitude, $longitude);
97
98
        $mapIframe = <<<EOD
99 7
<iframe width="450px" height="450px" src="$iframeUrl">
100
</iframe>
101
<br/>
102
<small>
103 7
    <a href="$mapUrl">
104
        Visa större karta
105
    </a>
106
</small>
107
EOD;
108 7
        return $mapIframe;
109
    }
110
}
111