Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / src / Audit / IdAuditor.php
diff --git a/web/core/modules/migrate/src/Audit/IdAuditor.php b/web/core/modules/migrate/src/Audit/IdAuditor.php
new file mode 100644 (file)
index 0000000..f482acd
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace Drupal\migrate\Audit;
+
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\migrate\Plugin\MigrationInterface;
+
+/**
+ * Audits migrations that create content entities in the destination system.
+ */
+class IdAuditor implements AuditorInterface {
+
+  use StringTranslationTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function audit(MigrationInterface $migration) {
+    // If the migration does not opt into auditing, it passes.
+    if (!$migration->isAuditable()) {
+      return AuditResult::pass($migration);
+    }
+
+    $interface = HighestIdInterface::class;
+
+    $destination = $migration->getDestinationPlugin();
+    if (!$destination instanceof HighestIdInterface) {
+      throw new AuditException($migration, "Destination does not implement $interface");
+    }
+
+    $id_map = $migration->getIdMap();
+    if (!$id_map instanceof HighestIdInterface) {
+      throw new AuditException($migration, "ID map does not implement $interface");
+    }
+
+    if ($destination->getHighestId() > $id_map->getHighestId()) {
+      return AuditResult::fail($migration, [
+        $this->t('The destination system contains data which was not created by a migration.'),
+      ]);
+    }
+    return AuditResult::pass($migration);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function auditMultiple(array $migrations) {
+    $conflicts = [];
+
+    foreach ($migrations as $migration) {
+      $migration_id = $migration->getPluginId();
+      $conflicts[$migration_id] = $this->audit($migration);
+    }
+    ksort($conflicts);
+    return $conflicts;
+  }
+
+}