<?php
declare(strict_types=1);
namespace App\Order\Workflow;
use App\Order\Doctrine\Entity\Detail;
use App\Point\UseCase\Transaction\Refund\RefundInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\WorkflowInterface;
final class DetailSubscriber implements EventSubscriberInterface
{
public function __construct(private WorkflowInterface $orderStateMachine, private RefundInterface $refund)
{
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
'workflow.detail.completed' => ['onCompleted'],
'workflow.detail.completed.cancel' => ['onCompletedCancel'],
];
}
public function onCompletedCancel(Event $event): void
{
/** @var Detail $detail */
$detail = $event->getSubject();
$this->refund->__invoke($detail);
}
public function onCompleted(Event $event): void
{
/** @var Detail $detail */
$detail = $event->getSubject();
$detail->addStateHistory($detail->getState());
if ($this->orderStateMachine->can($detail->getOrder(), 'process')) {
$this->orderStateMachine->apply($detail->getOrder(), 'process');
}
}
}