vendor/shopware/core/Content/ProductExport/ScheduledTask/ProductExportGenerateTaskHandler.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ProductExport\ScheduledTask;
  3. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. /**
  16.  * @deprecated tag:v6.5.0 - reason:becomes-internal - MessageHandler will be internal and final starting with v6.5.0.0
  17.  */
  18. #[Package('inventory')]
  19. class ProductExportGenerateTaskHandler extends ScheduledTaskHandler
  20. {
  21.     /**
  22.      * @var AbstractSalesChannelContextFactory
  23.      */
  24.     private $salesChannelContextFactory;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $salesChannelRepository;
  29.     /**
  30.      * @var EntityRepositoryInterface
  31.      */
  32.     private $productExportRepository;
  33.     /**
  34.      * @var MessageBusInterface
  35.      */
  36.     private $messageBus;
  37.     /**
  38.      * @internal
  39.      */
  40.     public function __construct(
  41.         EntityRepositoryInterface $scheduledTaskRepository,
  42.         AbstractSalesChannelContextFactory $salesChannelContextFactory,
  43.         EntityRepositoryInterface $salesChannelRepository,
  44.         EntityRepositoryInterface $productExportRepository,
  45.         MessageBusInterface $messageBus
  46.     ) {
  47.         parent::__construct($scheduledTaskRepository);
  48.         $this->salesChannelContextFactory $salesChannelContextFactory;
  49.         $this->salesChannelRepository $salesChannelRepository;
  50.         $this->productExportRepository $productExportRepository;
  51.         $this->messageBus $messageBus;
  52.     }
  53.     /**
  54.      * @return iterable<class-string>
  55.      */
  56.     public static function getHandledMessages(): iterable
  57.     {
  58.         return [
  59.             ProductExportGenerateTask::class,
  60.         ];
  61.     }
  62.     public function run(): void
  63.     {
  64.         $salesChannelIds $this->fetchSalesChannelIds();
  65.         foreach ($salesChannelIds as $salesChannelId) {
  66.             $productExports $this->fetchProductExports($salesChannelId);
  67.             if (\count($productExports) === 0) {
  68.                 continue;
  69.             }
  70.             $now = new \DateTimeImmutable('now');
  71.             foreach ($productExports as $productExport) {
  72.                 if (!$this->shouldBeRun($productExport$now)) {
  73.                     continue;
  74.                 }
  75.                 $this->messageBus->dispatch(
  76.                     new ProductExportPartialGeneration($productExport->getId(), $salesChannelId)
  77.                 );
  78.             }
  79.         }
  80.     }
  81.     /**
  82.      * @return array<string>
  83.      */
  84.     private function fetchSalesChannelIds(): array
  85.     {
  86.         $criteria = new Criteria();
  87.         $criteria
  88.             ->addFilter(new EqualsFilter('typeId'Defaults::SALES_CHANNEL_TYPE_STOREFRONT))
  89.             ->addFilter(new EqualsFilter('active'true));
  90.         /**
  91.          * @var array<string>
  92.          */
  93.         return $this->salesChannelRepository
  94.             ->searchIds($criteriaContext::createDefaultContext())
  95.             ->getIds();
  96.     }
  97.     /**
  98.      * @return array<ProductExportEntity>
  99.      */
  100.     private function fetchProductExports(string $salesChannelId): array
  101.     {
  102.         $salesChannelContext $this->salesChannelContextFactory->create(Uuid::randomHex(), $salesChannelId);
  103.         $criteria = new Criteria();
  104.         $criteria
  105.             ->addAssociation('salesChannel')
  106.             ->addFilter(
  107.                 new MultiFilter(
  108.                     'AND',
  109.                     [
  110.                         new EqualsFilter('generateByCronjob'true),
  111.                         new EqualsFilter('salesChannel.active'true),
  112.                     ]
  113.                 )
  114.             )
  115.             ->addFilter(
  116.                 new MultiFilter(
  117.                     'OR',
  118.                     [
  119.                         new EqualsFilter('storefrontSalesChannelId'$salesChannelId),
  120.                         new EqualsFilter('salesChannelDomain.salesChannel.id'$salesChannelId),
  121.                     ]
  122.                 )
  123.             );
  124.         /**
  125.          * @var array<ProductExportEntity>
  126.          */
  127.         return $this->productExportRepository->search($criteria$salesChannelContext->getContext())->getElements();
  128.     }
  129.     private function shouldBeRun(ProductExportEntity $productExport\DateTimeImmutable $now): bool
  130.     {
  131.         if ($productExport->getIsRunning()) {
  132.             return false;
  133.         }
  134.         if ($productExport->getGeneratedAt() === null) {
  135.             return true;
  136.         }
  137.         return $now->getTimestamp() - $productExport->getGeneratedAt()->getTimestamp() >= $productExport->getInterval();
  138.     }
  139. }