Skip to content

Commit 811177a

Browse files
authored
Merge pull request Sylius#335 from GSadee/replace-date-provider-by-clock
Replace custom DateTimeProvider by Symfony Clock
2 parents eaf01fd + 1cb7d09 commit 811177a

17 files changed

+101
-96
lines changed

UPGRADE.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77

88
1. The deprecated method `Sylius\InvoicingPlugin\Entity\InvoiceInterface::orderNumber()` has been removed,
99
use `Sylius\InvoicingPlugin\Entity\InvoiceInterface::order()` instead.
10+
11+
1. The `Sylius\InvoicingPlugin\SystemDateTimeProvider` class, `Sylius\InvoicingPlugin\DateTimeProvider` interface
12+
and corresponding `sylius_invoicing_plugin.date_time_provider` service have been removed.
13+
It has been replaced by `clock` service and `Symfony\Component\Clock\ClockInterface` interface.
14+
15+
Affected classes:
16+
- `Sylius\InvoicingPlugin\Creator\MassInvoicesCreator`
17+
- `Sylius\InvoicingPlugin\EventProducer\OrderPaymentPaidProducer`
18+
- `Sylius\InvoicingPlugin\EventProducer\OrderPlacedProducer`
19+
- `Sylius\InvoicingPlugin\Generator\SequentialInvoiceNumberGenerator`

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sylius/grid-bundle": "^1.9",
1212
"sylius/resource-bundle": "^1.9",
1313
"sylius/sylius": "~1.13.0 || ~1.14.0",
14+
"symfony/clock": "^6.4",
1415
"symfony/config": "^5.4.21 || ^6.4",
1516
"symfony/dependency-injection": "^5.4.21 || ^6.4",
1617
"symfony/form": "^5.4.21 || ^6.4",

spec/Creator/MassInvoicesCreatorSpec.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
use PhpSpec\ObjectBehavior;
1717
use Sylius\Component\Core\Model\OrderInterface;
1818
use Sylius\InvoicingPlugin\Creator\InvoiceCreatorInterface;
19-
use Sylius\InvoicingPlugin\DateTimeProvider;
19+
use Symfony\Component\Clock\ClockInterface;
2020

2121
final class MassInvoicesCreatorSpec extends ObjectBehavior
2222
{
2323
function let(
2424
InvoiceCreatorInterface $invoiceCreator,
25-
DateTimeProvider $dateTimeProvider,
25+
ClockInterface $clock,
2626
): void {
27-
$this->beConstructedWith($invoiceCreator, $dateTimeProvider);
27+
$this->beConstructedWith($invoiceCreator, $clock);
2828
}
2929

3030
function it_requests_invoices_creation_for_multiple_orders(
3131
InvoiceCreatorInterface $invoiceCreator,
32-
DateTimeProvider $dateTimeProvider,
32+
ClockInterface $clock,
3333
OrderInterface $firstOrder,
3434
OrderInterface $secondOrder,
3535
OrderInterface $thirdOrder,
@@ -42,7 +42,7 @@ function it_requests_invoices_creation_for_multiple_orders(
4242
$secondInvoiceDateTime = new \DateTimeImmutable('2019-02-25');
4343
$thirdInvoiceDateTime = new \DateTimeImmutable('2019-02-25');
4444

45-
$dateTimeProvider->__invoke()->willReturn($firstInvoiceDateTime, $secondInvoiceDateTime, $thirdInvoiceDateTime);
45+
$clock->now()->willReturn($firstInvoiceDateTime, $secondInvoiceDateTime, $thirdInvoiceDateTime);
4646

4747
$invoiceCreator->__invoke('0000001', $firstInvoiceDateTime)->shouldBeCalled();
4848
$invoiceCreator->__invoke('0000002', $secondInvoiceDateTime)->shouldBeCalled();

spec/EventProducer/OrderPaymentPaidProducerSpec.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
use Prophecy\Argument;
1818
use Sylius\Component\Core\Model\OrderInterface;
1919
use Sylius\Component\Core\Model\PaymentInterface;
20-
use Sylius\InvoicingPlugin\DateTimeProvider;
2120
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
2221
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
2322
use Sylius\InvoicingPlugin\Event\OrderPaymentPaid;
23+
use Symfony\Component\Clock\ClockInterface;
2424
use Symfony\Component\Messenger\Envelope;
2525
use Symfony\Component\Messenger\MessageBusInterface;
2626

2727
final class OrderPaymentPaidProducerSpec extends ObjectBehavior
2828
{
2929
function let(
3030
MessageBusInterface $eventBus,
31-
DateTimeProvider $dateTimeProvider,
31+
ClockInterface $clock,
3232
InvoiceRepositoryInterface $invoiceRepository,
3333
): void {
34-
$this->beConstructedWith($eventBus, $dateTimeProvider, $invoiceRepository);
34+
$this->beConstructedWith($eventBus, $clock, $invoiceRepository);
3535
}
3636

3737
function it_dispatches_order_payment_paid_event_for_payment(
3838
MessageBusInterface $eventBus,
39-
DateTimeProvider $dateTimeProvider,
39+
ClockInterface $clock,
4040
PaymentInterface $payment,
4141
OrderInterface $order,
4242
InvoiceRepositoryInterface $invoiceRepository,
@@ -45,8 +45,8 @@ function it_dispatches_order_payment_paid_event_for_payment(
4545
$payment->getOrder()->willReturn($order);
4646
$order->getNumber()->willReturn('0000001');
4747

48-
$dateTime = new \DateTime();
49-
$dateTimeProvider->__invoke()->willReturn($dateTime);
48+
$dateTime = new \DateTimeImmutable();
49+
$clock->now()->willReturn($dateTime);
5050

5151
$event = new OrderPaymentPaid('0000001', $dateTime);
5252

@@ -59,21 +59,21 @@ function it_dispatches_order_payment_paid_event_for_payment(
5959

6060
function it_does_not_dispatch_event_when_payment_is_not_related_to_order(
6161
MessageBusInterface $eventBus,
62-
DateTimeProvider $dateTimeProvider,
62+
ClockInterface $clock,
6363
PaymentInterface $payment,
6464
): void {
6565
$payment->getOrder()->willReturn(null);
6666

6767
$eventBus->dispatch(Argument::any())->shouldNotBeCalled();
6868

69-
$dateTimeProvider->__invoke()->shouldNotBeCalled();
69+
$clock->now()->shouldNotBeCalled();
7070

7171
$this->__invoke($payment);
7272
}
7373

7474
function it_does_not_dispatch_event_when_there_is_no_invoice_related_to_order(
7575
MessageBusInterface $eventBus,
76-
DateTimeProvider $dateTimeProvider,
76+
ClockInterface $clock,
7777
PaymentInterface $payment,
7878
OrderInterface $order,
7979
InvoiceRepositoryInterface $invoiceRepository,
@@ -83,7 +83,7 @@ function it_does_not_dispatch_event_when_there_is_no_invoice_related_to_order(
8383
$invoiceRepository->findOneByOrder($order)->willReturn(null);
8484

8585
$eventBus->dispatch(Argument::any())->shouldNotBeCalled();
86-
$dateTimeProvider->__invoke()->shouldNotBeCalled();
86+
$clock->now()->shouldNotBeCalled();
8787

8888
$this->__invoke($payment);
8989
}

spec/EventProducer/OrderPlacedProducerSpec.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@
2222
use Prophecy\Argument;
2323
use Sylius\Component\Core\Model\OrderInterface;
2424
use Sylius\Component\Core\OrderCheckoutStates;
25-
use Sylius\InvoicingPlugin\DateTimeProvider;
2625
use Sylius\InvoicingPlugin\Event\OrderPlaced;
26+
use Symfony\Component\Clock\ClockInterface;
2727
use Symfony\Component\Messenger\Envelope;
2828
use Symfony\Component\Messenger\MessageBusInterface;
2929

3030
final class OrderPlacedProducerSpec extends ObjectBehavior
3131
{
32-
function let(MessageBusInterface $eventBus, DateTimeProvider $dateTimeProvider): void
32+
function let(MessageBusInterface $eventBus, ClockInterface $clock): void
3333
{
34-
$this->beConstructedWith($eventBus, $dateTimeProvider);
34+
$this->beConstructedWith($eventBus, $clock);
3535
}
3636

3737
function it_dispatches_an_order_placed_event_for_persisted_order(
3838
MessageBusInterface $eventBus,
39-
DateTimeProvider $dateTimeProvider,
39+
ClockInterface $clock,
4040
OrderInterface $order,
4141
EntityManagerInterface $entityManager,
4242
): void {
43-
$dateTime = new \DateTime('2018-12-14');
44-
$dateTimeProvider->__invoke()->willReturn($dateTime);
43+
$dateTime = new \DateTimeImmutable('2018-12-14');
44+
$clock->now()->willReturn($dateTime);
4545

4646
$order->getNumber()->willReturn('000666');
4747
$order->getCheckoutState()->willReturn(OrderCheckoutStates::STATE_COMPLETED);
@@ -56,12 +56,12 @@ function it_dispatches_an_order_placed_event_for_persisted_order(
5656

5757
function it_dispatches_an_order_placed_event_for_updated_order(
5858
MessageBusInterface $eventBus,
59-
DateTimeProvider $dateTimeProvider,
59+
ClockInterface $clock,
6060
EntityManagerInterface $entityManager,
6161
OrderInterface $order,
6262
): void {
63-
$dateTime = new \DateTime('2018-12-14');
64-
$dateTimeProvider->__invoke()->willReturn($dateTime);
63+
$dateTime = new \DateTimeImmutable('2018-12-14');
64+
$clock->now()->willReturn($dateTime);
6565

6666
/** @var UnitOfWork|MockInterface $unitOfWork */
6767
$unitOfWork = Mockery::mock(UnitOfWork::class);

spec/Generator/SequentialInvoiceNumberGeneratorSpec.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
use PhpSpec\ObjectBehavior;
1919
use Sylius\Component\Resource\Factory\FactoryInterface;
2020
use Sylius\Component\Resource\Repository\RepositoryInterface;
21-
use Sylius\InvoicingPlugin\DateTimeProvider;
2221
use Sylius\InvoicingPlugin\Entity\InvoiceSequenceInterface;
2322
use Sylius\InvoicingPlugin\Generator\InvoiceNumberGenerator;
23+
use Symfony\Component\Clock\ClockInterface;
2424

2525
final class SequentialInvoiceNumberGeneratorSpec extends ObjectBehavior
2626
{
2727
function let(
2828
RepositoryInterface $sequenceRepository,
2929
FactoryInterface $sequenceFactory,
3030
EntityManagerInterface $sequenceManager,
31-
DateTimeProvider $dateTimeProvider,
31+
ClockInterface $clock,
3232
): void {
3333
$this->beConstructedWith(
3434
$sequenceRepository,
3535
$sequenceFactory,
3636
$sequenceManager,
37-
$dateTimeProvider,
37+
$clock,
3838
1,
3939
9,
4040
);
@@ -48,12 +48,11 @@ function it_implements_invoice_number_generator_interface(): void
4848
function it_generates_invoice_number(
4949
RepositoryInterface $sequenceRepository,
5050
EntityManagerInterface $sequenceManager,
51-
DateTimeProvider $dateTimeProvider,
51+
ClockInterface $clock,
5252
InvoiceSequenceInterface $sequence,
5353
): void {
54-
$dateTime = new \DateTime('now');
55-
56-
$dateTimeProvider->__invoke()->willReturn($dateTime);
54+
$dateTime = new \DateTimeImmutable('now');
55+
$clock->now()->willReturn($dateTime);
5756

5857
$sequenceRepository->findOneBy([])->willReturn($sequence);
5958

@@ -71,12 +70,11 @@ function it_generates_invoice_number_when_sequence_is_null(
7170
RepositoryInterface $sequenceRepository,
7271
FactoryInterface $sequenceFactory,
7372
EntityManagerInterface $sequenceManager,
74-
DateTimeProvider $dateTimeProvider,
73+
ClockInterface $clock,
7574
InvoiceSequenceInterface $sequence,
7675
): void {
77-
$dateTime = new \DateTime('now');
78-
79-
$dateTimeProvider->__invoke()->willReturn($dateTime);
76+
$dateTime = new \DateTimeImmutable('now');
77+
$clock->now()->willReturn($dateTime);
8078

8179
$sequenceRepository->findOneBy([])->willReturn(null);
8280

src/Creator/MassInvoicesCreator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
namespace Sylius\InvoicingPlugin\Creator;
1515

1616
use Sylius\Component\Core\Model\OrderInterface;
17-
use Sylius\InvoicingPlugin\DateTimeProvider;
1817
use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated;
18+
use Symfony\Component\Clock\ClockInterface;
1919

2020
final class MassInvoicesCreator implements MassInvoicesCreatorInterface
2121
{
2222
public function __construct(
2323
private readonly InvoiceCreatorInterface $invoiceCreator,
24-
private readonly DateTimeProvider $dateTimeProvider,
24+
private readonly ClockInterface $clock,
2525
) {
2626
}
2727

@@ -30,7 +30,7 @@ public function __invoke(array $orders): void
3030
/** @var OrderInterface $order */
3131
foreach ($orders as $order) {
3232
try {
33-
$this->invoiceCreator->__invoke($order->getNumber(), $this->dateTimeProvider->__invoke());
33+
$this->invoiceCreator->__invoke($order->getNumber(), $this->clock->now());
3434
} catch (InvoiceAlreadyGenerated) {
3535
continue;
3636
}

src/DateTimeProvider.php

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\DependencyInjection\Compiler;
15+
16+
use Symfony\Component\Clock\Clock;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
21+
final class SymfonyClockCompilerPass implements CompilerPassInterface
22+
{
23+
public function process(ContainerBuilder $container): void
24+
{
25+
if ($container->hasParameter('clock')) {
26+
return;
27+
}
28+
29+
$container->setDefinition('clock', new Definition(Clock::class));
30+
}
31+
}

src/EventProducer/OrderPaymentPaidProducer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515

1616
use Sylius\Component\Core\Model\OrderInterface;
1717
use Sylius\Component\Core\Model\PaymentInterface;
18-
use Sylius\InvoicingPlugin\DateTimeProvider;
1918
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
2019
use Sylius\InvoicingPlugin\Event\OrderPaymentPaid;
20+
use Symfony\Component\Clock\ClockInterface;
2121
use Symfony\Component\Messenger\MessageBusInterface;
2222
use Webmozart\Assert\Assert;
2323

2424
final class OrderPaymentPaidProducer
2525
{
2626
public function __construct(
2727
private readonly MessageBusInterface $eventBus,
28-
private readonly DateTimeProvider $dateTimeProvider,
28+
private readonly ClockInterface $clock,
2929
private readonly InvoiceRepositoryInterface $invoiceRepository,
3030
) {
3131
}
@@ -42,7 +42,7 @@ public function __invoke(PaymentInterface $payment): void
4242

4343
$this->eventBus->dispatch(new OrderPaymentPaid(
4444
$order->getNumber(),
45-
$this->dateTimeProvider->__invoke(),
45+
$this->clock->now(),
4646
));
4747
}
4848

src/EventProducer/OrderPlacedProducer.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
use Doctrine\ORM\Event\LifecycleEventArgs;
1717
use Sylius\Component\Core\Model\OrderInterface;
1818
use Sylius\Component\Core\OrderCheckoutStates;
19-
use Sylius\InvoicingPlugin\DateTimeProvider;
2019
use Sylius\InvoicingPlugin\Event\OrderPlaced;
20+
use Symfony\Component\Clock\ClockInterface;
2121
use Symfony\Component\Messenger\MessageBusInterface;
2222

2323
final class OrderPlacedProducer
2424
{
2525
public function __construct(
2626
private readonly MessageBusInterface $eventBus,
27-
private readonly DateTimeProvider $dateTimeProvider,
27+
private readonly ClockInterface $clock,
2828
) {
2929
}
3030

@@ -67,8 +67,6 @@ public function postUpdate(LifecycleEventArgs $event): void
6767

6868
private function dispatchOrderPlacedEvent(OrderInterface $order): void
6969
{
70-
$this->eventBus->dispatch(
71-
new OrderPlaced($order->getNumber(), $this->dateTimeProvider->__invoke()),
72-
);
70+
$this->eventBus->dispatch(new OrderPlaced($order->getNumber(), $this->clock->now()));
7371
}
7472
}

0 commit comments

Comments
 (0)