src/InvoiceBundle/Listener/Mailer/InvoiceSubjectListener.php line 28

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. class InvoiceSubjectListener implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private readonly SystemConfig $config
  20.     ) {
  21.     }
  22.     public function __invoke(MessageEvent $event): void
  23.     {
  24.         $message $event->getMessage();
  25.         if ($message instanceof InvoiceEmail && null === $message->getSubject()) {
  26.             $message->subject(\str_replace('{id}', (string) $message->getInvoice()->getInvoiceId(), $this->config->get('invoice/email_subject')));
  27.         }
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             MessageEvent::class => '__invoke',
  33.         ];
  34.     }
  35. }