Skip to content

Commit 8cd390f

Browse files
committed
Add support for invoice modifiers in InvoiceCreator
1 parent 48be817 commit 8cd390f

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

config/services/generators.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<argument type="service" id="sylius_invoicing.generator.invoice_pdf_file" />
6161
<argument type="service" id="sylius_invoicing.manager.invoice_file" />
6262
<argument>%sylius_invoicing.pdf_generator.enabled%</argument>
63+
<argument type="tagged_iterator" tag="sylius_invoicing.modifier.invoice" />
6364
</service>
6465
<service id="Sylius\InvoicingPlugin\Creator\InvoiceCreatorInterface" alias="sylius_invoicing.creator.invoice" />
6566

src/Creator/InvoiceCreator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface;
2323
use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface;
2424
use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface;
25+
use Sylius\InvoicingPlugin\Modifier\InvoiceModifierInterface;
2526

2627
final class InvoiceCreator implements InvoiceCreatorInterface
2728
{
@@ -32,7 +33,17 @@ public function __construct(
3233
private readonly InvoicePdfFileGeneratorInterface $invoicePdfFileGenerator,
3334
private readonly InvoiceFileManagerInterface $invoiceFileManager,
3435
private readonly bool $hasEnabledPdfFileGenerator = true,
36+
private readonly ?iterable $invoiceModifiers = null,
3537
) {
38+
if (null === $this->invoiceModifiers) {
39+
trigger_deprecation(
40+
'sylius/invoicing-plugin',
41+
'2.1',
42+
'Not passing a "%s" to "%s" is deprecated and will be required in Sylius Invoicing Plugin 3.0.',
43+
InvoiceModifierInterface::class,
44+
self::class,
45+
);
46+
}
3647
}
3748

3849
public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void
@@ -49,6 +60,12 @@ public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): voi
4960

5061
$invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime);
5162

63+
if (null !== $this->invoiceModifiers) {
64+
foreach ($this->invoiceModifiers as $modifier) {
65+
$invoice = $modifier->modify($invoice, $order);
66+
}
67+
}
68+
5269
if (!$this->hasEnabledPdfFileGenerator) {
5370
$this->invoiceRepository->add($invoice);
5471

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\InvoicingPlugin\Modifier;
15+
16+
use Sylius\Component\Core\Model\OrderInterface;
17+
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
18+
19+
interface InvoiceModifierInterface
20+
{
21+
public function modify(InvoiceInterface $invoice, OrderInterface $order): InvoiceInterface;
22+
}

0 commit comments

Comments
 (0)