vendor/store.shopware.com/watodigitecgalaxusexport/src/Subscriber/ProductExportWrittenSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wato\DigitecGalaxusExport\Subscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Wato\DigitecGalaxusExport\Helper\SFTPFileHelper;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Psr\Log\LoggerInterface;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. class ProductExportWrittenSubscriber implements EventSubscriberInterface {
  14.     public function __construct(EntityRepositoryInterface $productExportRepoSFTPFileHelper $sftpLoggerInterface $loggerSystemConfigService $systemConfigService)
  15.     {
  16.         $this->productExportRepo $productExportRepo;
  17.         $this->sftp $sftp;
  18.         $this->logger $logger;
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             'product_export.written' => 'afterWrite',
  25.         ];
  26.     }
  27.     public function afterWrite(EntityWrittenEvent $event): void
  28.     {
  29.         $context $event->getContext();
  30.         $user $this->systemConfigService->get('DigitecGalaxusExport.config.ftpUser');
  31.         $pw $this->systemConfigService->get('DigitecGalaxusExport.config.ftpPassword');
  32.         $server $this->systemConfigService->get('DigitecGalaxusExport.config.ftpServer');
  33.         $directory $this->systemConfigService->get('DigitecGalaxusExport.config.articleDataDirectory');
  34.         foreach ($event->getPayloads() as $payload)
  35.         {
  36.             $id $payload['id'];
  37.             $filename $this->productExportRepo->search(new Criteria([$id]), $context)->first()->fileName;
  38.             if (str_starts_with($filename'Product'))
  39.             {
  40.                 try {
  41.                     $this->sftp->deployFile($filename$user$pw$server$directory);
  42.                 } catch (\Throwable $t){
  43.                     $this->logger->error(date('D M d, Y G:i') . ': ' $t->getMessage() . ' Line: ' $t->getLine() . ' File: ' $t->getFile() . PHP_EOL);
  44.                     continue;
  45.                 }
  46.             } 
  47.         }
  48.     }
  49.     private function productExportWritten(EntityWriteResult $writeResult): bool
  50.     {
  51.         return $writeResult->getEntityName() === 'product_export'
  52.             && $writeResult->getOperation() !== EntityWriteResult::OPERATION_DELETE
  53.             && !\array_key_exists('generatedAt'$writeResult->getPayload());
  54.     }
  55. }