GeoFinder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 186
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCountryCode() 0 4 1
A getCountryName() 0 4 1
A getRegion() 0 4 1
A getRegionName() 0 4 1
A getRegionCode() 0 4 1
A getCity() 0 4 1
A getLatitude() 0 4 1
A getLongitude() 0 4 1
A getPreparedCountryData() 0 28 5
1
<?php
2
3
namespace yiicod\geo;
4
5
use Exception;
6
use Yii;
7
use yii\base\Component;
8
use yiicod\geo\base\GeoAdapterInterface;
9
use yiicod\geo\base\GeoDataInterface;
10
use yiicod\geo\base\GeoFinderInterface;
11
use yiicod\geo\storages\StorageInterface;
12
13
/**
14
 * Class GeoManager
15
 *
16
 * @package yiicod\geo\components
17
 *
18
 * @author Dmitry Turchanin
19
 * @author Alexey Orlov
20
 */
21
class GeoFinder extends Component implements GeoFinderInterface
22
{
23
    /**
24
     * @var array
25
     */
26
    public $gettersList = [
27
        [
28
            'class' => \yiicod\geo\adapters\geoIp2\GeoIp2CityAdapter::class,
29
        ],
30
        [
31
            'class' => \yiicod\geo\adapters\geoPlugin\GeoPluginAdapter::class,
32
        ],
33
        [
34
            'class' => \yiicod\geo\adapters\ipstack\IpstackAdapter::class,
35
        ],
36
        [
37
//            'class' => \yiicod\geo\adapters\freeGeoIp\FreeGeoIpLocator::class
38
        ],
39
    ];
40
41
    /**
42
     * Cache expire value
43
     *
44
     * @var int
45
     */
46
    public $duration = 604800;
47
48
    /**
49
     * Storage instance
50
     *
51
     * @var StorageInterface
52
     */
53
    private $storage;
54
55
    /**
56
     * Prepared country data found in the one request
57
     *
58
     * @var array
59
     */
60
    private static $result = [];
61
62
    /**
63
     * GeoFinder constructor.
64
     *
65
     * @param StorageInterface $storage
66
     * @param array $config
67
     */
68
    public function __construct(StorageInterface $storage, $config = [])
69
    {
70
        parent::__construct($config);
71
72
        $this->storage = $storage;
73
    }
74
75
    /**
76
     * Gets country code by IP
77
     *
78
     * @param string|null
79
     *
80
     * @return string
81
     */
82
    public function getCountryCode(string $ip): ?string
83
    {
84
        return $this->getPreparedCountryData($ip)->getCountryCode();
85
    }
86
87
    /**
88
     * Gets country code by IP
89
     *
90
     * @param string|null
91
     *
92
     * @return string
93
     */
94
    public function getCountryName(string $ip): ?string
95
    {
96
        return $this->getPreparedCountryData($ip)->getCountryName();
97
    }
98
99
    /**
100
     * Gets region by IP
101
     *
102
     * @param string $ip
103
     *
104
     * @return null|string
105
     */
106
    public function getRegion(string $ip): ?string
107
    {
108
        return $this->getPreparedCountryData($ip)->getRegion();
109
    }
110
111
    /**
112
     * Gets region name by IP
113
     *
114
     * @param string $ip
115
     *
116
     * @return null|string
117
     */
118
    public function getRegionName(string $ip): ?string
119
    {
120
        return $this->getPreparedCountryData($ip)->getRegionName();
121
    }
122
123
    /**
124
     * Gets region code by IP
125
     *
126
     * @param string $ip
127
     *
128
     * @return null|string
129
     */
130
    public function getRegionCode(string $ip): ?string
131
    {
132
        return $this->getPreparedCountryData($ip)->getRegionCode();
133
    }
134
135
    /**
136
     * Gets city by IP
137
     *
138
     * @param string $ip
139
     *
140
     * @return null|string
141
     */
142
    public function getCity(string $ip): ?string
143
    {
144
        return $this->getPreparedCountryData($ip)->getCity();
145
    }
146
147
    /**
148
     * Gets latitude by IP
149
     *
150
     * @param string $ip
151
     *
152
     * @return null|string
153
     */
154
    public function getLatitude(string $ip): ?string
155
    {
156
        return $this->getPreparedCountryData($ip)->getLatitude();
157
    }
158
159
    /**
160
     * Gets longitude by IP
161
     *
162
     * @param string $ip
163
     *
164
     * @return null|string
165
     */
166
    public function getLongitude(string $ip): ?string
167
    {
168
        return $this->getPreparedCountryData($ip)->getLongitude();
169
    }
170
171
    /**
172
     * Gets prepared country data by IP (country code, country name)
173
     *
174
     * @param null|string|mixed $ip
175
     *
176
     * @return GeoDataInterface
177
     */
178
    public function getPreparedCountryData($ip): GeoDataInterface
179
    {
180
        if (true === isset(self::$result[$ip])) {
181
            return self::$result[$ip];
182
        }
183
184
        self::$result[$ip] = $this->storage->getOrSet($ip, function () use ($ip) {
185
            foreach ($this->gettersList as $key => $config) {
186
                try {
187
                    $adapter = Yii::createObject($config);
188
189
                    if (!($adapter instanceof GeoAdapterInterface)) {
190
                        unset($key);
191
                        continue;
192
                    }
193
194
                    return $adapter->getLocationData($ip);
195
                } catch (Exception $e) {
196
                    unset($key);
197
                    continue;
198
                }
199
            }
200
201
            return new GeoData(['ip' => $ip]);
202
        }, $this->duration);
203
204
        return self::$result[$ip];
205
    }
206
}
207