Lines: 89.47%
| Line | Hits | Source |
|---|---|---|
| 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 | ) { | |
| 52 | 2 | 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) { | |
| 59 | 2 | return new static( |
| 60 | 2 | $configuration, |
| 61 | 2 | $plugin_id, |
| 62 | 2 | $plugin_definition, |
| 63 | 2 | $migration, |
| 64 | 2 | $container->get('migrate.lookup'), |
| 65 | 2 | ); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * {@inheritdoc} | |
| 70 | */ | |
| 71 | public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
| 72 | 2 | $migration_ids = $this->migration->getSourceConfiguration()['node_migrations'] ?? []; |
| 73 | 2 | if ($migration_ids === [] || $migration_ids === '') { |
| 74 | 1 | return (int) $value; |
| 75 | } | |
| 76 | ||
| 77 | 1 | foreach ((array) $migration_ids as $migration_id) { |
| 78 | try { | |
| 79 | 1 | $matches = $this->migrateLookup->lookup($migration_id, [$value]); |
| 80 | } | |
| 81 | 0 | catch (PluginNotFoundException $e) { |
| 82 | 0 | throw new MigrateException(sprintf('Node migration "%s" was not found.', $migration_id), 0, $e); |
| 83 | } | |
| 84 | 1 | if ($matches) { |
| 85 | 1 | $ids = array_values(reset($matches)); |
| 86 | 1 | return (int) reset($ids); |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | 1 | throw new MigrateSkipRowException(sprintf('Node %s was not migrated.', $value)); |
| 91 | } | |
| 92 | ||
| 93 | } |