vendor/shopware/core/Content/ProductExport/SalesChannel/ExportController.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ProductExport\SalesChannel;
  3. use League\Flysystem\FilesystemInterface;
  4. use Monolog\Logger;
  5. use Shopware\Core\Content\ProductExport\Event\ProductExportContentTypeEvent;
  6. use Shopware\Core\Content\ProductExport\Event\ProductExportLoggingEvent;
  7. use Shopware\Core\Content\ProductExport\Exception\ExportNotFoundException;
  8. use Shopware\Core\Content\ProductExport\Exception\ExportNotGeneratedException;
  9. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  10. use Shopware\Core\Content\ProductExport\Service\ProductExporterInterface;
  11. use Shopware\Core\Content\ProductExport\Service\ProductExportFileHandlerInterface;
  12. use Shopware\Core\Content\ProductExport\Struct\ExportBehavior;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Feature;
  18. use Shopware\Core\Framework\Log\Package;
  19. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  20. use Shopware\Core\Framework\Routing\Annotation\Since;
  21. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  22. use Shopware\Storefront\Event\ProductExportContentTypeEvent as StorefrontProductExportContentTypeEvent;
  23. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. /**
  28.  * @Route(defaults={"_routeScope"={"store-api"}})
  29.  */
  30. #[Package('inventory')]
  31. class ExportController
  32. {
  33.     /**
  34.      * @var ProductExporterInterface
  35.      */
  36.     private $productExportService;
  37.     /**
  38.      * @var FilesystemInterface
  39.      */
  40.     private $fileSystem;
  41.     /**
  42.      * @var EventDispatcherInterface
  43.      */
  44.     private $eventDispatcher;
  45.     /**
  46.      * @var EntityRepositoryInterface
  47.      */
  48.     private $productExportRepository;
  49.     /**
  50.      * @var ProductExportFileHandlerInterface
  51.      */
  52.     private $productExportFileHandler;
  53.     /**
  54.      * @var AbstractSalesChannelContextFactory
  55.      */
  56.     private $contextFactory;
  57.     /**
  58.      * @internal
  59.      */
  60.     public function __construct(
  61.         ProductExporterInterface $productExportService,
  62.         ProductExportFileHandlerInterface $productExportFileHandler,
  63.         FilesystemInterface $fileSystem,
  64.         EventDispatcherInterface $eventDispatcher,
  65.         EntityRepositoryInterface $productExportRepository,
  66.         AbstractSalesChannelContextFactory $contextFactory
  67.     ) {
  68.         $this->productExportService $productExportService;
  69.         $this->productExportFileHandler $productExportFileHandler;
  70.         $this->fileSystem $fileSystem;
  71.         $this->eventDispatcher $eventDispatcher;
  72.         $this->productExportRepository $productExportRepository;
  73.         $this->contextFactory $contextFactory;
  74.     }
  75.     /**
  76.      * @Since("6.3.2.0")
  77.      * @Route("/store-api/product-export/{accessKey}/{fileName}", name="store-api.product.export", methods={"GET"}, defaults={"auth_required"=false})
  78.      */
  79.     public function index(Request $request): Response
  80.     {
  81.         $criteria = new Criteria();
  82.         $criteria
  83.             ->addFilter(new EqualsFilter('fileName'$request->get('fileName')))
  84.             ->addFilter(new EqualsFilter('accessKey'$request->get('accessKey')))
  85.             ->addFilter(new EqualsFilter('salesChannel.active'true))
  86.             ->addAssociation('salesChannelDomain');
  87.         /** @var ProductExportEntity|null $productExport */
  88.         $productExport $this->productExportRepository->search($criteriaContext::createDefaultContext())->first();
  89.         if ($productExport === null) {
  90.             $exportNotFoundException = new ExportNotFoundException(null$request->get('fileName'));
  91.             $this->logException(Context::createDefaultContext(), $exportNotFoundException);
  92.             throw $exportNotFoundException;
  93.         }
  94.         $context $this->contextFactory->create(''$productExport->getSalesChannelDomain()->getSalesChannelId());
  95.         $filePath $this->productExportFileHandler->getFilePath($productExport);
  96.         // if file not present or interval = live
  97.         if (!$this->fileSystem->has($filePath) || $productExport->getInterval() === 0) {
  98.             $this->productExportService->export($context, new ExportBehavior(), $productExport->getId());
  99.         }
  100.         if (!$this->fileSystem->has($filePath)) {
  101.             $exportNotGeneratedException = new ExportNotGeneratedException();
  102.             $this->logException($context->getContext(), $exportNotGeneratedException);
  103.             throw $exportNotGeneratedException;
  104.         }
  105.         $content $this->fileSystem->read($filePath);
  106.         $contentType $this->getContentType($productExport->getFileFormat());
  107.         $encoding $productExport->getEncoding();
  108.         return (new Response($content $content null200, ['Content-Type' => $contentType ';charset=' $encoding]))
  109.             ->setCharset($encoding);
  110.     }
  111.     private function getContentType(string $fileFormat): string
  112.     {
  113.         $contentType 'text/plain';
  114.         switch ($fileFormat) {
  115.             case ProductExportEntity::FILE_FORMAT_CSV:
  116.                 $contentType 'text/csv';
  117.                 break;
  118.             case ProductExportEntity::FILE_FORMAT_XML:
  119.                 $contentType 'text/xml';
  120.                 break;
  121.         }
  122.         if (!Feature::isActive('v6.5.0.0') && \class_exists(StorefrontProductExportContentTypeEvent::class)) {
  123.             $event = new StorefrontProductExportContentTypeEvent($fileFormat$contentType);
  124.             $this->eventDispatcher->dispatch($event);
  125.         }
  126.         $event = new ProductExportContentTypeEvent($fileFormat$contentType);
  127.         $this->eventDispatcher->dispatch($event);
  128.         return $event->getContentType();
  129.     }
  130.     private function logException(
  131.         Context $context,
  132.         \Exception $exception
  133.     ): void {
  134.         $loggingEvent = new ProductExportLoggingEvent(
  135.             $context,
  136.             $exception->getMessage(),
  137.             Logger::ERROR,
  138.             $exception
  139.         );
  140.         $this->eventDispatcher->dispatch($loggingEvent);
  141.     }
  142. }