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.

df_tools_panelizer_scenarios_migration_finished()   C
last analyzed

Complexity

Conditions 12
Paths 19

Size

Total Lines 91
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 91
rs 6.9666
c 1
b 0
f 0
cc 12
nc 19
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Scenarios hooks implemented by the DF Tools Panelizer module.
6
 */
7
8
use Drupal\Core\Entity\RevisionLogInterface;
9
use Drupal\migrate\Plugin\Migration;
10
use Drupal\migrate_source_csv\CSVFileObject;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Implements hook_scenarios_migration_finished().
15
 */
16
function df_tools_panelizer_scenarios_migration_finished(Migration $migration) {
17
  // Initialize variables required to parse migration.
18
  /** @var Drupal\migrate_source_csv\Plugin\migrate\source\CSV $source */
19
  $source = $migration->getSourcePlugin();
20
21
  /** @var Drupal\migrate_source_csv\CSVFileObject $iterator */
22
  $iterator = $source->initializeIterator();
23
  $base_path = $iterator->getPath();
24
  $filename = $base_path . '/' . $iterator->getFilename();
25
26
  $destination = $migration->getDestinationPlugin();
27
  $entity_type = explode(':', $destination->getPluginId())[1];
28
29
  // We only support Nodes at this point.
30
  // @todo Support more types if necessary.
31
  if ($entity_type != 'node') {
32
    return;
33
  }
34
35
  $entity_storage = \Drupal::entityManager()->getStorage($entity_type);
0 ignored issues
show
Deprecated Code introduced by
The function Drupal::entityManager() has been deprecated: in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
  $entity_storage = /** @scrutinizer ignore-deprecated */ \Drupal::entityManager()->getStorage($entity_type);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
36
37
  // Determine the ID required to lookup entities in this migration.
38
  $id_map = [];
39
  $ids = array_keys($source->getIds());
40
  $processes = $migration->getProcess();
41
42
  foreach ($processes as $field => $process) {
43
    if (isset($process[0]['source']) && $process[0]['source'] == $ids[0] && strpos($field, '/') === FALSE) {
44
      $id_map = [$field, $ids[0]];
45
    }
46
  }
47
48
  // Grab the processed CSV file using existing CSV methods.
49
  $file = new CSVFileObject($filename);
50
  $file->setHeaderRowCount(1);
51
52
  $count = $file->count();
53
  $file->rewind();
54
  $iterator->rewind();
55
56
  if (!empty($id_map)) {
57
    for ($i = 0; $i < $count; ++$i) {
58
      // Get the current row.
59
      $source_row = $iterator->current();
60
61
      // Search for the Entity based on its identifier.
62
      $field = $id_map[0];
63
      $value = $source_row[$id_map[1]];
64
65
      $ids = \Drupal::entityQuery($entity_type)
66
        ->condition($field, $value)
67
        ->execute();
68
69
      if (!empty($ids)) {
70
        // Load the Entity, so we can grab field values and add the Panelizer display.
71
        $id = reset($ids);
0 ignored issues
show
Bug introduced by
It seems like $ids can also be of type integer; however, parameter $array of reset() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $id = reset(/** @scrutinizer ignore-type */ $ids);
Loading history...
72
73
        /** @var Drupal\Core\Entity\ContentEntityBase $entity */
74
        $entity = $entity_storage->load($id);
75
76
        // See if a Panelizer config file exists for this Node.
77
        $panels_file = $base_path . '/panelizer.panels_display.' . $entity_type . '.' . $entity->uuid() . '.yml';
78
79
        if (file_exists($panels_file) && isset($entity->panelizer)) {
80
          $yaml = file_get_contents($panels_file);
81
82
          $panels_display = Yaml::parse($yaml);
83
84
          // The storage ID isn't based on UUIDs, unfortunately. Hotswap it with current info.
85
          $panels_display['storage_id'] = $entity_type . ':' . $entity->id() . ':full:' . $entity->getRevisionId();
86
87
          // Set the Panels Display to match what's defined in YAML.
88
          $entity->panelizer->panels_display = $panels_display;
89
90
          // Mark that this is an overridden display.
91
          $entity->panelizer->default = NULL;
92
93
          // This is a required field in SQL with no default, but it always "full".
94
          $entity->panelizer->view_mode = 'full';
95
96
          if ($entity instanceof RevisionLogInterface) {
97
            $entity->setRevisionLogMessage(NULL);
98
          }
99
100
          $entity->save();
101
        }
102
      }
103
104
      // Move to the next row.
105
      $file->next();
106
      $iterator->next();
107
    }
108
  }
109
}
110