src/Aqarmap/Bundle/ListingBundle/EventListener/ListingSlugListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Constant\PhotoTypes;
  4. use Aqarmap\Bundle\ListingBundle\Entity\ListingPhoto;
  5. use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
  6. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ListingSlugListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var ListingManager
  13.      */
  14.     private $listingManager;
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     private $entityManager;
  19.     public function __construct(ListingManager $listingManagerEntityManagerInterface $entityManager)
  20.     {
  21.         $this->listingManager $listingManager;
  22.         $this->entityManager $entityManager;
  23.     }
  24.     public function afterSaveListingEvent(ListingEvent $event): void
  25.     {
  26.         $listing $event->getListing();
  27.         $slug null;
  28.         // Add parent Slug
  29.         if ($listing->getParent() && !$listing->getCustomSlug()) {
  30.             $listing->setSlug($listing->getParent()->getSlug());
  31.         } else {
  32.             // Add Section Slug
  33.             // Todo: This doesn't support 3 levels listings
  34.             if ($listing->getSection()) {
  35.                 $slug .= $listing->getSection()->getSlug();
  36.             }
  37.             // Add Custom Slug Or add -{property-type}-{location}
  38.             if ($listing->getCustomSlug()) {
  39.                 $slug .= '-'.$listing->getCustomSlug();
  40.             } else {
  41.                 // Add Property Type Slug
  42.                 if ($propertyType $listing->getPropertyType()) {
  43.                     while ($propertyType->getParent() && $propertyType->getLevel() > 1) {
  44.                         $propertyType $propertyType->getParent();
  45.                     }
  46.                 }
  47.                 // Add Location Slug
  48.                 if ($location $listing->getLocation()) {
  49.                     if (!$location->getSearchable() && $location->getParent()) {
  50.                         while ($location && !$location->getSearchable()) {
  51.                             $location $location->getParent();
  52.                         }
  53.                     }
  54.                     if (!empty($listing->getLocation())) {
  55.                         $slug .= '-'.$listing->getLocation()->getSlug();
  56.                     }
  57.                 }
  58.             }
  59.             $slug str_replace('/''-'$slug);
  60.             $listing->setSlug(trim($slug'-'));
  61.         }
  62.         $mainPhoto $listing->getMainPhoto();
  63.         if ($mainPhoto instanceof ListingPhoto) {
  64.             if (PhotoTypes::MAIN_PHOTO == !$mainPhoto->getType()) {
  65.                 $mainPhoto->setType(PhotoTypes::MAIN_PHOTO);
  66.                 $this->getEntityManager()->persist($mainPhoto);
  67.                 $this->getEntityManager()->flush($mainPhoto);
  68.             }
  69.         }
  70.         /** @var $em \Doctrine\ORM\EntityManager */
  71.         $em $this->getEntityManager();
  72.         $em->persist($listing);
  73.         $em->flush();
  74.         $this->listingManager->syncSlug($listing);
  75.     }
  76.     /**
  77.      * @return \Doctrine\ORM\EntityManager
  78.      */
  79.     public function getEntityManager()
  80.     {
  81.         return $this->entityManager;
  82.     }
  83.     public static function getSubscribedEvents()
  84.     {
  85.         return [
  86.             'aqarmap.listing.after_save' => ['afterSaveListingEvent'],
  87.         ];
  88.     }
  89. }