src/EventSubscriber/AddDateSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Category;
  4. use App\Entity\DateCmsInterface;
  5. use App\Entity\Content;
  6. use App\Entity\Product;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use ApiPlatform\Symfony\EventListener\EventPriorities;
  13. class AddDateSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct()
  16.     {
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             KernelEvents::VIEW => ['attachDate'EventPriorities::PRE_WRITE],
  22.         ];
  23.     }
  24.     public function attachDate(ViewEvent  $event)
  25.     {
  26.         $controllerResulte $event->getControllerResult();
  27.         $method $event->getRequest()->getMethod();
  28.         if (!$controllerResulte instanceof DateCmsInterface
  29.             || (Request::METHOD_POST !== $method
  30.             && Request::METHOD_PUT !== $method
  31.             && Request::METHOD_PATCH !== $method)
  32.         ) {
  33.             return;
  34.         }
  35.         if(Request::METHOD_POST === $method)
  36.         {
  37.             $controllerResulte->setCreatedAt(new \DateTime('now'));
  38.         }
  39.         $controllerResulte->setUpdatedAt(new \DateTime('now'));
  40.     }
  41. }