MaxMind   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 12
eloc 33
c 6
b 0
f 1
dl 0
loc 128
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserId() 0 3 1
A getDatabasePath() 0 3 1
A newReader() 0 3 1
A hydrate() 0 12 1
A newClient() 0 3 1
A isWebServiceEnabled() 0 3 1
A process() 0 20 3
A url() 0 3 1
A getLicenseKey() 0 3 1
A getOptions() 0 3 1
1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use Exception;
6
use GeoIp2\Database\Reader;
7
use GeoIp2\WebService\Client;
8
use Illuminate\Support\Fluent;
9
use Stevebauman\Location\Position;
10
11
class MaxMind extends Driver
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function url($ip)
17
    {
18
        return;
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function hydrate(Position $position, Fluent $location)
25
    {
26
        $position->countryName = $location->country;
27
        $position->countryCode = $location->country_code;
28
        $position->isoCode = $location->country_code;
29
        $position->cityName = $location->city;
30
        $position->postalCode = $location->postal;
31
        $position->metroCode = $location->metro_code;
32
        $position->latitude = $location->latitude;
33
        $position->longitude = $location->longitude;
34
35
        return $position;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function process($ip)
42
    {
43
        try {
44
            $maxmind = $this->isWebServiceEnabled() ?
45
                $this->newClient($this->getUserId(), $this->getLicenseKey(), $this->getOptions()) :
46
                $this->newReader($this->getDatabasePath());
47
48
            $record = $maxmind->city($ip);
49
50
            return new Fluent([
51
                'country' => $record->country->name,
52
                'country_code' => $record->country->isoCode,
53
                'city' => $record->city->name,
54
                'postal' => $record->postal->code,
55
                'latitude' => (string) $record->location->latitude,
56
                'longitude' => (string) $record->location->longitude,
57
                'metro_code' => (string) $record->location->metroCode,
58
            ]);
59
        } catch (Exception $e) {
60
            return false;
61
        }
62
    }
63
64
    /**
65
     * Returns a new MaxMind web service client.
66
     *
67
     * @param string $userId
68
     * @param string $licenseKey
69
     * @param array  $options
70
     *
71
     * @return Client
72
     */
73
    protected function newClient($userId, $licenseKey, array $options = [])
74
    {
75
        return new Client($userId, $licenseKey, $options);
0 ignored issues
show
Bug introduced by Steve Bauman
$userId of type string is incompatible with the type integer expected by parameter $userId of GeoIp2\WebService\Client::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        return new Client(/** @scrutinizer ignore-type */ $userId, $licenseKey, $options);
Loading history...
76
    }
77
78
    /**
79
     * Returns a new MaxMind reader client with
80
     * the specified database file path.
81
     *
82
     * @param string $path
83
     *
84
     * @return \GeoIp2\Database\Reader
85
     */
86
    protected function newReader($path)
87
    {
88
        return new Reader($path);
89
    }
90
91
    /**
92
     * Returns true / false if the MaxMind web service is enabled.
93
     *
94
     * @return mixed
95
     */
96
    protected function isWebServiceEnabled()
97
    {
98
        return config('location.maxmind.web.enabled', false);
99
    }
100
101
    /**
102
     * Returns the configured MaxMind web user ID.
103
     *
104
     * @return string
105
     */
106
    protected function getUserId()
107
    {
108
        return config('location.maxmind.web.user_id');
109
    }
110
111
    /**
112
     * Returns the configured MaxMind web license key.
113
     *
114
     * @return string
115
     */
116
    protected function getLicenseKey()
117
    {
118
        return config('location.maxmind.web.license_key');
119
    }
120
121
    /**
122
     * Returns the configured MaxMind web option array.
123
     *
124
     * @return array
125
     */
126
    protected function getOptions()
127
    {
128
        return config('location.maxmind.web.options', []);
129
    }
130
131
    /**
132
     * Returns the MaxMind database file path.
133
     *
134
     * @return string
135
     */
136
    protected function getDatabasePath()
137
    {
138
        return config('location.maxmind.local.path', database_path('maxmind/GeoLite2-City.mmdb'));
139
    }
140
}
141