You are here

class RngRequestSubscriber in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/RngRequestSubscriber.php \Drupal\rng\EventSubscriber\RngRequestSubscriber
  2. 3.x src/EventSubscriber/RngRequestSubscriber.php \Drupal\rng\EventSubscriber\RngRequestSubscriber

Runs tasks for RNG related to the request.

Hierarchy

  • class \Drupal\rng\EventSubscriber\RngRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RngRequestSubscriber

1 string reference to 'RngRequestSubscriber'
rng.services.yml in ./rng.services.yml
rng.services.yml
1 service uses RngRequestSubscriber
rng.request_subscriber in ./rng.services.yml
Drupal\rng\EventSubscriber\RngRequestSubscriber

File

src/EventSubscriber/RngRequestSubscriber.php, line 15

Namespace

Drupal\rng\EventSubscriber
View source
class RngRequestSubscriber implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The RNG entity model manager.
   *
   * @var \Drupal\rng\RngEntityModelInterface
   */
  protected $rngEntityModel;

  /**
   * The RNG event manager.
   *
   * @var \Drupal\rng\EventManagerInterface
   */
  protected $eventManager;

  /**
   * Constructs a new RngRequestSubscriber.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\rng\RngEntityModelInterface
   *   The RNG entity model manager.
   * @param \Drupal\rng\EventManagerInterface $event_manager
   *   The RNG event manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RngEntityModelInterface $rng_entity_model, EventManagerInterface $event_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->rngEntityModel = $rng_entity_model;
    $this->eventManager = $event_manager;
  }

  /**
   * Run RNG rules for entity operations which occurred during this request.
   *
   * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
   *   The event to process.
   */
  public function onKernelTerminate(PostResponseEvent $event) {
    $operation_records = $this->rngEntityModel
      ->getOperationRecords();
    foreach ($operation_records as $operation_record) {
      if ($operation_record
        ->getEntityTypeId() == 'registration') {

        /** @var \Drupal\rng\RegistrationInterface $registration */
        $registration = $this->entityTypeManager
          ->getStorage('registration')
          ->load($operation_record
          ->getEntityId());
        if (!$registration) {

          // Registration no longer exists, need full object to act on.
          // @todo: if entity is about to be deleted, then all existing
          // operation records should be processed in preDelete.
          continue;
        }
        switch ($operation_record
          ->getOperation()) {
          case 'insert':
            $trigger_id = 'entity:registration:new';
            break;
          case 'update':
            $trigger_id = 'entity:registration:update';
            break;
        }
        if (isset($trigger_id)) {
          $event_meta = $this->eventManager
            ->getMeta($registration
            ->getEvent());
          $event_meta
            ->trigger($trigger_id, [
            'registrations' => [
              $registration,
            ],
          ]);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Should go before other subscribers start to write their caches. Notably
    // before \Drupal\Core\EventSubscriber\KernelDestructionSubscriber to
    // prevent instantiation of destructed services.
    $events[KernelEvents::TERMINATE][] = [
      'onKernelTerminate',
      300,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RngRequestSubscriber::$entityTypeManager protected property The entity type manager.
RngRequestSubscriber::$eventManager protected property The RNG event manager.
RngRequestSubscriber::$rngEntityModel protected property The RNG entity model manager.
RngRequestSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RngRequestSubscriber::onKernelTerminate public function Run RNG rules for entity operations which occurred during this request.
RngRequestSubscriber::__construct public function Constructs a new RngRequestSubscriber.