Completed
Push — master ( 6c5932...f55ac0 )
by ARCANEDEV
07:13
created

IpApiDriver::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\GeoIP\Drivers;
2
3
use Arcanedev\GeoIP\Entities\Continents;
4
use Exception;
5
use GuzzleHttp\Client;
6
7
/**
8
 * Class     IpApiDriver
9
 *
10
 * @package  Arcanedev\GeoIP\Drivers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class IpApiDriver extends AbstractDriver
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Http client instance.
21
     *
22
     * @var \GuzzleHttp\Client
23
     */
24
    protected $client;
25
26
    /**
27
     * A collection of continents.
28
     *
29
     * @var \Arcanedev\GeoIP\Entities\Continents
30
     */
31
    protected $continents;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Main Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Init the driver.
39
     */
40 18
    protected function init()
41
    {
42 18
        $this->client = new Client(
43 18
            $this->getHttpClientConfig()
44 9
        );
45
46 18
        $this->continents = Continents::make();
47 18
    }
48
49
    /**
50
     * Locate the ip address.
51
     *
52
     * @param  string  $ipAddress
53
     *
54
     * @return \Arcanedev\GeoIP\Location
55
     *
56
     * @throws \Exception
57
     */
58 6
    public function locate($ipAddress)
59
    {
60 6
        $response = $this->client->get("json/$ipAddress");
61
62
        // Parse body content
63 6
        $data = json_decode($response->getBody());
64
65
        // Verify response status
66 6
        if ($data->status !== 'success') {
67
            throw new Exception("Request failed ({$data->message})");
68
        }
69
70 6
        return $this->hydrate([
71 6
            'ip'          => $ipAddress,
72 6
            'iso_code'    => $data->countryCode,
73 6
            'country'     => $data->country,
74 6
            'city'        => $data->city,
75 6
            'state'       => $data->regionName,
76 6
            'state_code'  => $data->region,
77 6
            'postal_code' => $data->zip,
78 6
            'latitude'    => $data->lat,
79 6
            'longitude'   => $data->lon,
80 6
            'timezone'    => $data->timezone,
81 6
            'continent'   => $this->continents->get($data->countryCode, 'Unknown'),
82 3
        ]);
83
    }
84
85
    /**
86
     * Get the http client config.
87
     *
88
     * @return array
89
     */
90 18
    private function getHttpClientConfig()
91
    {
92
        $config = [
93 18
            'base_uri' => 'http://ip-api.com/',
94
            'headers' => [
95 9
                'User-Agent' => 'Laravel-GeoIP',
96 9
            ],
97
            'query' => [
98 9
                'fields' => 16895,
99 9
            ],
100 9
        ];
101
102
        // Using the Pro service
103 18
        if ($this->getOption('key')) {
104
            $config['base_uri']     = $this->isSecure() ? 'https://pro.ip-api.com/' : 'http://pro.ip-api.com/';
105
            $config['query']['key'] = $this->getOption('key');
106
        }
107
108 18
        return $config;
109
    }
110
111
    /**
112
     * Check if secure url.
113
     *
114
     * @return bool
115
     */
116
    private function isSecure()
117
    {
118
        return (bool) $this->getOption('secure');
119
    }
120
}
121