vendor/shopware/core/Content/Sitemap/ScheduledTask/SitemapGenerateTaskHandler.php line 119

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Sitemap\ScheduledTask;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Content\Sitemap\Event\SitemapSalesChannelCriteriaEvent;
  5. use Shopware\Core\Content\Sitemap\Exception\AlreadyLockedException;
  6. use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\Framework\Log\Package;
  14. use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
  15. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelDomain\SalesChannelDomainEntity;
  16. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  17. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  18. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  19. use Shopware\Core\System\SystemConfig\SystemConfigService;
  20. use Symfony\Component\Messenger\MessageBusInterface;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * @deprecated tag:v6.5.0 - reason:becomes-internal - MessageHandler will be internal and final starting with v6.5.0.0
  24.  */
  25. #[Package('sales-channel')]
  26. class SitemapGenerateTaskHandler extends ScheduledTaskHandler
  27. {
  28.     private EntityRepositoryInterface $salesChannelRepository;
  29.     private AbstractSalesChannelContextFactory $salesChannelContextFactory;
  30.     private SitemapExporterInterface $sitemapExporter;
  31.     private LoggerInterface $logger;
  32.     private SystemConfigService $systemConfigService;
  33.     private MessageBusInterface $messageBus;
  34.     private EventDispatcherInterface $eventDispatcher;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(
  39.         EntityRepositoryInterface $scheduledTaskRepository,
  40.         EntityRepositoryInterface $salesChannelRepository,
  41.         AbstractSalesChannelContextFactory $salesChannelContextFactory,
  42.         SitemapExporterInterface $sitemapExporter,
  43.         LoggerInterface $logger,
  44.         SystemConfigService $systemConfigService,
  45.         MessageBusInterface $messageBus,
  46.         EventDispatcherInterface $eventDispatcher
  47.     ) {
  48.         parent::__construct($scheduledTaskRepository);
  49.         $this->salesChannelRepository $salesChannelRepository;
  50.         $this->salesChannelContextFactory $salesChannelContextFactory;
  51.         $this->sitemapExporter $sitemapExporter;
  52.         $this->logger $logger;
  53.         $this->systemConfigService $systemConfigService;
  54.         $this->messageBus $messageBus;
  55.         $this->eventDispatcher $eventDispatcher;
  56.     }
  57.     public static function getHandledMessages(): iterable
  58.     {
  59.         return [
  60.             SitemapGenerateTask::class,
  61.             SitemapMessage::class,
  62.         ];
  63.     }
  64.     public function run(): void
  65.     {
  66.         $criteria = new Criteria();
  67.         $criteria->addAssociation('domains');
  68.         $criteria->addFilter(new NotFilter(
  69.             NotFilter::CONNECTION_AND,
  70.             [new EqualsFilter('domains.id'null)]
  71.         ));
  72.         $criteria->addAssociation('type');
  73.         $criteria->addFilter(new EqualsFilter('type.id'Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
  74.         $context Context::createDefaultContext();
  75.         $this->eventDispatcher->dispatch(
  76.             new SitemapSalesChannelCriteriaEvent($criteria$context)
  77.         );
  78.         $salesChannels $this->salesChannelRepository->search($criteria$context)->getEntities();
  79.         /** @var SalesChannelEntity $salesChannel */
  80.         foreach ($salesChannels as $salesChannel) {
  81.             if ($salesChannel->getDomains() === null) {
  82.                 continue;
  83.             }
  84.             $languageIds $salesChannel->getDomains()->map(function (SalesChannelDomainEntity $salesChannelDomain) {
  85.                 return $salesChannelDomain->getLanguageId();
  86.             });
  87.             $languageIds array_unique($languageIds);
  88.             foreach ($languageIds as $languageId) {
  89.                 $this->messageBus->dispatch(new SitemapMessage($salesChannel->getId(), $languageIdnullnullfalse));
  90.             }
  91.         }
  92.     }
  93.     /**
  94.      * @param SitemapGenerateTask|SitemapMessage $message
  95.      *
  96.      * @throws \Throwable
  97.      */
  98.     public function handle($message): void
  99.     {
  100.         $sitemapRefreshStrategy $this->systemConfigService->getInt('core.sitemap.sitemapRefreshStrategy');
  101.         if ($sitemapRefreshStrategy !== SitemapExporterInterface::STRATEGY_SCHEDULED_TASK) {
  102.             return;
  103.         }
  104.         if ($message instanceof SitemapMessage) {
  105.             $this->generate($message);
  106.             return;
  107.         }
  108.         if ($message instanceof SitemapGenerateTask) {
  109.             parent::handle($message);
  110.             return;
  111.         }
  112.     }
  113.     private function generate(SitemapMessage $message): void
  114.     {
  115.         if ($message->getLastSalesChannelId() === null || $message->getLastLanguageId() === null) {
  116.             return;
  117.         }
  118.         $context $this->salesChannelContextFactory->create(''$message->getLastSalesChannelId(), [SalesChannelContextService::LANGUAGE_ID => $message->getLastLanguageId()]);
  119.         try {
  120.             $this->sitemapExporter->generate($contexttrue$message->getLastProvider(), $message->getNextOffset());
  121.         } catch (AlreadyLockedException $exception) {
  122.             $this->logger->error(sprintf('ERROR: %s'$exception->getMessage()));
  123.         }
  124.     }
  125. }