vendor/kunstmaan/pagepart-bundle/EventListener/CloneListener.php line 46

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\PagePartBundle\EventListener;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
  6. use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
  7. use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
  8. use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
  9. use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
  10. /**
  11.  * This event will make sure pageparts are being copied when deepClone is done on an entity implementing hasPagePartsInterface
  12.  */
  13. class CloneListener
  14. {
  15.     /**
  16.      * @var EntityManager|EntityManagerInterface
  17.      */
  18.     private $em;
  19.     /**
  20.      * @var PagePartConfigurationReaderInterface
  21.      */
  22.     private $pagePartReader;
  23.     /**
  24.      * @var PageTemplateConfigurationService
  25.      */
  26.     private $pageTemplateConfiguratiorService;
  27.     public function __construct(
  28.         EntityManagerInterface $em,
  29.         PagePartConfigurationReaderInterface $pagePartReader,
  30.         PageTemplateConfigurationService $pageTemplateConfiguratiorService
  31.     ) {
  32.         $this->em $em;
  33.         $this->pagePartReader $pagePartReader;
  34.         $this->pageTemplateConfiguratiorService $pageTemplateConfiguratiorService;
  35.     }
  36.     /**
  37.      * @param DeepCloneAndSaveEvent $event
  38.      */
  39.     public function postDeepCloneAndSave(DeepCloneAndSaveEvent $event)
  40.     {
  41.         $originalEntity $event->getEntity();
  42.         if ($originalEntity instanceof HasPagePartsInterface) {
  43.             $clonedEntity $event->getClonedEntity();
  44.             $contexts $this->pagePartReader->getPagePartContexts($originalEntity);
  45.             foreach ($contexts as $context) {
  46.                 $this->em->getRepository('KunstmaanPagePartBundle:PagePartRef')->copyPageParts($this->em$originalEntity$clonedEntity$context);
  47.             }
  48.         }
  49.         if ($originalEntity instanceof HasPageTemplateInterface) {
  50.             $clonedEntity $event->getClonedEntity();
  51.             $newPageTemplateConfiguration = clone $this->pageTemplateConfiguratiorService->findOrCreateFor($originalEntity);
  52.             $newPageTemplateConfiguration->setId(null);
  53.             $newPageTemplateConfiguration->setPageId($clonedEntity->getId());
  54.             $this->em->persist($newPageTemplateConfiguration);
  55.         }
  56.     }
  57. }