Skip to content

Commit

Permalink
Cancel order in Peak WMS when order is cancelled in Sylius
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Sep 9, 2024
1 parent 1a04946 commit bff72b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/EventSubscriber/HandleOrderCancellationSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\EventSubscriber;

use Setono\PeakWMS\Client\ClientInterface;
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use SM\Event\SMEvents;
use SM\Event\TransitionEvent;
use Sylius\Component\Order\OrderTransitions;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class HandleOrderCancellationSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly ClientInterface $client)
{
}

public static function getSubscribedEvents(): array

Check warning on line 20 in src/EventSubscriber/HandleOrderCancellationSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/HandleOrderCancellationSubscriber.php#L20

Added line #L20 was not covered by tests
{
return [
SMEvents::POST_TRANSITION => 'handleOrderCancellation',
];

Check warning on line 24 in src/EventSubscriber/HandleOrderCancellationSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/HandleOrderCancellationSubscriber.php#L22-L24

Added lines #L22 - L24 were not covered by tests
}

public function handleOrderCancellation(TransitionEvent $event): void
{
$obj = $event->getStateMachine()->getObject();
if (!$obj instanceof OrderInterface) {
return;

Check warning on line 31 in src/EventSubscriber/HandleOrderCancellationSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/HandleOrderCancellationSubscriber.php#L31

Added line #L31 was not covered by tests
}

if ($event->getTransition() !== OrderTransitions::TRANSITION_CANCEL) {
return;
}

if ($obj->getPeakUploadOrderRequest()?->getPeakOrderId() === null) {
return;

Check warning on line 39 in src/EventSubscriber/HandleOrderCancellationSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/HandleOrderCancellationSubscriber.php#L38-L39

Added lines #L38 - L39 were not covered by tests
}

$this->client->salesOrder()->cancel((string) $obj->getId());

Check warning on line 42 in src/EventSubscriber/HandleOrderCancellationSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/HandleOrderCancellationSubscriber.php#L42

Added line #L42 was not covered by tests
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="Setono\SyliusPeakPlugin\EventSubscriber\HandleOrderCancellationSubscriber">
<argument type="service" id="Setono\PeakWMS\Client\ClientInterface"/>

<tag name="kernel.event_subscriber"/>
</service>

<!-- Workflow: Inventory Update -->
<service id="Setono\SyliusPeakPlugin\EventSubscriber\Workflow\InventoryUpdate\CompleteSubscriber">
<tag name="kernel.event_subscriber"/>
Expand Down

0 comments on commit bff72b3

Please sign in to comment.