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

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