You are here

class InvoiceConfirmationSubscriber in Commerce Invoice 8.2

Sends a confirmation email when an invoice is generated.

Hierarchy

Expanded class hierarchy of InvoiceConfirmationSubscriber

1 string reference to 'InvoiceConfirmationSubscriber'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses InvoiceConfirmationSubscriber
commerce_invoice.invoice_confirmation_subscriber in ./commerce_invoice.services.yml
Drupal\commerce_invoice\EventSubscriber\InvoiceConfirmationSubscriber

File

src/EventSubscriber/InvoiceConfirmationSubscriber.php, line 14

Namespace

Drupal\commerce_invoice\EventSubscriber
View source
class InvoiceConfirmationSubscriber implements EventSubscriberInterface, DestructableInterface {

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

  /**
   * The invoice confirmation mail.
   *
   * @var \Drupal\commerce_invoice\Mail\InvoiceConfirmationMailInterface
   */
  protected $invoiceConfirmationMail;

  /**
   * The invoice IDs for which we should send invoice confirmation emails.
   *
   * @var int[]
   */
  protected $invoicesList = [];

  /**
   * Constructs a new InvoiceConfirmationSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce_invoice\Mail\InvoiceConfirmationMailInterface $invoice_confirmation_mail
   *   The mail handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, InvoiceConfirmationMailInterface $invoice_confirmation_mail) {
    $this->entityTypeManager = $entity_type_manager;
    $this->invoiceConfirmationMail = $invoice_confirmation_mail;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'commerce_invoice.confirm.post_transition' => [
        'sendInvoiceConfirmation',
        -100,
      ],
    ];
  }

  /**
   * Sends an invoice confirmation email.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function sendInvoiceConfirmation(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
    $invoice = $event
      ->getEntity();
    $invoice_type_storage = $this->entityTypeManager
      ->getStorage('commerce_invoice_type');

    /** @var \Drupal\commerce_invoice\Entity\InvoiceTypeInterface $invoice_type */
    $invoice_type = $invoice_type_storage
      ->load($invoice
      ->bundle());

    // Don't send the invoice confirmation email right away because doing so
    // triggers an invoice save after generating the invoice file.
    if ($invoice_type
      ->shouldSendConfirmation()) {
      $this->invoicesList[$invoice
        ->id()] = $invoice
        ->id();
    }
  }

  /**
   * {@inheritdoc}
   */
  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());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvoiceConfirmationSubscriber::$entityTypeManager protected property The entity type manager.
InvoiceConfirmationSubscriber::$invoiceConfirmationMail protected property The invoice confirmation mail.
InvoiceConfirmationSubscriber::$invoicesList protected property The invoice IDs for which we should send invoice confirmation emails.
InvoiceConfirmationSubscriber::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
InvoiceConfirmationSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
InvoiceConfirmationSubscriber::sendInvoiceConfirmation public function Sends an invoice confirmation email.
InvoiceConfirmationSubscriber::__construct public function Constructs a new InvoiceConfirmationSubscriber object.