Coverage index

src/Plugin/migrate/process/NodeCounterId.php

Lines: 89.47%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Plugin\migrate\process;
6
7
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\migrate\MigrateException;
10
use Drupal\migrate\MigrateExecutableInterface;
11
use Drupal\migrate\MigrateLookupInterface;
12
use Drupal\migrate\MigrateSkipRowException;
13
use Drupal\migrate\Plugin\MigrationInterface;
14
use Drupal\migrate\ProcessPluginBase;
15
use Drupal\migrate\Row;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
/**
19
 * Maps a Statistics node ID to the destination node ID.
20
 *
21
 * When the source configuration node_migrations is empty, the source ID is
22
 * returned. Otherwise the ID is looked up in those migrations. A node that
23
 * was not migrated is skipped.
24
 *
25
 * @MigrateProcessPlugin(
26
 *   id = "visitors_node_counter_id"
27
 * )
28
 */
29
class NodeCounterId extends ProcessPluginBase implements ContainerFactoryPluginInterface {
30
31
  /**
32
   * Constructs a NodeCounterId object.
33
   *
34
   * @param array $configuration
35
   *   A configuration array containing information about the plugin instance.
36
   * @param string $plugin_id
37
   *   The plugin ID for the plugin instance.
38
   * @param mixed $plugin_definition
39
   *   The plugin implementation definition.
40
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
41
   *   The migration.
42
   * @param \Drupal\migrate\MigrateLookupInterface $migrateLookup
43
   *   The migrate lookup service.
44
   */
45
  public function __construct(
46
    array $configuration,
47
    $plugin_id,
48
    $plugin_definition,
49
    protected MigrationInterface $migration,
50
    protected MigrateLookupInterface $migrateLookup,
51
  ) {
522
    parent::__construct($configuration, $plugin_id, $plugin_definition);
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ?MigrationInterface $migration = NULL) {
592
    return new static(
602
      $configuration,
612
      $plugin_id,
622
      $plugin_definition,
632
      $migration,
642
      $container->get('migrate.lookup'),
652
    );
66
  }
67
68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
722
    $migration_ids = $this->migration->getSourceConfiguration()['node_migrations'] ?? [];
732
    if ($migration_ids === [] || $migration_ids === '') {
741
      return (int) $value;
75
    }
76
771
    foreach ((array) $migration_ids as $migration_id) {
78
      try {
791
        $matches = $this->migrateLookup->lookup($migration_id, [$value]);
80
      }
810
      catch (PluginNotFoundException $e) {
820
        throw new MigrateException(sprintf('Node migration "%s" was not found.', $migration_id), 0, $e);
83
      }
841
      if ($matches) {
851
        $ids = array_values(reset($matches));
861
        return (int) reset($ids);
87
      }
88
    }
89
901
    throw new MigrateSkipRowException(sprintf('Node %s was not migrated.', $value));
91
  }
92
93
}