Completed
Push — master ( 1d4f14...5b6fa1 )
by Steven
12:16
created

MaxMind::getOptions()   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 View Code Duplication
    protected function hydrate(Position $position, Fluent $location)
0 ignored issues
show
Duplication introduced by Steve Bauman
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $position->driver = MaxMind::class;
26
27
        $position->countryName = $location->country;
28
        $position->countryCode = $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);
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