Completed
Push — master ( cd46ad...a80418 )
by
unknown
37:43
created

GeoPluginAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadLocationData() 0 6 1
A getLocationData() 0 24 2
A getName() 0 4 1
1
<?php
2
3
namespace yiicod\geo\adapters\geoPlugin;
4
5
use yiicod\geo\base\GeoAdapterInterface;
6
use yiicod\geo\base\GeoDataInterface;
7
use yiicod\geo\exceptions\EmptyLocationDataException;
8
use yiicod\geo\GeoData;
9
10
/**
11
 * Class GeoPluginAdapter
12
 * Geo getter based on the GeoPlugin source (http://www.geoplugin.net/)
13
 *
14
 * @package yiicod\geo\adapters\geoPlugin
15
 *
16
 * @author Dmitry Turchanin
17
 */
18
class GeoPluginAdapter implements GeoAdapterInterface
19
{
20
    /**
21
     * Loads location data from source for $ip
22
     *
23
     * @param $ip
24
     *
25
     * @return bool|string
26
     */
27
    public function loadLocationData($ip)
28
    {
29
        $xml = file_get_contents('http://www.geoplugin.net/xml.gp?ip=' . $ip);
30
31
        return $xml;
32
    }
33
34
    /**
35
     * Gets location data from loaded data
36
     *
37
     * @param $ip
38
     *
39
     * @return GeoDataInterface
40
     *
41
     * @throws EmptyLocationDataException
42
     */
43
    public function getLocationData($ip): GeoDataInterface
44
    {
45
        $xml = $this->loadLocationData($ip);
46
        $xml = simplexml_load_string($xml);
47
48
        if (false === $xml) {
49
            throw new EmptyLocationDataException("Location data is empty");
50
        }
51
52
        $data = [
53
            'ip' => $ip,
54
            'countryCode' => (string)$xml->geoplugin_countryCode,
55
            'countryName' => (string)$xml->geoplugin_countryName,
56
            'regionName' => (string)$xml->geoplugin_region,
57
            'regionCode' => (string)$xml->geoplugin_regionCode,
58
            'city' => (string)$xml->geoplugin_city,
59
            'latitude' => (float)$xml->geoplugin_latitude,
60
            'longitude' => (float)$xml->geoplugin_longitude,
61
        ];
62
63
        $result = new GeoData($data);
64
65
        return $result;
66
    }
67
68
    /**
69
     * Gets adapter's name
70
     *
71
     * @return string
72
     */
73
    public function getName(): string
74
    {
75
        return 'geo_plugin';
76
    }
77
}
78