Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: bruno
- * Date: 14.06.19.
- * Time: 16:20
- */
- namespace Q\CoreBundle\EventListener;
- use ApartmentBundle\Entity\Apartment;
- use ApartmentBundle\Entity\BookingService;
- use ApartmentBundle\Entity\Building;
- use Doctrine\Common\EventSubscriber;
- use Doctrine\ORM\Event\LifecycleEventArgs;
- use Doctrine\ORM\Events;
- use Q\CoreBundle\Entity\User;
- use Ramsey\Uuid\Doctrine\UuidGenerator;
- class UuidSubscriberListener implements EventSubscriber
- {
- /**
- * @var UuidGenerator
- */
- private $generator;
- /**
- * @param UuidGenerator $uuidGenerator
- */
- public function __construct(UuidGenerator $uuidGenerator)
- {
- $this->generator = $uuidGenerator;
- }
- /**
- * {@inheritdoc}
- */
- public function getSubscribedEvents()
- {
- return [
- Events::prePersist,
- Events::preUpdate,
- ];
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function prePersist(LifecycleEventArgs $args)
- {
- $this->updateUuid($args);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preUpdate(LifecycleEventArgs $args)
- {
- $this->updateUuid($args);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function updateUuid(LifecycleEventArgs $args)
- {
- $entity = $args->getEntity();
- if (!$entity instanceof BookingService &&
- !$entity instanceof Apartment &&
- !$entity instanceof Building &&
- !$entity instanceof User)
- {
- return;
- }
- if (empty($entity->getUuid()))
- {
- $uuid = $this->generator->generate($args->getEntityManager(), $entity);
- $entity->setUuid($uuid);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement