Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
GeoIpService | |
100.00% |
23 / 23 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
metadata | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
city | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getReader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setReader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasLibrary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasExtension | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Drupal\visitors_geoip\Service; |
4 | |
5 | use Drupal\Core\Config\ConfigFactoryInterface; |
6 | use Drupal\Core\File\FileSystemInterface; |
7 | use Drupal\visitors_geoip\VisitorsGeoIpInterface; |
8 | use GeoIp2\Database\Reader; |
9 | |
10 | /** |
11 | * GeoIp lookup Service. |
12 | * |
13 | * @package visitors |
14 | */ |
15 | class GeoIpService implements VisitorsGeoIpInterface { |
16 | |
17 | /** |
18 | * The GeoIP reader. |
19 | * |
20 | * @var \GeoIp2\Database\Reader|null |
21 | */ |
22 | protected $reader; |
23 | |
24 | /** |
25 | * Constructs a new GeoIpService object. |
26 | * |
27 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
28 | * The config factory. |
29 | * @param \Drupal\Core\File\FileSystemInterface $file_system |
30 | * The file system service. |
31 | */ |
32 | public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system) { |
33 | $settings = $config_factory->get('visitors_geoip.settings'); |
34 | $path = $settings->get('geoip_path'); |
35 | $free_database = $path . '/GeoLite2-City.mmdb'; |
36 | $better_database = $path . '/GeoIP2-City.mmdb'; |
37 | $database = NULL; |
38 | if ($file_system->realPath($better_database)) { |
39 | $database = $better_database; |
40 | } |
41 | elseif ($file_system->realPath($free_database)) { |
42 | $database = $free_database; |
43 | } |
44 | |
45 | if ($database) { |
46 | $this->reader = new Reader($database); |
47 | } |
48 | |
49 | } |
50 | |
51 | /** |
52 | * {@inheritdoc} |
53 | */ |
54 | public function metadata() { |
55 | if (is_null($this->reader)) { |
56 | return NULL; |
57 | } |
58 | $metadata = $this->reader->metadata(); |
59 | return $metadata; |
60 | } |
61 | |
62 | /** |
63 | * {@inheritdoc} |
64 | */ |
65 | public function city($ip_address) { |
66 | if (is_null($this->reader)) { |
67 | return NULL; |
68 | } |
69 | $record = $this->reader->city($ip_address); |
70 | return $record; |
71 | } |
72 | |
73 | /** |
74 | * {@inheritdoc} |
75 | */ |
76 | public function getReader() { |
77 | return $this->reader; |
78 | } |
79 | |
80 | /** |
81 | * {@inheritdoc} |
82 | */ |
83 | public function setReader($reader) { |
84 | $this->reader = $reader; |
85 | } |
86 | |
87 | /** |
88 | * {@inheritdoc} |
89 | */ |
90 | public function hasLibrary($class_name = 'GeoIp2\Database\Reader'): bool { |
91 | return class_exists($class_name); |
92 | } |
93 | |
94 | /** |
95 | * {@inheritdoc} |
96 | */ |
97 | public function hasExtension($extension = 'maxminddb'): bool { |
98 | return extension_loaded($extension); |
99 | } |
100 | |
101 | } |