Skip to content

Commit

Permalink
Add order listener
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 29, 2024
1 parent eb43d7b commit a618b0f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/EventListener/Doctrine/OrderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\EventListener\Doctrine;

use Doctrine\Persistence\Event\PreUpdateEventArgs;
use Setono\SyliusPeakPlugin\Factory\UploadOrderRequestFactoryInterface;
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use Setono\SyliusPeakPlugin\Workflow\UploadOrderRequestWorkflow;
use Symfony\Component\Workflow\WorkflowInterface;

final class OrderListener
{
public function __construct(
private readonly UploadOrderRequestFactoryInterface $uploadOrderRequestFactory,
private readonly WorkflowInterface $uploadOrderRequestWorkflow,
) {
}

public function preUpdate(PreUpdateEventArgs $eventArgs): void
{
$obj = $eventArgs->getObject();
if (!$obj instanceof OrderInterface) {
return;
}

// For now this listener only triggers when the state changes
if (!$eventArgs->hasChangedField('state')) {
return;
}

$uploadOrderRequest = $obj->getPeakUploadOrderRequest() ?? $this->uploadOrderRequestFactory->createNew();
$this->uploadOrderRequestWorkflow->apply($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_RESET);

$obj->setPeakUploadOrderRequest($uploadOrderRequest);
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<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\SyliusPeakPlugin\EventListener\Doctrine\OrderListener">
<argument type="service" id="setono_sylius_peak.factory.upload_order_request"/>
<argument type="service" id="state_machine.setono_sylius_peak__upload_order_request"/>

<tag name="doctrine.event_listener" event="preUpdate"/>
</service>

<service id="Setono\SyliusPeakPlugin\EventListener\Doctrine\ProductListener">
<argument type="service" id="setono_sylius_peak.factory.upload_product_variant_request"/>
<argument type="service" id="state_machine.setono_sylius_peak__upload_product_variant_request"/>
Expand Down

0 comments on commit a618b0f

Please sign in to comment.