Skip to content

Commit

Permalink
Merge pull request #6 from Setono/refactor-states
Browse files Browse the repository at this point in the history
Change naming from 'dispatching' to 'uploading'
  • Loading branch information
loevgaard authored Apr 10, 2024
2 parents e30e0da + cbffac2 commit 9bf834c
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

namespace Setono\SyliusShipmondoPlugin\Command;

use Setono\SyliusShipmondoPlugin\Dispatcher\OrderDispatcherInterface;
use Setono\SyliusShipmondoPlugin\Uploader\OrderUploaderInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'setono:sylius-shipmondo:dispatch-orders',
description: 'Dispatch orders to Shipmondo',
name: 'setono:sylius-shipmondo:upload-orders',
description: 'Upload orders to Shipmondo',
)]
final class DispatchOrdersCommand extends Command
final class UploadOrdersCommand extends Command
{
public function __construct(private readonly OrderDispatcherInterface $orderDispatcher)
public function __construct(private readonly OrderUploaderInterface $orderUploader)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->orderDispatcher->dispatch();
$this->orderUploader->upload();

return 0;
}
Expand Down
13 changes: 0 additions & 13 deletions src/Dispatcher/OrderDispatcherInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Event/MapOrderLineEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class MapOrderLineEvent
{
public function __construct(
/**
* This is the order line that will be dispatched to Shipmondo
* This is the order line that will be uploaded to Shipmondo
*/
public readonly OrderLine $orderLine,
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Event/MapSalesOrderEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class MapSalesOrderEvent
{
public function __construct(
/**
* This is the sales order that will be dispatched to Shipmondo
* This is the sales order that will be uploaded to Shipmondo
*/
public readonly SalesOrder $salesOrder,
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Event/MapShippingOrderLineEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class MapShippingOrderLineEvent
{
public function __construct(
/**
* This is the order line that will be dispatched to Shipmondo
* This is the order line that will be uploaded to Shipmondo
*/
public readonly OrderLine $orderLine,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Doctrine\ORM\QueryBuilder;

/**
* This event is fired when selecting the pre-qualified dispatchable orders.
* Listen to this event to filter orders that are considered pre-qualified dispatchable orders.
* This event is fired when selecting the pre-qualified uploadable orders.
* Listen to this event to filter orders that are considered pre-qualified uploadable orders.
*/
final class PreSelectPreQualifiedDispatchableOrders
final class PreSelectPreQualifiedUploadableOrders
{
public function __construct(public readonly QueryBuilder $queryBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use Setono\SyliusShipmondoPlugin\Model\OrderInterface;

final class DispatchOrder implements CommandInterface
/**
* Uploads an order to Shipmondo
*/
final class UploadOrder implements CommandInterface
{
/**
* The order id
*/
public mixed $order;

/**
* If the version is set, it will be used to check if the order has been updated since it was dispatched
* If the version is set, it will be used to check if the order has been updated since it was triggered for upload
*/
public ?int $version = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

use Setono\Shipmondo\Client\ClientInterface;
use Setono\SyliusShipmondoPlugin\DataMapper\SalesOrderDataMapperInterface;
use Setono\SyliusShipmondoPlugin\Message\Command\DispatchOrder;
use Setono\SyliusShipmondoPlugin\Message\Command\UploadOrder;
use Setono\SyliusShipmondoPlugin\Model\OrderInterface;
use Setono\SyliusShipmondoPlugin\Workflow\OrderWorkflow;
use Sylius\Component\Order\Repository\OrderRepositoryInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Workflow\WorkflowInterface;
use Webmozart\Assert\Assert;

final class DispatchOrderHandler
final class UploadOrderHandler
{
public function __construct(
private readonly OrderRepositoryInterface $orderRepository,
private readonly ClientInterface $shipmondoClient,
private readonly SalesOrderDataMapperInterface $salesOrderDataMapper,
private readonly WorkflowInterface $orderWorkflow,
) {
}

public function __invoke(DispatchOrder $message): void
public function __invoke(UploadOrder $message): void
{
/** @var OrderInterface|null $order */
$order = $this->orderRepository->find($message->order);
Expand All @@ -32,11 +35,13 @@ public function __invoke(DispatchOrder $message): void
Assert::isInstanceOf($order, OrderInterface::class);

if (null !== $message->version && $order->getVersion() !== $message->version) {
throw new UnrecoverableMessageHandlingException(sprintf('Order with id %s has been updated since it was dispatched', (string) $message->order));
throw new UnrecoverableMessageHandlingException(sprintf('Order with id %s has been updated since it was tried to be uploaded', (string) $message->order));
}

$salesOrder = $this->salesOrderDataMapper->map($order);

$this->shipmondoClient->salesOrders()->create($salesOrder);

$this->orderWorkflow->apply($order, OrderWorkflow::TRANSITION_COMPLETE_UPLOAD);
}
}
4 changes: 2 additions & 2 deletions src/Model/OrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ interface OrderInterface extends BaseOrderInterface, VersionedInterface
{
public const SHIPMONDO_STATE_PENDING = 'pending';

public const SHIPMONDO_STATE_DISPATCHING = 'dispatching';
public const SHIPMONDO_STATE_UPLOADING_TO_SHIPMONDO = 'uploading_to_shipmondo';

public const SHIPMONDO_STATE_DISPATCHED = 'dispatched';
public const SHIPMONDO_STATE_UPLOADED_TO_SHIPMONDO = 'uploaded_to_shipmondo';

public const SHIPMONDO_STATE_FAILED = 'failed';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Doctrine\ORM\EntityRepository;
use DoctrineBatchUtils\BatchProcessing\SelectBatchIteratorAggregate;
use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\SyliusShipmondoPlugin\Event\PreSelectPreQualifiedDispatchableOrders;
use Setono\SyliusShipmondoPlugin\Event\PreSelectPreQualifiedUploadableOrders;
use Setono\SyliusShipmondoPlugin\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Order\Repository\OrderRepositoryInterface;

final class PreQualifiedDispatchableOrdersProvider implements PreQualifiedDispatchableOrdersProviderInterface
final class PreQualifiedUploadableOrdersProvider implements PreQualifiedUploadableOrdersProviderInterface
{
public function __construct(
private readonly OrderRepositoryInterface&EntityRepository $orderRepository,
Expand All @@ -37,7 +37,7 @@ public function getOrders(): \Generator
->setParameter('paymentStates', [OrderPaymentStates::STATE_PAID, OrderPaymentStates::STATE_AUTHORIZED])
;

$this->eventDispatcher->dispatch(new PreSelectPreQualifiedDispatchableOrders($qb));
$this->eventDispatcher->dispatch(new PreSelectPreQualifiedUploadableOrders($qb));

/** @var SelectBatchIteratorAggregate<array-key, OrderInterface> $iterator */
$iterator = SelectBatchIteratorAggregate::fromQuery($qb->getQuery(), 50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Setono\SyliusShipmondoPlugin\Model\OrderInterface;

interface PreQualifiedDispatchableOrdersProviderInterface
interface PreQualifiedUploadableOrdersProviderInterface
{
/**
* @return iterable<array-key, OrderInterface>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<import resource="services/command.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/data_mapper.xml"/>
<import resource="services/dispatcher.xml"/>
<import resource="services/event_subscriber.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/message.xml"/>
<import resource="services/provider.xml"/>
<import resource="services/registrar.xml"/>
<import resource="services/uploader.xml"/>
<import resource="services/webhook.xml"/>
</imports>
</container>
6 changes: 3 additions & 3 deletions src/Resources/config/services/command.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_shipmondo.command.dispatch_orders"
class="Setono\SyliusShipmondoPlugin\Command\DispatchOrdersCommand">
<argument type="service" id="setono_sylius_shipmondo.dispatcher.order"/>
<service id="setono_sylius_shipmondo.command.upload_orders"
class="Setono\SyliusShipmondoPlugin\Command\UploadOrdersCommand">
<argument type="service" id="setono_sylius_shipmondo.uploader.order"/>

<tag name="console.command"/>
</service>
Expand Down
5 changes: 3 additions & 2 deletions src/Resources/config/services/message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_shipmondo.message.command_handler.dispatch_order"
class="Setono\SyliusShipmondoPlugin\Message\CommandHandler\DispatchOrderHandler">
<service id="setono_sylius_shipmondo.message.command_handler.upload_order"
class="Setono\SyliusShipmondoPlugin\Message\CommandHandler\UploadOrderHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="setono_sylius_shipmondo.client"/>
<argument type="service" id="setono_sylius_shipmondo.data_mapper.sales_order"/>
<argument type="service" id="state_machine.setono_sylius_shipmondo__order"/>

<tag name="messenger.message_handler"/>
</service>
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_shipmondo.provider.pre_qualified_dispatchable_orders"
class="Setono\SyliusShipmondoPlugin\Provider\PreQualifiedDispatchableOrdersProvider">
<service id="setono_sylius_shipmondo.provider.pre_qualified_uploadable_orders"
class="Setono\SyliusShipmondoPlugin\Provider\PreQualifiedUploadableOrdersProvider">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="event_dispatcher"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_shipmondo.dispatcher.order"
class="Setono\SyliusShipmondoPlugin\Dispatcher\OrderDispatcher">
<argument type="service" id="setono_sylius_shipmondo.provider.pre_qualified_dispatchable_orders"/>
<service id="setono_sylius_shipmondo.uploader.order"
class="Setono\SyliusShipmondoPlugin\Uploader\OrderUploader">
<argument type="service" id="setono_sylius_shipmondo.provider.pre_qualified_uploadable_orders"/>
<argument type="service" id="setono_sylius_shipmondo.command_bus"/>
<argument type="service" id="state_machine.setono_sylius_shipmondo__order"/>
<argument type="service" id="doctrine"/>
Expand Down
12 changes: 6 additions & 6 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
setono_sylius_shipmondo:
ui:
shipmondo: Shipmondo
shipmondo_index: Manage your Shipmondo settings
shipmondo_state: Shipmondo state
dispatching: Dispatching
pending: Pending
dispatched: Dispatched
failed: Failed
pending: Pending
register_webhooks: Register webhooks
register_webhooks_information: Either you have not registered webhooks yet or you need to re-register them because of changes in the configuration. Either way, please click the "Register webhooks" button below.
shipmondo: Shipmondo
shipmondo_index: Manage your Shipmondo settings
shipmondo_state: Shipmondo state
uploaded_to_shipmondo: Uploaded to Shipmondo
uploading_to_shipmondo: Uploading to Shipmondo
webhooks_registered_information: Webhooks were registered on %date%.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

declare(strict_types=1);

namespace Setono\SyliusShipmondoPlugin\Dispatcher;
namespace Setono\SyliusShipmondoPlugin\Uploader;

use Doctrine\ORM\OptimisticLockException;
use Doctrine\Persistence\ManagerRegistry;
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait;
use Setono\SyliusShipmondoPlugin\Message\Command\DispatchOrder;
use Setono\SyliusShipmondoPlugin\Provider\PreQualifiedDispatchableOrdersProviderInterface;
use Setono\SyliusShipmondoPlugin\Message\Command\UploadOrder;
use Setono\SyliusShipmondoPlugin\Provider\PreQualifiedUploadableOrdersProviderInterface;
use Setono\SyliusShipmondoPlugin\Workflow\OrderWorkflow;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\WorkflowInterface;

final class OrderDispatcher implements OrderDispatcherInterface
final class OrderUploader implements OrderUploaderInterface
{
use ORMManagerTrait;

public function __construct(
private readonly PreQualifiedDispatchableOrdersProviderInterface $preQualifiedDispatchableOrdersProvider,
private readonly PreQualifiedUploadableOrdersProviderInterface $preQualifiedUploadableOrdersProvider,
private readonly MessageBusInterface $commandBus,
private readonly WorkflowInterface $orderWorkflow,
ManagerRegistry $managerRegistry,
) {
$this->managerRegistry = $managerRegistry;
}

public function dispatch(): void
public function upload(): void
{
// todo: check for eligibility

foreach ($this->preQualifiedDispatchableOrdersProvider->getOrders() as $order) {
foreach ($this->preQualifiedUploadableOrdersProvider->getOrders() as $order) {
try {
$this->orderWorkflow->apply($order, OrderWorkflow::TRANSITION_START_DISPATCH);
$this->orderWorkflow->apply($order, OrderWorkflow::TRANSITION_START_UPLOAD);
} catch (LogicException) {
continue;
}
Expand All @@ -44,7 +44,7 @@ public function dispatch(): void
continue;
}

$this->commandBus->dispatch(new DispatchOrder($order));
$this->commandBus->dispatch(new UploadOrder($order));
}
}
}
13 changes: 13 additions & 0 deletions src/Uploader/OrderUploaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusShipmondoPlugin\Uploader;

interface OrderUploaderInterface
{
/**
* Uploads eligible orders to Shipmondo
*/
public function upload(): void;
}
14 changes: 7 additions & 7 deletions src/Workflow/OrderWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ final class OrderWorkflow

final public const NAME = 'setono_sylius_shipmondo__order';

final public const TRANSITION_START_DISPATCH = 'start_dispatch';
final public const TRANSITION_START_UPLOAD = 'start_upload';

final public const TRANSITION_COMPLETE_DISPATCH = 'complete_dispatch';
final public const TRANSITION_COMPLETE_UPLOAD = 'complete_upload';

final public const TRANSITION_FAIL = 'fail';

Expand All @@ -34,8 +34,8 @@ public static function getStates(): array
{
return [
OrderInterface::SHIPMONDO_STATE_PENDING,
OrderInterface::SHIPMONDO_STATE_DISPATCHING,
OrderInterface::SHIPMONDO_STATE_DISPATCHED,
OrderInterface::SHIPMONDO_STATE_UPLOADING_TO_SHIPMONDO,
OrderInterface::SHIPMONDO_STATE_UPLOADED_TO_SHIPMONDO,
OrderInterface::SHIPMONDO_STATE_FAILED,
];
}
Expand Down Expand Up @@ -83,9 +83,9 @@ public static function getWorkflow(EventDispatcherInterface $eventDispatcher): W
public static function getTransitions(): array
{
return [
new Transition(self::TRANSITION_START_DISPATCH, [OrderInterface::SHIPMONDO_STATE_PENDING], OrderInterface::SHIPMONDO_STATE_DISPATCHING),
new Transition(self::TRANSITION_COMPLETE_DISPATCH, [OrderInterface::SHIPMONDO_STATE_DISPATCHING], OrderInterface::SHIPMONDO_STATE_DISPATCHED),
new Transition(self::TRANSITION_FAIL, [OrderInterface::SHIPMONDO_STATE_PENDING, OrderInterface::SHIPMONDO_STATE_DISPATCHING], OrderInterface::SHIPMONDO_STATE_FAILED),
new Transition(self::TRANSITION_START_UPLOAD, [OrderInterface::SHIPMONDO_STATE_PENDING], OrderInterface::SHIPMONDO_STATE_UPLOADING_TO_SHIPMONDO),
new Transition(self::TRANSITION_COMPLETE_UPLOAD, [OrderInterface::SHIPMONDO_STATE_UPLOADING_TO_SHIPMONDO], OrderInterface::SHIPMONDO_STATE_UPLOADED_TO_SHIPMONDO),
new Transition(self::TRANSITION_FAIL, [OrderInterface::SHIPMONDO_STATE_PENDING, OrderInterface::SHIPMONDO_STATE_UPLOADING_TO_SHIPMONDO], OrderInterface::SHIPMONDO_STATE_FAILED),
];
}
}

0 comments on commit 9bf834c

Please sign in to comment.