Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom DateTimeProvider by Symfony Clock #335

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@

1. The deprecated method `Sylius\InvoicingPlugin\Entity\InvoiceInterface::orderNumber()` has been removed,
use `Sylius\InvoicingPlugin\Entity\InvoiceInterface::order()` instead.

1. The `Sylius\InvoicingPlugin\SystemDateTimeProvider` class, `Sylius\InvoicingPlugin\DateTimeProvider` interface
and corresponding `sylius_invoicing_plugin.date_time_provider` service have been removed.
It has been replaced by `clock` service and `Symfony\Component\Clock\ClockInterface` interface.

Affected classes:
- `Sylius\InvoicingPlugin\Creator\MassInvoicesCreator`
- `Sylius\InvoicingPlugin\EventProducer\OrderPaymentPaidProducer`
- `Sylius\InvoicingPlugin\EventProducer\OrderPlacedProducer`
- `Sylius\InvoicingPlugin\Generator\SequentialInvoiceNumberGenerator`
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"sylius/grid-bundle": "^1.9",
"sylius/resource-bundle": "^1.9",
"sylius/sylius": "~1.13.0 || ~1.14.0",
"symfony/clock": "^6.4",
"symfony/config": "^5.4.21 || ^6.4",
"symfony/dependency-injection": "^5.4.21 || ^6.4",
"symfony/form": "^5.4.21 || ^6.4",
Expand Down
10 changes: 5 additions & 5 deletions spec/Creator/MassInvoicesCreatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\Creator\InvoiceCreatorInterface;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Symfony\Component\Clock\ClockInterface;

final class MassInvoicesCreatorSpec extends ObjectBehavior
{
function let(
InvoiceCreatorInterface $invoiceCreator,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
): void {
$this->beConstructedWith($invoiceCreator, $dateTimeProvider);
$this->beConstructedWith($invoiceCreator, $clock);
}

function it_requests_invoices_creation_for_multiple_orders(
InvoiceCreatorInterface $invoiceCreator,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
OrderInterface $firstOrder,
OrderInterface $secondOrder,
OrderInterface $thirdOrder,
Expand All @@ -42,7 +42,7 @@ function it_requests_invoices_creation_for_multiple_orders(
$secondInvoiceDateTime = new \DateTimeImmutable('2019-02-25');
$thirdInvoiceDateTime = new \DateTimeImmutable('2019-02-25');

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

$invoiceCreator->__invoke('0000001', $firstInvoiceDateTime)->shouldBeCalled();
$invoiceCreator->__invoke('0000002', $secondInvoiceDateTime)->shouldBeCalled();
Expand Down
20 changes: 10 additions & 10 deletions spec/EventProducer/OrderPaymentPaidProducerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
use Prophecy\Argument;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Event\OrderPaymentPaid;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class OrderPaymentPaidProducerSpec extends ObjectBehavior
{
function let(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
InvoiceRepositoryInterface $invoiceRepository,
): void {
$this->beConstructedWith($eventBus, $dateTimeProvider, $invoiceRepository);
$this->beConstructedWith($eventBus, $clock, $invoiceRepository);
}

function it_dispatches_order_payment_paid_event_for_payment(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
PaymentInterface $payment,
OrderInterface $order,
InvoiceRepositoryInterface $invoiceRepository,
Expand All @@ -45,8 +45,8 @@ function it_dispatches_order_payment_paid_event_for_payment(
$payment->getOrder()->willReturn($order);
$order->getNumber()->willReturn('0000001');

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

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

Expand All @@ -59,21 +59,21 @@ function it_dispatches_order_payment_paid_event_for_payment(

function it_does_not_dispatch_event_when_payment_is_not_related_to_order(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
PaymentInterface $payment,
): void {
$payment->getOrder()->willReturn(null);

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

$dateTimeProvider->__invoke()->shouldNotBeCalled();
$clock->now()->shouldNotBeCalled();

$this->__invoke($payment);
}

function it_does_not_dispatch_event_when_there_is_no_invoice_related_to_order(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
PaymentInterface $payment,
OrderInterface $order,
InvoiceRepositoryInterface $invoiceRepository,
Expand All @@ -83,7 +83,7 @@ function it_does_not_dispatch_event_when_there_is_no_invoice_related_to_order(
$invoiceRepository->findOneByOrder($order)->willReturn(null);

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

$this->__invoke($payment);
}
Expand Down
18 changes: 9 additions & 9 deletions spec/EventProducer/OrderPlacedProducerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
use Prophecy\Argument;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Event\OrderPlaced;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class OrderPlacedProducerSpec extends ObjectBehavior
{
function let(MessageBusInterface $eventBus, DateTimeProvider $dateTimeProvider): void
function let(MessageBusInterface $eventBus, ClockInterface $clock): void
{
$this->beConstructedWith($eventBus, $dateTimeProvider);
$this->beConstructedWith($eventBus, $clock);
}

function it_dispatches_an_order_placed_event_for_persisted_order(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
OrderInterface $order,
EntityManagerInterface $entityManager,
): void {
$dateTime = new \DateTime('2018-12-14');
$dateTimeProvider->__invoke()->willReturn($dateTime);
$dateTime = new \DateTimeImmutable('2018-12-14');
$clock->now()->willReturn($dateTime);

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

function it_dispatches_an_order_placed_event_for_updated_order(
MessageBusInterface $eventBus,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
EntityManagerInterface $entityManager,
OrderInterface $order,
): void {
$dateTime = new \DateTime('2018-12-14');
$dateTimeProvider->__invoke()->willReturn($dateTime);
$dateTime = new \DateTimeImmutable('2018-12-14');
$clock->now()->willReturn($dateTime);

/** @var UnitOfWork|MockInterface $unitOfWork */
$unitOfWork = Mockery::mock(UnitOfWork::class);
Expand Down
20 changes: 9 additions & 11 deletions spec/Generator/SequentialInvoiceNumberGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Entity\InvoiceSequenceInterface;
use Sylius\InvoicingPlugin\Generator\InvoiceNumberGenerator;
use Symfony\Component\Clock\ClockInterface;

final class SequentialInvoiceNumberGeneratorSpec extends ObjectBehavior
{
function let(
RepositoryInterface $sequenceRepository,
FactoryInterface $sequenceFactory,
EntityManagerInterface $sequenceManager,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
): void {
$this->beConstructedWith(
$sequenceRepository,
$sequenceFactory,
$sequenceManager,
$dateTimeProvider,
$clock,
1,
9,
);
Expand All @@ -48,12 +48,11 @@ function it_implements_invoice_number_generator_interface(): void
function it_generates_invoice_number(
RepositoryInterface $sequenceRepository,
EntityManagerInterface $sequenceManager,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
InvoiceSequenceInterface $sequence,
): void {
$dateTime = new \DateTime('now');

$dateTimeProvider->__invoke()->willReturn($dateTime);
$dateTime = new \DateTimeImmutable('now');
$clock->now()->willReturn($dateTime);

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

Expand All @@ -71,12 +70,11 @@ function it_generates_invoice_number_when_sequence_is_null(
RepositoryInterface $sequenceRepository,
FactoryInterface $sequenceFactory,
EntityManagerInterface $sequenceManager,
DateTimeProvider $dateTimeProvider,
ClockInterface $clock,
InvoiceSequenceInterface $sequence,
): void {
$dateTime = new \DateTime('now');

$dateTimeProvider->__invoke()->willReturn($dateTime);
$dateTime = new \DateTimeImmutable('now');
$clock->now()->willReturn($dateTime);

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

Expand Down
6 changes: 3 additions & 3 deletions src/Creator/MassInvoicesCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
namespace Sylius\InvoicingPlugin\Creator;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated;
use Symfony\Component\Clock\ClockInterface;

final class MassInvoicesCreator implements MassInvoicesCreatorInterface
{
public function __construct(
private readonly InvoiceCreatorInterface $invoiceCreator,
private readonly DateTimeProvider $dateTimeProvider,
private readonly ClockInterface $clock,
) {
}

Expand All @@ -30,7 +30,7 @@ public function __invoke(array $orders): void
/** @var OrderInterface $order */
foreach ($orders as $order) {
try {
$this->invoiceCreator->__invoke($order->getNumber(), $this->dateTimeProvider->__invoke());
$this->invoiceCreator->__invoke($order->getNumber(), $this->clock->now());
} catch (InvoiceAlreadyGenerated) {
continue;
}
Expand Down
19 changes: 0 additions & 19 deletions src/DateTimeProvider.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/DependencyInjection/Compiler/SymfonyClockCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\InvoicingPlugin\DependencyInjection\Compiler;

use Symfony\Component\Clock\Clock;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

final class SymfonyClockCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->hasParameter('clock')) {
return;
}

$container->setDefinition('clock', new Definition(Clock::class));
}
}
6 changes: 3 additions & 3 deletions src/EventProducer/OrderPaymentPaidProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
use Sylius\InvoicingPlugin\Event\OrderPaymentPaid;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

final class OrderPaymentPaidProducer
{
public function __construct(
private readonly MessageBusInterface $eventBus,
private readonly DateTimeProvider $dateTimeProvider,
private readonly ClockInterface $clock,
private readonly InvoiceRepositoryInterface $invoiceRepository,
) {
}
Expand All @@ -42,7 +42,7 @@ public function __invoke(PaymentInterface $payment): void

$this->eventBus->dispatch(new OrderPaymentPaid(
$order->getNumber(),
$this->dateTimeProvider->__invoke(),
$this->clock->now(),
));
}

Expand Down
8 changes: 3 additions & 5 deletions src/EventProducer/OrderPlacedProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\InvoicingPlugin\DateTimeProvider;
use Sylius\InvoicingPlugin\Event\OrderPlaced;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface;

final class OrderPlacedProducer
{
public function __construct(
private readonly MessageBusInterface $eventBus,
private readonly DateTimeProvider $dateTimeProvider,
private readonly ClockInterface $clock,
) {
}

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

private function dispatchOrderPlacedEvent(OrderInterface $order): void
{
$this->eventBus->dispatch(
new OrderPlaced($order->getNumber(), $this->dateTimeProvider->__invoke()),
);
$this->eventBus->dispatch(new OrderPlaced($order->getNumber(), $this->clock->now()));
}
}
Loading