<?php
namespace CioSponsoredOrderConfirmation\Subscriber;
use CioSponsoredOrderConfirmation\CioSponsoredOrderConfirmation;
use CioSponsoredOrderConfirmation\Service\OrderConfirmationService;
use Shopware\Core\Checkout\Order\OrderStates;
use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderChangesSubscriber implements EventSubscriberInterface
{
private OrderConfirmationService $orderConfirmationService;
public function __construct(OrderConfirmationService $orderConfirmationService)
{
$this->orderConfirmationService = $orderConfirmationService;
}
public static function getSubscribedEvents(): array
{
return [
StateMachineTransitionEvent::class => 'onStateMachineTransition',
];
}
public function onStateMachineTransition(StateMachineTransitionEvent $event)
{
if ($event->getToPlace()->getTechnicalName() == CioSponsoredOrderConfirmation::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME) {
$order = $this->orderConfirmationService->getOrder($event->getEntityId(), $event->getContext());
$this->orderConfirmationService->allow($order, $event->getContext());
}
if ($event->getToPlace()->getTechnicalName() == OrderStates::STATE_CANCELLED || $event->getToPlace()->getTechnicalName() == CioSponsoredOrderConfirmation::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME) {
$order = $this->orderConfirmationService->getOrder($event->getEntityId(), $event->getContext());
$this->orderConfirmationService->deny($order, $event->getContext());
}
}
}