GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MapBlock::prepareRow()   B
last analyzed

Complexity

Conditions 9
Paths 12

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
cc 9
nc 12
nop 1
1
<?php
2
3
namespace Drupal\df_tools_map\Plugin\migrate\source;
4
5
use Drupal\migrate\Row;
6
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
7
8
/**
9
 * Source for Map block CSV.
10
 *
11
 * @MigrateSource(
12
 *   id = "map_block"
13
 * )
14
 */
15
class MapBlock extends CSV {
16
17
  public function prepareRow(Row $row) {
18
    // Prefix the state per the normal Address schema.
19
    if ($state = $row->getSourceProperty('Address State')) {
20
      $row->setSourceProperty('Address State', 'US-' . $state);
21
    }
22
23
    // Use a cached geolocation if possible.
24
    if ($geolocation = $row->getSourceProperty('Geolocation')) {
25
      list($lat, $lon) = explode(',', $geolocation);
26
      $point = [$lon, $lat];
27
      $row->setSourceProperty('Geofield', \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point));
28
    }
29
    else {
30
      // Manually geocode from source information.
31
      $address = $row->getSourceProperty('Address Line 1');
32
      $address2 = $row->getSourceProperty('Address Line 2');
33
      $city = $row->getSourceProperty('Address City');
34
      $zip = $row->getSourceProperty('Address Zip');
35
36
      if ($address && $city && $zip && $state) {
37
        $address2 = $address2 ? $address2 . "\n" : '';
38
        $address_string = "$address\n$address2$city, $state $zip\nUS";
39
        if ($collection = \Drupal::service('geocoder')->geocode($address_string, ['googlemaps'])) {
40
          // Set our value in a similar way to Geofield's LatLon Widget.
41
          // @see \Drupal\geofield\Plugin\Field\FieldWidget\GeofieldLatLonWidget::massageFormValues()
42
          $coordinates = $collection->first()->getCoordinates();
43
          $point = [
44
            $coordinates->getLongitude(),
45
            $coordinates->getLatitude()
46
          ];
47
          $row->setSourceProperty('Geofield', \Drupal::service('geofield.wkt_generator')
48
            ->WktBuildPoint($point));
49
        }
50
      }
51
    }
52
  }
53
54
}
55