You are here

class InvoiceConfirmationMail in Commerce Invoice 8.2

Hierarchy

Expanded class hierarchy of InvoiceConfirmationMail

1 string reference to 'InvoiceConfirmationMail'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses InvoiceConfirmationMail
commerce_invoice.invoice_confirmation_mail in ./commerce_invoice.services.yml
Drupal\commerce_invoice\Mail\InvoiceConfirmationMail

File

src/Mail/InvoiceConfirmationMail.php, line 12

Namespace

Drupal\commerce_invoice\Mail
View source
class InvoiceConfirmationMail implements InvoiceConfirmationMailInterface {
  use StringTranslationTrait;

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

  /**
   * The invoice total summary.
   *
   * @var \Drupal\commerce_invoice\InvoiceTotalSummaryInterface
   */
  protected $invoiceTotalSummary;

  /**
   * The profile view builder.
   *
   * @var \Drupal\profile\ProfileViewBuilder
   */
  protected $profileViewBuilder;

  /**
   * The invoice file manager.
   *
   * @var \Drupal\commerce_invoice\InvoiceFileManagerInterface
   */
  protected $invoiceFileManager;

  /**
   * Constructs a new InvoiceConfirmationMail object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\MailHandlerInterface $mail_handler
   *   The mail handler.
   * @param \Drupal\commerce_invoice\InvoiceTotalSummaryInterface $invoice_total_summary
   *   The invoice total summary.
   * @param \Drupal\commerce_invoice\InvoiceFileManagerInterface $invoice_file_manager
   *   The invoice file manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MailHandlerInterface $mail_handler, InvoiceTotalSummaryInterface $invoice_total_summary, InvoiceFileManagerInterface $invoice_file_manager) {
    $this->mailHandler = $mail_handler;
    $this->invoiceTotalSummary = $invoice_total_summary;
    $this->profileViewBuilder = $entity_type_manager
      ->getViewBuilder('profile');
    $this->invoiceFileManager = $invoice_file_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function send(InvoiceInterface $invoice, $to = NULL, $bcc = NULL) {
    $to = isset($to) ? $to : $invoice
      ->getEmail();
    if (!$to) {

      // The email should not be empty.
      return FALSE;
    }
    $subject = $this
      ->t('Invoice #@number', [
      '@number' => $invoice
        ->getInvoiceNumber(),
    ]);
    $body = [
      '#theme' => 'commerce_invoice_confirmation',
      '#invoice_entity' => $invoice,
      '#totals' => $this->invoiceTotalSummary
        ->buildTotals($invoice),
    ];
    if ($billing_profile = $invoice
      ->getBillingProfile()) {
      $body['#billing_information'] = $this->profileViewBuilder
        ->view($billing_profile);
    }
    $params = [
      'id' => 'invoice_confirmation',
      'from' => $invoice
        ->getStore()
        ->getEmail(),
      'bcc' => $bcc,
      'invoice' => $invoice,
    ];
    $customer = $invoice
      ->getCustomer();
    if ($customer
      ->isAuthenticated()) {
      $params['langcode'] = $customer
        ->getPreferredLangcode();
    }
    $file = $this->invoiceFileManager
      ->getInvoiceFile($invoice);
    $attachment = [
      'filepath' => $file
        ->getFileUri(),
      'filename' => $file
        ->getFilename(),
      'filemime' => $file
        ->getMimeType(),
    ];
    $params['attachments'][] = $attachment;
    return $this->mailHandler
      ->sendMail($to, $subject, $body, $params);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvoiceConfirmationMail::$invoiceFileManager protected property The invoice file manager.
InvoiceConfirmationMail::$invoiceTotalSummary protected property The invoice total summary.
InvoiceConfirmationMail::$mailHandler protected property The mail handler.
InvoiceConfirmationMail::$profileViewBuilder protected property The profile view builder.
InvoiceConfirmationMail::send public function Sends the invoice confirmation email. Overrides InvoiceConfirmationMailInterface::send
InvoiceConfirmationMail::__construct public function Constructs a new InvoiceConfirmationMail 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.