SHARE
TWEET

Untitled

a guest Oct 21st, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: bruno
  5.  * Date: 14.06.19.
  6.  * Time: 16:20
  7.  */
  8.  
  9. namespace Q\CoreBundle\EventListener;
  10.  
  11. use ApartmentBundle\Entity\Apartment;
  12. use ApartmentBundle\Entity\BookingService;
  13. use ApartmentBundle\Entity\Building;
  14. use Doctrine\Common\EventSubscriber;
  15. use Doctrine\ORM\Event\LifecycleEventArgs;
  16. use Doctrine\ORM\Events;
  17. use Q\CoreBundle\Entity\User;
  18. use Ramsey\Uuid\Doctrine\UuidGenerator;
  19.  
  20. class UuidSubscriberListener implements EventSubscriber
  21. {
  22.     /**
  23.      * @var UuidGenerator
  24.      */
  25.     private $generator;
  26.  
  27.     /**
  28.      * @param UuidGenerator $uuidGenerator
  29.      */
  30.     public function __construct(UuidGenerator $uuidGenerator)
  31.     {
  32.         $this->generator = $uuidGenerator;
  33.     }
  34.  
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function getSubscribedEvents()
  39.     {
  40.         return [
  41.             Events::prePersist,
  42.             Events::preUpdate,
  43.         ];
  44.     }
  45.  
  46.     /**
  47.      * @param LifecycleEventArgs $args
  48.      */
  49.     public function prePersist(LifecycleEventArgs $args)
  50.     {
  51.         $this->updateUuid($args);
  52.     }
  53.  
  54.     /**
  55.      * @param LifecycleEventArgs $args
  56.      */
  57.     public function preUpdate(LifecycleEventArgs $args)
  58.     {
  59.         $this->updateUuid($args);
  60.     }
  61.  
  62.     /**
  63.      * @param LifecycleEventArgs $args
  64.      */
  65.     public function updateUuid(LifecycleEventArgs $args)
  66.     {
  67.         $entity = $args->getEntity();
  68.  
  69.         if (!$entity instanceof BookingService &&
  70.             !$entity instanceof Apartment &&
  71.             !$entity instanceof Building &&
  72.             !$entity instanceof User)
  73.         {
  74.             return;
  75.         }
  76.  
  77.         if (empty($entity->getUuid()))
  78.         {
  79.             $uuid = $this->generator->generate($args->getEntityManager(), $entity);
  80.             $entity->setUuid($uuid);
  81.         }
  82.     }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top