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

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