src/QuoteBundle/Listener/Mailer/QuoteReceiverListener.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 <[email protected]>
  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\QuoteBundle\Listener\Mailer;
  12. use SolidInvoice\QuoteBundle\Email\QuoteEmail;
  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\QuoteBundle\Tests\Listener\Mailer\QuoteReceiverListenerTest
  19.  */
  20. class QuoteReceiverListener implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private readonly SystemConfig $config
  24.     ) {
  25.     }
  26.     public function __invoke(MessageEvent $event): void
  27.     {
  28.         /** @var QuoteEmail $message */
  29.         $message $event->getMessage();
  30.         if ($message instanceof QuoteEmail && [] === $message->getTo()) {
  31.             $quote $message->getQuote();
  32.             foreach ($quote->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('quote/bcc_address'))) {
  36.                 $message->bcc($bcc);
  37.             }
  38.         }
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             MessageEvent::class => '__invoke',
  44.         ];
  45.     }
  46. }