You are here

public function InvoiceConfirmationSubscriber::destruct in Commerce Invoice 8.2

Performs destruct operations.

Overrides DestructableInterface::destruct

File

src/EventSubscriber/InvoiceConfirmationSubscriber.php, line 82

Class

InvoiceConfirmationSubscriber
Sends a confirmation email when an invoice is generated.

Namespace

Drupal\commerce_invoice\EventSubscriber

Code

public function destruct() {
  if (empty($this->invoicesList)) {
    return;
  }
  $invoice_storage = $this->entityTypeManager
    ->getStorage('commerce_invoice');
  $invoice_type_storage = $this->entityTypeManager
    ->getStorage('commerce_invoice_type');

  /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface[] $invoices */
  $invoices = $invoice_storage
    ->loadMultiple($this->invoicesList);
  foreach ($invoices as $invoice) {

    // If the invoice is already paid, skip sending the email.
    if ($invoice
      ->isPaid()) {
      continue;
    }

    /** @var \Drupal\commerce_invoice\Entity\InvoiceTypeInterface $invoice_type */
    $invoice_type = $invoice_type_storage
      ->load($invoice
      ->bundle());
    $this->invoiceConfirmationMail
      ->send($invoice, $invoice
      ->getEmail(), $invoice_type
      ->getConfirmationBcc());
  }
}