Completed
Branch master (954974)
by Steven
06:07 queued 44s
created

MaxMind::getUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use GeoIp2\Database\Reader;
6
use GeoIp2\WebService\Client;
7
use Illuminate\Support\Fluent;
8
use Stevebauman\Location\Position;
9
10
class MaxMind extends Driver
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function url()
16
    {
17
        return;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function hydrate(Position $position, Fluent $location)
24
    {
25
        $position->countryName = $location->country;
26
        $position->cityName = $location->city;
27
        $position->postalCode = $location->postal;
28
        $position->latitude = $location->latitude;
29
        $position->longitude = $location->longitude;
30
31
        return $position;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function process($ip)
38
    {
39
        try {
40
            $maxmind = $this->isWebServiceEnabled() ?
41
                $this->newClient($this->getUserId(), $this->getLicenseKey()) :
42
                $this->newReader($this->getDatabasePath());
43
44
            $record = $maxmind->city($ip);
45
46
            return new Fluent([
47
                'country' => $record->country->name,
48
                'city' => $record->city->name,
49
                'postal' => $record->postal->code,
50
                'latitude' => $record->location->latitude,
51
                'longitude' => $record->location->longitude,
52
            ]);
53
        } catch (\Exception $e) {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * Returns a new MaxMind web service client.
60
     *
61
     * @param string $userId
62
     * @param string $licenseKey
63
     *
64
     * @return Client
65
     */
66
    protected function newClient($userId, $licenseKey)
67
    {
68
        return new Client($userId, $licenseKey);
69
    }
70
71
    /**
72
     * Returns a new MaxMind reader client with
73
     * the specified database file path.
74
     *
75
     * @param string $path
76
     *
77
     * @return \GeoIp2\Database\Reader
78
     */
79
    protected function newReader($path)
80
    {
81
        return new Reader($path);
82
    }
83
84
    /**
85
     * Returns true / false if the MaxMind web service is enabled.
86
     *
87
     * @return mixed
88
     */
89
    protected function isWebServiceEnabled()
90
    {
91
        return config('location.maxmind.web.enabled', false);
92
    }
93
94
    /**
95
     * Returns the configured MaxMinds web user ID.
96
     *
97
     * @return string
98
     */
99
    protected function getUserId()
100
    {
101
        return config('location.maxmind.web.user_id');
102
    }
103
104
    /**
105
     * Returns the configured MaxMinds web license key.
106
     *
107
     * @return string
108
     */
109
    protected function getLicenseKey()
110
    {
111
        return config('location.maxmind.web.license_key');
112
    }
113
114
    /**
115
     * Returns the MaxMind database file path.
116
     *
117
     * @return string
118
     */
119
    protected function getDatabasePath()
120
    {
121
        return config('location.maxmind.local.path', database_path('maxmind/GeoLite2-City.mmdb'));
122
    }
123
}
124