|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber\Address;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- use Lc\CaracoleBundle\Model\Address\AddressInterface;
- use Lc\SovBundle\Doctrine\EntityInterface;
- use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
- use Lc\SovBundle\Doctrine\Extension\SortableInterface;
- use Lc\SovBundle\Doctrine\Extension\StatusInterface;
- use Lc\SovBundle\Doctrine\Extension\TreeInterface;
- use Lc\SovBundle\Event\EntityComponentEvent;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Lc\SovBundle\Repository\AbstractRepositoryInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class DuplicateAddressEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $adminUrlGenerator;
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->em = $entityManager;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- EntityComponentEvent::DUPLICATE_EVENT => ['duplicateAddressOnDuplicateEvent'],
- ];
- }
-
- public function duplicateAddressOnDuplicateEvent(EntityComponentEvent $event)
- {
- $entity = $event->getEntity();
-
- $classMetadata = $this->em->getClassMetadata(get_class($entity));
-
- foreach ($classMetadata->getAssociationMappings() as $associationMapping){
- if(in_array(AddressInterface::class, class_implements($associationMapping['targetEntity']))){
- $methodGet = 'get'.ucfirst($associationMapping['fieldName']);
- $methodSet = 'set'.ucfirst($associationMapping['fieldName']);
- if(method_exists($entity, $methodGet) && method_exists($entity, $methodSet)){
- $newAddress = clone $entity->$methodGet();
- $entity->$methodSet($newAddress);
- $this->em->persist($newAddress);
- }
- }
-
- }
- }
-
-
- }
|