src/Order/Workflow/DetailSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Order\Workflow;
  4. use App\Order\Doctrine\Entity\Detail;
  5. use App\Point\UseCase\Transaction\Refund\RefundInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Workflow\Event\Event;
  8. use Symfony\Component\Workflow\WorkflowInterface;
  9. final class DetailSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(private WorkflowInterface $orderStateMachine, private RefundInterface $refund)
  12.     {
  13.     }
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             'workflow.detail.completed' => ['onCompleted'],
  21.             'workflow.detail.completed.cancel' => ['onCompletedCancel'],
  22.         ];
  23.     }
  24.     public function onCompletedCancel(Event $event): void
  25.     {
  26.         /** @var Detail $detail */
  27.         $detail $event->getSubject();
  28.         $this->refund->__invoke($detail);
  29.     }
  30.     public function onCompleted(Event $event): void
  31.     {
  32.         /** @var Detail $detail */
  33.         $detail $event->getSubject();
  34.         $detail->addStateHistory($detail->getState());
  35.         if ($this->orderStateMachine->can($detail->getOrder(), 'process')) {
  36.             $this->orderStateMachine->apply($detail->getOrder(), 'process');
  37.         }
  38.     }
  39. }