<?php declare(strict_types=1);
namespace App\Order\Service;
use App\Order\Entity\Order;
use App\Order\Entity\OrderActivity;
use App\Order\Event\OrderCancelledEvent;
use App\Order\Event\OrderPackedEvent;
use App\Order\Repository\OrderActivityRepository;
use Doctrine\ORM\ORMException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderActivityService implements EventSubscriberInterface
{
private OrderActivityRepository $repository;
private OrderService $orderService;
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
OrderPackedEvent::class => 'onOrderPacked',
];
}
public function __construct(OrderActivityRepository $repository, OrderService $orderService)
{
$this->repository = $repository;
$this->orderService = $orderService;
}
/**
* @throws ORMException
*/
public function onOrderPacked(OrderPackedEvent $event): void
{
// TODO: Use messenger...
$orderId = $event->getOrder();
$order = $this->orderService->getOrderById($orderId);
$activity = new OrderActivity();
$activity->setOrder($order);
$activity->settype('order.packed');
$activity->setName('armó el pedido #' . $order->getId());
// Persist
$this->repository->add($activity, true);
}
/**
* @return OrderActivity[]
*/
public function getActivityForOrder(Order $order): array
{
return $order->getActivity()->toArray();
}
}