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

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