src/InvoiceBundle/Listener/Mailer/InvoiceReceiverListener.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <open-source@solidworx.co>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\InvoiceBundle\Listener\Mailer;
  12. use SolidInvoice\InvoiceBundle\Email\InvoiceEmail;
  13. use SolidInvoice\SettingsBundle\SystemConfig;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Mailer\Event\MessageEvent;
  16. use Symfony\Component\Mime\Address;
  17. /**
  18.  * @see \SolidInvoice\InvoiceBundle\Tests\Listener\Mailer\InvoiceReceiverListenerTest
  19.  */
  20. class InvoiceReceiverListener implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private readonly SystemConfig $config
  24.     ) {
  25.     }
  26.     public function __invoke(MessageEvent $event): void
  27.     {
  28.         /** @var InvoiceEmail $message */
  29.         $message $event->getMessage();
  30.         if ($message instanceof InvoiceEmail && [] === $message->getTo()) {
  31.             $invoice $message->getInvoice();
  32.             foreach ($invoice->getUsers() as $user) {
  33.                 $message->addTo(new Address($user->getEmail(), trim(sprintf('%s %s'$user->getFirstName(), $user->getLastName()))));
  34.             }
  35.             if ('' !== ($bcc = (string) $this->config->get('invoice/bcc_address'))) {
  36.                 $message->addBcc($bcc);
  37.             }
  38.         }
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             MessageEvent::class => '__invoke',
  44.         ];
  45.     }
  46. }