<?php
declare(strict_types=1);
namespace Kobizo\Bundle\ECommerceBundle\EventSubscriber;
use DateTime;
use Kobizo\Bundle\ECommerceBundle\Entity\Order;
use Kobizo\Bundle\ECommerceBundle\Processor\Order\OrderHistoryProcessor;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
class OrderWorkflowSubscriber implements EventSubscriberInterface
{
private OrderHistoryProcessor $orderHistoryProcessor;
public function __construct(OrderHistoryProcessor $orderHistoryProcessor)
{
$this->orderHistoryProcessor = $orderHistoryProcessor;
}
public static function getSubscribedEvents()
{
return [
'workflow.order.enter' => 'onEnter',
];
}
public function onEnter(Event $event)
{
/** @var Order $order */
$order = $event->getSubject();
$this->orderHistoryProcessor->add($order, sprintf(
'%s - Order #%s (ID=%s) performed transition "%s" from "%s" to "%s"',
(new DateTime())->format('Y-m-d H:i:s'),
$order->getReference(),
$order->getId(),
$event->getTransition()->getName(),
implode(', ', array_keys($order->getState())),
implode(', ', $event->getTransition()->getTos())
));
}
}