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

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