lib/boab/cms-bundle/src/EventListener/TermListener.php line 44

Open in your IDE?
  1. <?php 
  2. namespace Boab\CmsBundle\EventListener;
  3. use Boab\CmsBundle\Entity\TermInterface;
  4. use Boab\CmsBundle\Entity\TermRoute;
  5. use Boab\CmsBundle\Entity\Term;
  6. use Doctrine\ORM\Event\OnFlushEventArgs;
  7. use Boab\CmsBundle\Event\TermPostPersistEvent;
  8. class TermListener
  9. {
  10.     public function onFlush(OnFlushEventArgs $args)
  11.     {
  12.         $em $args->getEntityManager();
  13.         $uow $em->getUnitOfWork();
  14.     
  15.         $entities array_merge(
  16.             $uow->getScheduledEntityInsertions(),
  17.             $uow->getScheduledEntityUpdates()
  18.         );
  19.     
  20.         foreach ($entities as $entity) {
  21.             if (!$entity instanceof TermInterface) {
  22.                 continue;
  23.             }
  24.             //$changeSet = $uow->getEntityChangeSet($entity);
  25.             //dump($changeSet);
  26.             
  27.             $route $this->createRouteFromTerm($entity);
  28.             $em->persist($route);
  29.             $tmd $em->getClassMetadata(TermRoute::class);
  30.             $uow->computeChangeSet($tmd$route);
  31.             $entity->setRoute($route);
  32.             if(!$entity->getId()){
  33.                 $md $em->getClassMetadata(Term::class);
  34.                 $uow->recomputeSingleEntityChangeSet($md$entity);
  35.             }
  36.         }
  37.     }
  38.     public function onTermPesist(TermPostPersistEvent $event):void
  39.     {
  40.         $term $event->getTerm();
  41.         $route $this->createRouteFromTerm($term);
  42.         $term->setRoute($route);
  43.     }
  44.     private function createRouteFromTerm(TermInterface $term): TermRoute
  45.     {
  46.         $route $term->getRoute();
  47.         if(!$route){
  48.             $route = new TermRoute;
  49.             $route->setDateCreated( new \DateTime('now'));
  50.         }
  51.         $route->setTitle($term->getName())
  52.             ->setSlug($term->getSlug())
  53.             ->setPath($term->getPath())
  54.             ->setVisibility($term->isEnabled());
  55.             
  56.         if($parent $term->getParent()){
  57.             $route->setParentId($parent->getRoute()->getId());
  58.         }else{
  59.             $route->setParentId(0) ;
  60.         }
  61.         return $route;
  62.     }
  63. }