Skip to content

Commit b93c456

Browse files
committed
Refactor creating pdf files and sending emails logic
1 parent efd462a commit b93c456

File tree

14 files changed

+95
-31
lines changed

14 files changed

+95
-31
lines changed

config/doctrine/Invoice.orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<field name="localeCode" column="locale_code" />
1111
<field name="total" column="total" type="integer" />
1212
<field name="paymentState" column="payment_state" />
13+
<field name="path" column="path" />
1314

1415
<one-to-one field="billingData" target-entity="Sylius\InvoicingPlugin\Entity\BillingDataInterface">
1516
<cascade>

config/services.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<argument type="service" id="sylius_invoicing.repository.invoice" />
3838
<argument type="service" id="sylius.repository.order" />
3939
<argument type="service" id="sylius_invoicing.email.invoice_email_sender" />
40+
<argument type="service" id="sylius_invoicing.generator.invoice_pdf_file" />
41+
<argument type="service" id="sylius_invoicing.manager.invoice_file" />
42+
<argument type="service" id="gaufrette.sylius_invoicing_invoice_filesystem" />
4043
<tag name="messenger.message_handler" />
4144
</service>
4245

@@ -51,6 +54,7 @@
5154
<service id="sylius_invoicing.custom_factory.invoice" class="Sylius\InvoicingPlugin\Factory\InvoiceFactory">
5255
<argument>%sylius_invoicing.model.invoice.class%</argument>
5356
<argument type="service" id="sylius_invoicing.factory.shop_billing_data" />
57+
<argument type="service" id="sylius_invoicing.generator.invoice_file_name" />
5458
</service>
5559
<service id="Sylius\InvoicingPlugin\Factory\InvoiceFactoryInterface" alias="sylius_invoicing.custom_factory.invoice" />
5660

@@ -60,7 +64,6 @@
6064
<service id="Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface" alias="sylius_invoicing.manager.invoice_file" />
6165

6266
<service id="sylius_invoicing.provider.invoice_file" class="Sylius\InvoicingPlugin\Provider\InvoiceFileProvider">
63-
<argument type="service" id="sylius_invoicing.generator.invoice_file_name" />
6467
<argument type="service" id="gaufrette.sylius_invoicing_invoice_filesystem" />
6568
<argument type="service" id="sylius_invoicing.generator.invoice_pdf_file" />
6669
<argument type="service" id="sylius_invoicing.manager.invoice_file" />

config/services/generators.xml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@
3939
</service>
4040
<service id="Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface" alias="sylius_invoicing.generator.invoice" />
4141

42-
<service
43-
id="sylius_invoicing.generator.invoice_file_name"
44-
class="Sylius\InvoicingPlugin\Generator\InvoiceFileNameGenerator"
45-
/>
42+
<service id="sylius_invoicing.generator.invoice_file_name" class="Sylius\InvoicingPlugin\Generator\InvoiceFileNameGenerator">
43+
<argument>%sylius_invoicing.sequence_scope%</argument>
44+
</service>
4645
<service id="Sylius\InvoicingPlugin\Generator\InvoiceFileNameGeneratorInterface" alias="sylius_invoicing.generator.invoice_file_name" />
4746

4847
<service id="sylius_invoicing.generator.invoice_pdf_file" class="Sylius\InvoicingPlugin\Generator\InvoicePdfFileGenerator">
4948
<argument type="service" id="sylius_invoicing.generator.twig_to_pdf" />
5049
<argument type="service" id="file_locator" />
51-
<argument type="service" id="sylius_invoicing.generator.invoice_file_name" />
5250
<argument>@SyliusInvoicingPlugin/shared/download/pdf.html.twig</argument>
5351
<argument>%sylius_invoicing.template.logo_file%</argument>
5452
</service>
@@ -58,8 +56,6 @@
5856
<argument type="service" id="sylius_invoicing.repository.invoice" />
5957
<argument type="service" id="sylius.repository.order" />
6058
<argument type="service" id="sylius_invoicing.generator.invoice" />
61-
<argument type="service" id="sylius_invoicing.generator.invoice_pdf_file" />
62-
<argument type="service" id="sylius_invoicing.manager.invoice_file" />
6359
<argument>%sylius_invoicing.pdf_generator.enabled%</argument>
6460
</service>
6561
<service id="Sylius\InvoicingPlugin\Creator\InvoiceCreatorInterface" alias="sylius_invoicing.creator.invoice" />

src/CommandHandler/SendInvoiceEmailHandler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@
1313

1414
namespace Sylius\InvoicingPlugin\CommandHandler;
1515

16+
use Gaufrette\Exception\FileNotFound;
17+
use Gaufrette\FilesystemInterface;
1618
use Sylius\Component\Core\Model\OrderInterface;
1719
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
1820
use Sylius\InvoicingPlugin\Command\SendInvoiceEmail;
1921
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
2022
use Sylius\InvoicingPlugin\Email\InvoiceEmailSenderInterface;
2123
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
24+
use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface;
25+
use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface;
2226

2327
final 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
}

src/Creator/InvoiceCreator.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,19 @@
1313

1414
namespace Sylius\InvoicingPlugin\Creator;
1515

16-
use Doctrine\ORM\Exception\ORMException;
1716
use Sylius\Component\Core\Model\OrderInterface;
1817
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
1918
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
2019
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
2120
use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated;
2221
use Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface;
23-
use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface;
24-
use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface;
2522

2623
final class InvoiceCreator implements InvoiceCreatorInterface
2724
{
2825
public function __construct(
2926
private readonly InvoiceRepositoryInterface $invoiceRepository,
3027
private readonly OrderRepositoryInterface $orderRepository,
3128
private readonly InvoiceGeneratorInterface $invoiceGenerator,
32-
private readonly InvoicePdfFileGeneratorInterface $invoicePdfFileGenerator,
33-
private readonly InvoiceFileManagerInterface $invoiceFileManager,
3429
private readonly bool $hasEnabledPdfFileGenerator = true,
3530
) {
3631
}
@@ -55,13 +50,6 @@ public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): voi
5550
return;
5651
}
5752

58-
$invoicePdf = $this->invoicePdfFileGenerator->generate($invoice);
59-
$this->invoiceFileManager->save($invoicePdf);
60-
61-
try {
62-
$this->invoiceRepository->add($invoice);
63-
} catch (ORMException) {
64-
$this->invoiceFileManager->remove($invoicePdf);
65-
}
53+
$this->invoiceRepository->add($invoice);
6654
}
6755
}

src/Entity/Invoice.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct(
3636
protected ChannelInterface $channel,
3737
protected string $paymentState,
3838
protected InvoiceShopBillingDataInterface $shopBillingData,
39+
protected string $path,
3940
) {
4041
$this->issuedAt = clone $issuedAt;
4142

@@ -143,4 +144,9 @@ public function paymentState(): string
143144
{
144145
return $this->paymentState;
145146
}
147+
148+
public function path(): string
149+
{
150+
return $this->path;
151+
}
146152
}

src/Entity/InvoiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ public function channel(): ChannelInterface;
5353
public function shopBillingData(): InvoiceShopBillingDataInterface;
5454

5555
public function paymentState(): string;
56+
57+
public function path(): string;
5658
}

src/Factory/InvoiceFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Sylius\InvoicingPlugin\Entity\BillingDataInterface;
2121
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
2222
use Sylius\InvoicingPlugin\Entity\InvoiceShopBillingDataInterface;
23+
use Sylius\InvoicingPlugin\Generator\InvoiceFileNameGenerator;
2324
use Webmozart\Assert\Assert;
2425

2526
final class InvoiceFactory implements InvoiceFactoryInterface
@@ -30,6 +31,7 @@ final class InvoiceFactory implements InvoiceFactoryInterface
3031
public function __construct(
3132
private readonly string $className,
3233
private readonly FactoryInterface $invoiceShopBillingDataFactory,
34+
private readonly InvoiceFileNameGenerator $invoiceFileNameGenerator,
3335
) {
3436
}
3537

@@ -48,6 +50,8 @@ public function createForData(
4850
string $paymentState,
4951
?InvoiceShopBillingDataInterface $shopBillingData = null,
5052
): InvoiceInterface {
53+
$fileName = $this->invoiceFileNameGenerator->generateForPdf($number);
54+
5155
/** @var InvoiceInterface $invoice */
5256
$invoice = new $this->className(
5357
$id,
@@ -63,6 +67,7 @@ public function createForData(
6367
$channel,
6468
$paymentState,
6569
$shopBillingData ?? $this->invoiceShopBillingDataFactory->createNew(),
70+
$fileName,
6671
);
6772

6873
Assert::isInstanceOf($invoice, InvoiceInterface::class);

src/Generator/InvoiceFileNameGenerator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@
1313

1414
namespace Sylius\InvoicingPlugin\Generator;
1515

16-
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
16+
use Sylius\InvoicingPlugin\Enum\InvoiceSequenceScopeEnum;
1717

1818
final class InvoiceFileNameGenerator implements InvoiceFileNameGeneratorInterface
1919
{
2020
private const PDF_FILE_EXTENSION = '.pdf';
21+
public function __construct(
22+
private readonly ?string $scope = null,
23+
) {
24+
}
2125

22-
public function generateForPdf(InvoiceInterface $invoice): string
26+
public function generateForPdf(string $invoiceNumber): string
2327
{
24-
return str_replace('/', '_', $invoice->number()) . self::PDF_FILE_EXTENSION;
28+
$scope = InvoiceSequenceScopeEnum::tryFrom($this->scope ?? '') ?? InvoiceSequenceScopeEnum::GLOBAL;
29+
$prefix = $scope->value . '/';
30+
31+
if ($scope === InvoiceSequenceScopeEnum::GLOBAL) {
32+
$prefix = '';
33+
}
34+
35+
$fileName = str_replace('/', '_', $invoiceNumber) . self::PDF_FILE_EXTENSION;
36+
37+
return $prefix . $fileName;
2538
}
2639
}

src/Generator/InvoiceFileNameGeneratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717

1818
interface InvoiceFileNameGeneratorInterface
1919
{
20-
public function generateForPdf(InvoiceInterface $invoice): string;
20+
public function generateForPdf(string $invoiceNumber): string;
2121
}

0 commit comments

Comments
 (0)