You are here

class EmailUserEvaluator in Easy Email 2.0.x

Same name and namespace in other branches
  1. 8 src/Service/EmailUserEvaluator.php \Drupal\easy_email\Service\EmailUserEvaluator

Hierarchy

Expanded class hierarchy of EmailUserEvaluator

1 string reference to 'EmailUserEvaluator'
easy_email.services.yml in ./easy_email.services.yml
easy_email.services.yml
1 service uses EmailUserEvaluator
easy_email.user_evaluator in ./easy_email.services.yml
Drupal\easy_email\Service\EmailUserEvaluator

File

src/Service/EmailUserEvaluator.php, line 11

Namespace

Drupal\easy_email\Service
View source
class EmailUserEvaluator implements EmailUserEvaluatorInterface {

  /**
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * EmailUserEvaluator constructor.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EventDispatcherInterface $eventDispatcher, EntityTypeManagerInterface $entityTypeManager) {
    $this->eventDispatcher = $eventDispatcher;
    $this->entityTypeManager = $entityTypeManager;
    $this->userStorage = $entityTypeManager
      ->getStorage('user');
  }

  /**
   * @inheritDoc
   */
  public function evaluateUsers(EasyEmailInterface $email) {
    $this->eventDispatcher
      ->dispatch(EasyEmailEvents::EMAIL_PREUSEREVAL, new EasyEmailEvent($email));
    if ($email
      ->hasField('recipient_uid')) {
      $recipients = $email
        ->getRecipientAddresses();
      if (!empty($recipients)) {
        $results = $this->userStorage
          ->getQuery()
          ->condition('mail', $recipients, 'IN')
          ->execute();
        if (!empty($results)) {
          $email
            ->setRecipientIds(array_keys($results));
        }
      }
    }
    if ($email
      ->hasField('cc_uid')) {
      $cc = $email
        ->getCCAddresses();
      if (!empty($cc)) {
        $results = $this->userStorage
          ->getQuery()
          ->condition('mail', $cc, 'IN')
          ->execute();
        if (!empty($results)) {
          $email
            ->setCCIds(array_keys($results));
        }
      }
    }
    if ($email
      ->hasField('bcc_uid')) {
      $bcc = $email
        ->getBCCAddresses();
      if (!empty($bcc)) {
        $results = $this->userStorage
          ->getQuery()
          ->condition('mail', $bcc, 'IN')
          ->execute();
        if (!empty($results)) {
          $email
            ->setBCCIds(array_keys($results));
        }
      }
    }
    $this->eventDispatcher
      ->dispatch(EasyEmailEvents::EMAIL_USEREVAL, new EasyEmailEvent($email));
  }

}

Members