vendor/shopware/core/Content/ProductExport/EventListener/ProductExportEventListener.php line 37

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ProductExport\EventListener;
  3. use League\Flysystem\FilesystemOperator;
  4. use Shopware\Core\Content\ProductExport\Service\ProductExportFileHandlerInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('sales-channel')]
  15. class ProductExportEventListener implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(
  21.         private readonly EntityRepository $productExportRepository,
  22.         private readonly ProductExportFileHandlerInterface $productExportFileHandler,
  23.         private readonly FilesystemOperator $fileSystem
  24.     ) {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             'product_export.written' => 'afterWrite',
  30.         ];
  31.     }
  32.     public function afterWrite(EntityWrittenEvent $event): void
  33.     {
  34.         foreach ($event->getWriteResults() as $writeResult) {
  35.             if (!$this->productExportWritten($writeResult)) {
  36.                 continue;
  37.             }
  38.             $primaryKey $writeResult->getPrimaryKey();
  39.             $primaryKey \is_array($primaryKey) ? $primaryKey['id'] : $primaryKey;
  40.             $this->productExportRepository->update(
  41.                 [
  42.                     [
  43.                         'id' => $primaryKey,
  44.                         'generatedAt' => null,
  45.                     ],
  46.                 ],
  47.                 $event->getContext()
  48.             );
  49.             $productExportResult $this->productExportRepository->search(new Criteria([$primaryKey]), $event->getContext());
  50.             if ($productExportResult->getTotal() !== 0) {
  51.                 $productExport $productExportResult->first();
  52.                 $filePath $this->productExportFileHandler->getFilePath($productExport);
  53.                 if ($this->fileSystem->fileExists($filePath)) {
  54.                     $this->fileSystem->delete($filePath);
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     private function productExportWritten(EntityWriteResult $writeResult): bool
  60.     {
  61.         return $writeResult->getEntityName() === 'product_export'
  62.             && $writeResult->getOperation() !== EntityWriteResult::OPERATION_DELETE
  63.             && !\array_key_exists('generatedAt'$writeResult->getPayload());
  64.     }
  65. }