<?php
// api/src/EventSubscriber/BookMailSubscriber.php
namespace Roothirsch\ShopBundle\EventSubscriber\Api;
use ApiPlatform\Core\EventListener\EventPriorities;
use Roothirsch\ShopBundle\Repository\EstimateRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
final class EstimateDuplicateSubscriber implements EventSubscriberInterface
{
/**
* @var \Roothirsch\ShopBundle\Repository\EstimateRepository
*/
private $estimateRepository;
public function __construct(EstimateRepository $estimateRepository)
{
$this->estimateRepository = $estimateRepository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['validate', EventPriorities::PRE_VALIDATE],
];
}
public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
{
if ($event->getRequest()->attributes->get('_route') === 'api_estimates_duplicate_collection') {
/** @var \Roothirsch\ShopBundle\Entity\Estimate $estimate */
$duplicate = clone $this->estimateRepository->find($event->getRequest()->attributes->get('id'));
$duplicate->setCreated(time());
$duplicate->setUpdated(time());
$duplicate->setOrderedAt(null);
$duplicate->setDeliveryDate(null);
$duplicate->setId(null);
$event->setControllerResult($duplicate);
}
}
}