src/EventSubscriber/AppSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. /**
  3.  * AppSubscriber
  4.  *
  5.  * @package    App\EventListener
  6.  * @author     phisch <info@i42.de>
  7.  * @version    1.0
  8.  */
  9. namespace App\EventSubscriber;
  10. use DateTime;
  11. use App\Entity\Gplace;
  12. use App\Entity\GplaceImport;
  13. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  14. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. /**
  18.  * AppSubscriber class
  19.  */
  20. class AppSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * Container
  24.      *
  25.      * @var Object
  26.      */
  27.     protected $container;
  28.     /**
  29.      * AppSubscriber constructor.
  30.      *
  31.      * @param ContainerInterface $container
  32.      *
  33.      * @return void Nothing
  34.      */
  35.     public function __construct(ContainerInterface $container)
  36.     {
  37.         $this->container $container;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             BeforeEntityUpdatedEvent::class => ['setUpdateDate'],
  43.             BeforeEntityPersistedEvent::class => ['setCreatedDate']
  44.         ];
  45.     }
  46.     public function setUpdateDate(BeforeEntityUpdatedEvent $event)
  47.     {
  48.         $entity $event->getEntityInstance();
  49.         
  50.         if ($entity instanceof Gplace || $entity instanceof GplaceImport)
  51.         {
  52.             $entity->setUpdatedAt(new \DateTime('now'));
  53.             
  54.         }
  55.     }
  56.     public function setCreatedDate(BeforeEntityPersistedEvent $event)
  57.     {
  58.         $entity $event->getEntityInstance();
  59.         
  60.         if ($entity instanceof Gplace || $entity instanceof GplaceImport)
  61.         {
  62.             $entity->setCreatedAt(new \DateTime('now'));
  63.             $entity->setUpdatedAt($entity->getCreatedAt());
  64.         }
  65.     }
  66. }