vendor/roothirsch/shop-bundle/EventSubscriber/Api/EstimateDuplicateSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/BookMailSubscriber.php
  3. namespace Roothirsch\ShopBundle\EventSubscriber\Api;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use Roothirsch\ShopBundle\Repository\EstimateRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. final class EstimateDuplicateSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var \Roothirsch\ShopBundle\Repository\EstimateRepository
  12.      */
  13.     private $estimateRepository;
  14.     public function __construct(EstimateRepository $estimateRepository)
  15.     {
  16.         $this->estimateRepository $estimateRepository;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             KernelEvents::VIEW => ['validate'EventPriorities::PRE_VALIDATE],
  22.         ];
  23.     }
  24.     public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  25.     {
  26.         if ($event->getRequest()->attributes->get('_route') === 'api_estimates_duplicate_collection') {
  27.             /** @var \Roothirsch\ShopBundle\Entity\Estimate $estimate */
  28.             $duplicate = clone $this->estimateRepository->find($event->getRequest()->attributes->get('id'));
  29.             $duplicate->setCreated(time());
  30.             $duplicate->setUpdated(time());
  31.             $duplicate->setOrderedAt(null);
  32.             $duplicate->setDeliveryDate(null);
  33.             $duplicate->setId(null);
  34.             $event->setControllerResult($duplicate);
  35.         }
  36.     }
  37. }