Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

57 rindas
1.9KB

  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->create($newAddress, false);
  40. }
  41. }
  42. }
  43. }
  44. }