You are here

final class MediaMigrationUuidOracle in Media Migration 8

Media Migration's UUID oracle.

Predicts the UUID property of a media entity that does not yet exist.

Hierarchy

Expanded class hierarchy of MediaMigrationUuidOracle

1 file declares its use of MediaMigrationUuidOracle
MediaMigrateUuid.php in src/Plugin/migrate/process/MediaMigrateUuid.php
1 string reference to 'MediaMigrationUuidOracle'
media_migration.services.yml in ./media_migration.services.yml
media_migration.services.yml
1 service uses MediaMigrationUuidOracle
media_migration.media_uuid_oracle in ./media_migration.services.yml
Drupal\media_migration\MediaMigrationUuidOracle

File

src/MediaMigrationUuidOracle.php, line 15

Namespace

Drupal\media_migration
View source
final class MediaMigrationUuidOracle implements MediaMigrationUuidOracleInterface {

  /**
   * The database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The media entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $mediaStorage;

  /**
   * The UUID generator service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuidGenerator;

  /**
   * Constructs MediaMigrationUuidOracle.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   A database connection.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
   *   The UUID generator service.
   */
  public function __construct(Connection $database, EntityTypeManagerInterface $entity_type_manager, UuidInterface $uuid_generator) {
    $this->database = $database;
    $this->mediaStorage = $entity_type_manager
      ->getStorage('media');
    $this->uuidGenerator = $uuid_generator;
  }

  /**
   * {@inheritdoc}
   */
  public function getMediaUuid(int $source_id, bool $generate = TRUE) : ?string {

    // If the media entity already exist, return its UUID.
    if ($media = $this->mediaStorage
      ->load($source_id)) {
      return $media
        ->uuid();
    }

    // If the media does not exist, try to get its UUID from our prophecy table.
    if (!($uuid_prophecy = $this
      ->getMediaUuidProphecy($source_id)) && $generate) {
      $uuid_prophecy = $this
        ->setMediaProphecy($source_id);
    }
    return $uuid_prophecy;
  }

  /**
   * Returns the UUID prophecy if it exists.
   *
   * @param int $source_id
   *   The source media entity's identifier.
   *
   * @return string|null
   *   The UUID, or NULL if it does not exist at the moment.
   */
  private function getMediaUuidProphecy(int $source_id) : ?string {
    $results = $this->database
      ->select(MediaMigration::MEDIA_UUID_PROPHECY_TABLE, 'mupt')
      ->fields('mupt')
      ->condition('mupt.' . MediaMigration::MEDIA_UUID_PROPHECY_SOURCEID_COL, $source_id)
      ->execute()
      ->fetchAll();
    return isset($results[0]->{MediaMigration::MEDIA_UUID_PROPHECY_UUID_COL}) ? $results[0]->{MediaMigration::MEDIA_UUID_PROPHECY_UUID_COL} : NULL;
  }

  /**
   * Saves a UUID prophecy if it doesn't exist.
   *
   * @param int $source_id
   *   The source media entity's identifier.
   *
   * @return string
   *   The UUID to save.
   *
   * @throws \Exception
   */
  private function setMediaProphecy(int $source_id) {
    $uuid = $this->uuidGenerator
      ->generate();
    try {
      $this->database
        ->insert(MediaMigration::MEDIA_UUID_PROPHECY_TABLE)
        ->fields([
        MediaMigration::MEDIA_UUID_PROPHECY_SOURCEID_COL => $source_id,
        MediaMigration::MEDIA_UUID_PROPHECY_UUID_COL => $uuid,
      ])
        ->execute();
      return $uuid;
    } catch (DatabaseExceptionWrapper $e) {
      throw new \LogicException(sprintf('Cannot create prophecy for the media entity with source id %i', $source_id));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaMigrationUuidOracle::$database protected property The database service.
MediaMigrationUuidOracle::$mediaStorage protected property The media entity storage.
MediaMigrationUuidOracle::$uuidGenerator protected property The UUID generator service.
MediaMigrationUuidOracle::getMediaUuid public function Returns the UUID of a media entity based on its source ID. Overrides MediaMigrationUuidOracleInterface::getMediaUuid
MediaMigrationUuidOracle::getMediaUuidProphecy private function Returns the UUID prophecy if it exists.
MediaMigrationUuidOracle::setMediaProphecy private function Saves a UUID prophecy if it doesn't exist.
MediaMigrationUuidOracle::__construct public function Constructs MediaMigrationUuidOracle.