1313
1414namespace Sylius \InvoicingPlugin \CommandHandler ;
1515
16+ use Gaufrette \Exception \FileNotFound ;
17+ use Gaufrette \FilesystemInterface ;
1618use Sylius \Component \Core \Model \OrderInterface ;
1719use Sylius \Component \Core \Repository \OrderRepositoryInterface ;
1820use Sylius \InvoicingPlugin \Command \SendInvoiceEmail ;
1921use Sylius \InvoicingPlugin \Doctrine \ORM \InvoiceRepositoryInterface ;
2022use Sylius \InvoicingPlugin \Email \InvoiceEmailSenderInterface ;
2123use Sylius \InvoicingPlugin \Entity \InvoiceInterface ;
24+ use Sylius \InvoicingPlugin \Generator \InvoicePdfFileGeneratorInterface ;
25+ use Sylius \InvoicingPlugin \Manager \InvoiceFileManagerInterface ;
2226
2327final class SendInvoiceEmailHandler
2428{
2529 public function __construct (
2630 private readonly InvoiceRepositoryInterface $ invoiceRepository ,
2731 private readonly OrderRepositoryInterface $ orderRepository ,
2832 private readonly InvoiceEmailSenderInterface $ emailSender ,
33+ private readonly InvoicePdfFileGeneratorInterface $ invoicePdfFileGenerator ,
34+ private readonly InvoiceFileManagerInterface $ invoiceFileManager ,
35+ private readonly FilesystemInterface $ filesystem ,
2936 ) {
3037 }
3138
@@ -48,6 +55,15 @@ public function __invoke(SendInvoiceEmail $command): void
4855 return ;
4956 }
5057
58+ $ invoiceFileName = $ invoice ->path ();
59+
60+ try {
61+ $ this ->filesystem ->get ($ invoiceFileName );
62+ } catch (FileNotFound ) {
63+ $ invoicePdf = $ this ->invoicePdfFileGenerator ->generate ($ invoice );
64+ $ this ->invoiceFileManager ->save ($ invoicePdf );
65+ }
66+
5167 $ this ->emailSender ->sendInvoiceEmail ($ invoice , $ customer ->getEmail ());
5268 }
5369}
0 commit comments