No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

DuplicateAddressEventSubscriber.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\Address;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Model\Address\AddressInterface;
  5. use Lc\SovBundle\Doctrine\EntityInterface;
  6. use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
  7. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  8. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  9. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  10. use Lc\SovBundle\Event\EntityComponentEvent;
  11. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  12. use Lc\SovBundle\Repository\AbstractRepositoryInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class DuplicateAddressEventSubscriber implements EventSubscriberInterface
  15. {
  16. protected $em;
  17. protected $adminUrlGenerator;
  18. public function __construct(EntityManagerInterface $entityManager)
  19. {
  20. $this->em = $entityManager;
  21. }
  22. public static function getSubscribedEvents()
  23. {
  24. return [
  25. EntityComponentEvent::DUPLICATE_EVENT => ['duplicateAddressOnDuplicateEvent'],
  26. ];
  27. }
  28. public function duplicateAddressOnDuplicateEvent(EntityComponentEvent $event)
  29. {
  30. $entity = $event->getEntity();
  31. $classMetadata = $this->em->getClassMetadata(get_class($entity));
  32. foreach ($classMetadata->getAssociationMappings() as $associationMapping){
  33. if(in_array(AddressInterface::class, class_implements($associationMapping['targetEntity']))){
  34. $methodGet = 'get'.ucfirst($associationMapping['fieldName']);
  35. $methodSet = 'set'.ucfirst($associationMapping['fieldName']);
  36. if(method_exists($entity, $methodGet) && method_exists($entity, $methodSet)){
  37. $newAddress = clone $entity->$methodGet();
  38. $entity->$methodSet($newAddress);
  39. $this->em->persist($newAddress);
  40. }
  41. }
  42. }
  43. }
  44. }