You are here

class ShipmentConfirmationMail in Commerce Shipping 8.2

Hierarchy

Expanded class hierarchy of ShipmentConfirmationMail

1 string reference to 'ShipmentConfirmationMail'
commerce_shipping.services.yml in ./commerce_shipping.services.yml
commerce_shipping.services.yml
1 service uses ShipmentConfirmationMail
commerce_shipping.shipment_confirmation_mail in ./commerce_shipping.services.yml
Drupal\commerce_shipping\Mail\ShipmentConfirmationMail

File

src/Mail/ShipmentConfirmationMail.php, line 10

Namespace

Drupal\commerce_shipping\Mail
View source
class ShipmentConfirmationMail implements ShipmentConfirmationMailInterface {
  use StringTranslationTrait;

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

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

  /**
   * Constructs a new ShipmentConfirmationMail object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\MailHandlerInterface $mail_handler
   *   The mail handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MailHandlerInterface $mail_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->mailHandler = $mail_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function send(ShipmentInterface $shipment, $to = NULL, $bcc = NULL) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $shipment
      ->getOrder();
    $to = isset($to) ? $to : $order
      ->getEmail();
    if (!$to) {

      // The email should not be empty.
      return FALSE;
    }
    $subject = $this
      ->formatPlural($shipment
      ->get('items')
      ->count(), 'An item for order #@number shipped!', 'Items for your order #@number shipped!', [
      '@number' => $order
        ->getOrderNumber(),
    ]);
    $profile_view_builder = $this->entityTypeManager
      ->getViewBuilder('profile');
    $shipment_view_builder = $this->entityTypeManager
      ->getViewBuilder('commerce_shipment');
    $body = [
      '#theme' => 'commerce_shipment_confirmation',
      '#order_entity' => $order,
      '#shipment_entity' => $shipment,
      '#shipping_profile' => $profile_view_builder
        ->view($shipment
        ->getShippingProfile()),
      '#tracking_code' => $shipment_view_builder
        ->viewField($shipment
        ->get('tracking_code'), 'default'),
    ];
    $params = [
      'id' => 'shipment_confirmation',
      'from' => $order
        ->getStore()
        ->getEmail(),
      'bcc' => $bcc,
      'order' => $order,
      'shipment' => $shipment,
    ];
    $customer = $order
      ->getCustomer();
    if ($customer
      ->isAuthenticated()) {
      $params['langcode'] = $customer
        ->getPreferredLangcode();
    }
    return $this->mailHandler
      ->sendMail($to, $subject, $body, $params);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShipmentConfirmationMail::$entityTypeManager protected property The entity type manager.
ShipmentConfirmationMail::$mailHandler protected property The mail handler.
ShipmentConfirmationMail::send public function Sends the shipment confirmation email. Overrides ShipmentConfirmationMailInterface::send
ShipmentConfirmationMail::__construct public function Constructs a new ShipmentConfirmationMail object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.