Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RebuildLocationService
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rebuild
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
2
 getLocations
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors_geoip\Service;
4
5use Drupal\Core\Database\Connection;
6use Drupal\visitors_geoip\VisitorsGeoIpInterface;
7use Drupal\visitors_geoip\VisitorsGeoIpRebuildLocationInterface;
8
9/**
10 * Rebuild the location from the IP address.
11 */
12class RebuildLocationService implements VisitorsGeoIpRebuildLocationInterface {
13
14  /**
15   * The database connection.
16   *
17   * @var \Drupal\Core\Database\Connection
18   */
19  protected $database;
20
21  /**
22   * The GeoIP reader.
23   *
24   * @var \Drupal\visitors_geoip\VisitorsGeoIpInterface
25   */
26  protected $geoip;
27
28  /**
29   * Constructs a new Rebuild Location Service.
30   *
31   * @param \Drupal\Core\Database\Connection $database
32   *   The database connection.
33   * @param \Drupal\visitors_geoip\VisitorsGeoIpInterface $geoip
34   *   The GeoIP reader.
35   */
36  public function __construct(Connection $database, VisitorsGeoIpInterface $geoip) {
37    $this->database = $database;
38    $this->geoip = $geoip;
39  }
40
41  /**
42   * {@inheritdoc}
43   */
44  public function rebuild(string $ip_address) {
45    /** @var \GeoIp2\Model\City|null $location */
46    $location = $this->geoip->city($ip_address);
47    if (!$location) {
48      return NULL;
49    }
50
51    $geoip_data                   = [];
52    $geoip_data['continent_code'] = $location->continent->code;
53    $geoip_data['country_code']   = $location->country->isoCode;
54    $geoip_data['region']         = $location->subdivisions[0]->isoCode;
55    $geoip_data['city']           = $location->city->names['en'];
56    $geoip_data['latitude']       = $location->location->latitude;
57    $geoip_data['longitude']      = $location->location->longitude;
58
59    $fields['location_continent'] = $geoip_data['continent_code'];
60    $fields['location_country']   = $geoip_data['country_code'];
61    $fields['location_region']    = $geoip_data['region'];
62    $fields['location_city']      = $geoip_data['city'];
63    $fields['location_latitude']  = $geoip_data['latitude'];
64    $fields['location_longitude'] = $geoip_data['longitude'];
65
66    $this->database->update('visitors')
67      ->fields($fields)
68      ->condition('visitors_ip', $ip_address)
69      ->condition('location_latitude', NULL, 'IS NULL')
70      ->execute();
71  }
72
73  /**
74   * {@inheritdoc}
75   */
76  public function getLocations() {
77
78    $missing_location = $this->database->select('visitors', 'v')
79      ->fields('v', ['visitors_ip'])
80      ->condition('location_latitude', NULL, 'IS NULL')
81      ->distinct()
82      ->execute()
83      ->fetchAll(\PDO::FETCH_COLUMN, 0);
84
85    return $missing_location;
86  }
87
88}