You are here

class EmailSender in Commerce Email 8

Hierarchy

Expanded class hierarchy of EmailSender

1 string reference to 'EmailSender'
commerce_email.services.yml in ./commerce_email.services.yml
commerce_email.services.yml
1 service uses EmailSender
commerce_email.email_sender in ./commerce_email.services.yml
Drupal\commerce_email\EmailSender

File

src/EmailSender.php, line 10

Namespace

Drupal\commerce_email
View source
class EmailSender implements EmailSenderInterface {

  /**
   * The mail handler.
   *
   * @var \Drupal\commerce\MailHandlerInterface
   */
  protected $mailHandler;

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * Constructs a new EmailSender object.
   *
   * @param \Drupal\commerce\MailHandlerInterface $mail_handler
   *   The mail handler.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   */
  public function __construct(MailHandlerInterface $mail_handler, Token $token) {
    $this->mailHandler = $mail_handler;
    $this->token = $token;
  }

  /**
   * {@inheritDoc}
   */
  public function send(EmailInterface $email, ContentEntityInterface $entity) {
    $entity_type_id = $entity
      ->getEntityTypeId();
    $event_entity_type_id = $email
      ->getEvent()
      ->getEntityTypeId();
    if ($entity_type_id != $event_entity_type_id) {
      throw new \InvalidArgumentException(sprintf('The email requires a "%s" entity, but a "%s" entity was given.', $event_entity_type_id, $entity_type_id));
    }
    $short_entity_type_id = str_replace('commerce_', '', $entity_type_id);
    $to = $this
      ->replaceTokens($email
      ->getTo(), $entity);
    $subject = $this
      ->replaceTokens($email
      ->getSubject(), $entity);
    $body = [
      '#type' => 'inline_template',
      '#template' => $this
        ->replaceTokens($email
        ->getBody(), $entity),
      '#context' => [
        $short_entity_type_id => $entity,
      ],
    ];

    // @todo Figure out how to get the langcode generically.
    $params = [
      'id' => 'commerce_email',
      'from' => $this
        ->replaceTokens($email
        ->getFrom(), $entity),
      'cc' => $this
        ->replaceTokens($email
        ->getCc(), $entity),
      'bcc' => $this
        ->replaceTokens($email
        ->getBcc(), $entity),
    ];
    return $this->mailHandler
      ->sendMail($to, $subject, $body, $params);
  }

  /**
   * Replaces tokens in the given value.
   *
   * @param string $value
   *   The value.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to use for token replacements.
   *
   * @return string
   *   The value with tokens replaced.
   */
  protected function replaceTokens($value, ContentEntityInterface $entity) {
    if (!empty($value)) {
      $value = $this->token
        ->replace($value, [
        $entity
          ->getEntityTypeId() => $entity,
      ]);
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailSender::$mailHandler protected property The mail handler.
EmailSender::$token protected property The token service.
EmailSender::replaceTokens protected function Replaces tokens in the given value.
EmailSender::send public function Sends the given email. Overrides EmailSenderInterface::send
EmailSender::__construct public function Constructs a new EmailSender object.