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

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