You are here

public function EmailSubscriber::onEvent in Commerce Email 8

Sends emails associated with the given event.

Parameters

\Symfony\Component\EventDispatcher\Event $event: The event.

string $event_name: The event name.

File

src/EventSubscriber/EmailSubscriber.php, line 64

Class

EmailSubscriber
Subscribes to Symfony events and maps them to email events.

Namespace

Drupal\commerce_email\EventSubscriber

Code

public function onEvent(Event $event, $event_name) {
  $email_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_email');

  /** @var \Drupal\commerce_email\Entity\EmailInterface[] $emails */
  $emails = $email_storage
    ->loadMultiple();
  foreach ($emails as $email) {
    $email_event = $email
      ->getEvent();
    if ($email_event
      ->getEventName() == $event_name) {
      $entity = $email_event
        ->extractEntityFromEvent($event);
      if ($email
        ->applies($entity)) {
        $this->emailSender
          ->send($email, $entity);
      }
    }
  }
}