custom/plugins/CrehlerProductExportScheduler/src/Subscriber/ProductExportSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\ProductExportScheduler\Subscriber;
  3. use Crehler\ProductExportScheduler\DataAbstractionLayer\Entity\ProductExportLog\ProductExportLogDefinition;
  4. use Crehler\ProductExportScheduler\Service\ProductExportLogService;
  5. use League\Flysystem\FilesystemInterface;
  6. use Shopware\Core\Content\ProductExport\Service\ProductExportFileHandlerInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductExportSubscriber implements EventSubscriberInterface
  13. {
  14.     protected EntityRepositoryInterface $productExportRepository;
  15.     protected ProductExportFileHandlerInterface $productExportFileHandler;
  16.     protected FilesystemInterface $fileSystem;
  17.     protected ProductExportLogService $productExportLogService;
  18.     public function __construct(
  19.         EntityRepositoryInterface $productExportRepository,
  20.         ProductExportFileHandlerInterface $productExportFileHandler,
  21.         ProductExportLogService $productExportLogService,
  22.         FilesystemInterface $fileSystem
  23.     ) {
  24.         $this->productExportRepository $productExportRepository;
  25.         $this->productExportFileHandler $productExportFileHandler;
  26.         $this->productExportLogService $productExportLogService;
  27.         $this->fileSystem $fileSystem;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             'product_export.written' => 'onProductExportWritten',
  33.         ];
  34.     }
  35.     public function onProductExportWritten(EntityWrittenEvent $event)
  36.     {
  37.         foreach ($event->getWriteResults() as $writeResult) {
  38.             if (!$this->productExportWritten($writeResult)) {
  39.                 continue;
  40.             }
  41.             $primaryKey $writeResult->getPrimaryKey();
  42.             $primaryKey \is_array($primaryKey) ? $primaryKey['id'] : $primaryKey;
  43.             $productExportResult $this->productExportRepository->search(new Criteria([$primaryKey]), $event->getContext());
  44.             if ($productExportResult->getTotal() !== 0) {
  45.                 $productExport $productExportResult->first();
  46.                 $filePath $this->productExportFileHandler->getFilePath($productExport);
  47.                 try {
  48.                     $size $this->fileSystem->getSize($filePath);
  49.                 } catch (\Throwable $e) {
  50.                     $size 0;
  51.                 }
  52.                 $this->productExportLogService->log(
  53.                     $productExport->getId(),
  54.                     ProductExportLogDefinition::DONE_STATUS,
  55.                     $size
  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.             && $writeResult->getPayload()['generatedAt'] !== null;
  66.     }
  67. }