<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Core\Subscriber\Order;
use Bodymed\Webshop\Messenger\Messages\Order\OrderTransactionStateChanged;
use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class OrderStateSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $messageBus;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
public static function getSubscribedEvents(): array
{
return [
'state_enter.order_transaction.state.paid' => 'onEnterOrderTransactionStatePaid',
];
}
public function onEnterOrderTransactionStatePaid(OrderStateMachineStateChangeEvent $event): void
{
$this->dispatchMessage($event);
}
private function dispatchMessage(OrderStateMachineStateChangeEvent $event): void
{
$context = $event->getContext();
$order = $event->getOrder();
$salesChannelId = $order->getSalesChannelId();
$this->messageBus->dispatch(OrderTransactionStateChanged::create($order, $context, $salesChannelId));
}
}