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

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