vendor/shopware/storefront/Framework/Seo/SeoUrlRoute/SeoUrlUpdateListener.php line 82

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Seo\SeoUrlRoute;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Category\CategoryDefinition;
  5. use Shopware\Core\Content\Category\CategoryEvents;
  6. use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
  7. use Shopware\Core\Content\LandingPage\Event\LandingPageIndexerEvent;
  8. use Shopware\Core\Content\LandingPage\LandingPageEvents;
  9. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SalesChannel\SalesChannelDefinition;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  19.  */
  20. class SeoUrlUpdateListener implements EventSubscriberInterface
  21. {
  22.     public const CATEGORY_SEO_URL_UPDATER 'category.seo-url';
  23.     public const PRODUCT_SEO_URL_UPDATER 'product.seo-url';
  24.     public const LANDING_PAGE_SEO_URL_UPDATER 'landing_page.seo-url';
  25.     private SeoUrlUpdater $seoUrlUpdater;
  26.     private Connection $connection;
  27.     private EntityIndexerRegistry $indexerRegistry;
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(SeoUrlUpdater $seoUrlUpdaterConnection $connectionEntityIndexerRegistry $indexerRegistry)
  32.     {
  33.         $this->seoUrlUpdater $seoUrlUpdater;
  34.         $this->connection $connection;
  35.         $this->indexerRegistry $indexerRegistry;
  36.     }
  37.     public function detectSalesChannelEntryPoints(EntityWrittenContainerEvent $event): void
  38.     {
  39.         $properties = ['navigationCategoryId''footerCategoryId''serviceCategoryId'];
  40.         $salesChannelIds $event->getPrimaryKeysWithPropertyChange(SalesChannelDefinition::ENTITY_NAME$properties);
  41.         if (empty($salesChannelIds)) {
  42.             return;
  43.         }
  44.         $this->indexerRegistry->sendIndexingMessage(['category.indexer''product.indexer']);
  45.     }
  46.     /**
  47.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             ProductEvents::PRODUCT_INDEXER_EVENT => 'updateProductUrls',
  53.             CategoryEvents::CATEGORY_INDEXER_EVENT => 'updateCategoryUrls',
  54.             LandingPageEvents::LANDING_PAGE_INDEXER_EVENT => 'updateLandingPageUrls',
  55.             EntityWrittenContainerEvent::class => 'detectSalesChannelEntryPoints',
  56.         ];
  57.     }
  58.     public function updateCategoryUrls(CategoryIndexerEvent $event): void
  59.     {
  60.         if (\in_array(self::CATEGORY_SEO_URL_UPDATER$event->getSkip(), true)) {
  61.             return;
  62.         }
  63.         $ids array_merge($event->getIds(), $this->getCategoryChildren($event->getIds()));
  64.         $this->seoUrlUpdater->update(NavigationPageSeoUrlRoute::ROUTE_NAME$ids);
  65.     }
  66.     public function updateProductUrls(ProductIndexerEvent $event): void
  67.     {
  68.         if (\in_array(self::PRODUCT_SEO_URL_UPDATER$event->getSkip(), true)) {
  69.             return;
  70.         }
  71.         $this->seoUrlUpdater->update(ProductPageSeoUrlRoute::ROUTE_NAME$event->getIds());
  72.     }
  73.     public function updateLandingPageUrls(LandingPageIndexerEvent $event): void
  74.     {
  75.         if (\in_array(self::LANDING_PAGE_SEO_URL_UPDATER$event->getSkip(), true)) {
  76.             return;
  77.         }
  78.         $this->seoUrlUpdater->update(LandingPageSeoUrlRoute::ROUTE_NAME$event->getIds());
  79.     }
  80.     /**
  81.      * @param list<string> $ids
  82.      *
  83.      * @return list<string>
  84.      */
  85.     private function getCategoryChildren(array $ids): array
  86.     {
  87.         if (empty($ids)) {
  88.             return [];
  89.         }
  90.         $query $this->connection->createQueryBuilder();
  91.         $query->select('category.id');
  92.         $query->from('category');
  93.         foreach ($ids as $id) {
  94.             $key 'id' $id;
  95.             $query->orWhere('category.type != :type AND category.path LIKE :' $key);
  96.             $query->setParameter($key'%' $id '%');
  97.         }
  98.         $query->setParameter('type'CategoryDefinition::TYPE_LINK);
  99.         $children $query->executeQuery()->fetchFirstColumn();
  100.         if (!$children) {
  101.             return [];
  102.         }
  103.         return Uuid::fromBytesToHexList($children);
  104.     }
  105. }