You are here

RegistrationStorage.php in RNG - Events and Registrations 3.x

Same filename and directory in other branches
  1. 8.2 src/RegistrationStorage.php

Namespace

Drupal\rng

File

src/RegistrationStorage.php
View source
<?php

namespace Drupal\rng;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\rng\Event\RegistrationEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Defines the registration storage.
 */
class RegistrationStorage extends SqlContentEntityStorage {

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   *
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    $instance = parent::createInstance($container, $entity_type);
    $instance
      ->setDispatcher($container
      ->get('event_dispatcher'));
    return $instance;
  }

  /**
   * Set the event dispatcher.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   */
  public function setDispatcher(EventDispatcherInterface $event_dispatcher) {
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  protected function invokeHook($hook, EntityInterface $entity) {
    parent::invokeHook($hook, $entity);
    $this->eventDispatcher
      ->dispatch($this
      ->getEventName($hook), new RegistrationEvent($entity));
  }

  /**
   * Gets the event name for the given hook.
   *
   * Created using the the entity type's module name and ID.
   * For example, the 'presave' hook for registration entities maps
   * to the 'rng.registration.presave' event name.
   *
   * @param string $hook
   *   One of 'load', 'create', 'presave', 'insert', 'update', 'predelete',
   *   'delete', 'translation_insert', 'translation_delete'.
   *
   * @return string
   *   The event name.
   */
  protected function getEventName($hook) {
    return $this->entityType
      ->getProvider() . '.' . $this->entityType
      ->id() . '.' . $hook;
  }

}

Classes

Namesort descending Description
RegistrationStorage Defines the registration storage.