You are here

class ContentHasherEventSubscriber in Tome 8

Event subscriber that keeps the content hash table up to date.

@internal

Hierarchy

  • class \Drupal\tome_sync\EventSubscriber\ContentHasherEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ContentHasherEventSubscriber

1 string reference to 'ContentHasherEventSubscriber'
tome_sync.services.yml in modules/tome_sync/tome_sync.services.yml
modules/tome_sync/tome_sync.services.yml
1 service uses ContentHasherEventSubscriber
tome_sync.content_hasher_event_subscriber in modules/tome_sync/tome_sync.services.yml
Drupal\tome_sync\EventSubscriber\ContentHasherEventSubscriber

File

modules/tome_sync/src/EventSubscriber/ContentHasherEventSubscriber.php, line 17

Namespace

Drupal\tome_sync\EventSubscriber
View source
class ContentHasherEventSubscriber implements EventSubscriberInterface {

  /**
   * The content hasher.
   *
   * @var \Drupal\tome_sync\ContentHasherInterface
   */
  protected $contentHasher;

  /**
   * The target content storage.
   *
   * @var \Drupal\Core\Config\FileStorage
   */
  protected $contentStorage;

  /**
   * Creates a ContentHasherEventSubscriber object.
   *
   * @param \Drupal\tome_sync\ContentHasherInterface $content_hasher
   *   The content hasher.
   * @param \Drupal\Core\Config\FileStorage $content_storage
   *   The target content storage.
   */
  public function __construct(ContentHasherInterface $content_hasher, FileStorage $content_storage) {
    $this->contentHasher = $content_hasher;
    $this->contentStorage = $content_storage;
  }

  /**
   * Maintains a hash of imported content to support partial imports.
   *
   * @param \Drupal\tome_sync\Event\ContentCrudEvent $event
   *   The content CRUD event.
   */
  public function writeHash(ContentCrudEvent $event) {
    $entity = $event
      ->getContent();
    $content_name = TomeSyncHelper::getContentName($entity);
    $file_path = $this->contentStorage
      ->getFilePath($content_name);
    if (file_exists($file_path)) {
      $encoded_content = file_get_contents($file_path);
      $this->contentHasher
        ->writeHash($encoded_content, $content_name);
    }
  }

  /**
   * Maintains a hash of exported content to support partial imports.
   *
   * @param \Drupal\tome_sync\Event\ContentCrudEvent $event
   *   The content CRUD event.
   */
  public function writeSourceHash(ContentCrudEvent $event) {
    $entity = $event
      ->getContent();
    $content_name = TomeSyncHelper::getContentName($entity);
    $file_path = $this->contentStorage
      ->getFilePath($content_name);
    if (file_exists($file_path)) {
      $encoded_content = file_get_contents($file_path);
      $this->contentHasher
        ->writeHash($encoded_content, $content_name);
    }
  }

  /**
   * Maintains a hash of imported content to support partial imports.
   *
   * @param \Drupal\tome_sync\Event\ContentCrudEvent $event
   *   The content CRUD event.
   */
  public function deleteHash(ContentCrudEvent $event) {
    $entity = $event
      ->getContent();
    $content_name = TomeSyncHelper::getContentName($entity);
    $this->contentHasher
      ->deleteHash($content_name);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[TomeSyncEvents::IMPORT_CONTENT][] = [
      'writeHash',
    ];
    $events[TomeSyncEvents::EXPORT_CONTENT][] = [
      'writeSourceHash',
    ];
    $events[TomeSyncEvents::DELETE_CONTENT][] = [
      'deleteHash',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHasherEventSubscriber::$contentHasher protected property The content hasher.
ContentHasherEventSubscriber::$contentStorage protected property The target content storage.
ContentHasherEventSubscriber::deleteHash public function Maintains a hash of imported content to support partial imports.
ContentHasherEventSubscriber::getSubscribedEvents public static function
ContentHasherEventSubscriber::writeHash public function Maintains a hash of imported content to support partial imports.
ContentHasherEventSubscriber::writeSourceHash public function Maintains a hash of exported content to support partial imports.
ContentHasherEventSubscriber::__construct public function Creates a ContentHasherEventSubscriber object.