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

Cancel order in Peak WMS when order is cancelled in Sylius #7

Merged
merged 1 commit into from
Sep 9, 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
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