custom/plugins/UandiEfbErpSynchronization/src/Storefront/Controller/ErpDocumentListController.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbErpSynchronization\Storefront\Controller;
  3. use Psr\Container\ContainerExceptionInterface;
  4. use Psr\Container\NotFoundExceptionInterface;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Content\ImportExport\Exception\FileNotFoundException;
  9. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  10. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Controller\StorefrontController;
  13. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  14. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  18. use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Uandi\UandiEfbErpSynchronization\Service\Document\File;
  21. use Uandi\UandiEfbErpSynchronization\Service\Document\Lists\DocumentListServiceInterface;
  22. use Uandi\UandiEfbErpSynchronization\Service\Document\Lists\ExistingOrdersPdfService;
  23. use Uandi\UandiEfbErpSynchronization\Service\Document\Lists\ListingServiceFactory;
  24. /**
  25.  * @RouteScope(scopes={"storefront"})
  26.  */
  27. class ErpDocumentListController extends StorefrontController
  28. {
  29.     private File $fileService;
  30.     private ListingServiceFactory $listingServiceFactory;
  31.     private GenericPageLoaderInterface $pageLoader;
  32.     private LoggerInterface $logger;
  33.     public function __construct(
  34.         File $fileService,
  35.         ListingServiceFactory $listingServiceFactory,
  36.         GenericPageLoaderInterface $pageLoader,
  37.         LoggerInterface $logger
  38.     ) {
  39.         $this->fileService $fileService;
  40.         $this->listingServiceFactory $listingServiceFactory;
  41.         $this->pageLoader $pageLoader;
  42.         $this->logger $logger;
  43.     }
  44.     /**
  45.      * @LoginRequired()
  46.      * @Route("/documentList/{type}", name="frontend.erpdocuments.page", methods={"GET"})
  47.      *
  48.      * @throws CustomerNotLoggedInException
  49.      */
  50.     public function index(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  51.     {
  52.         $type $request->get('type');
  53.         $request->query->set('customerNumber'$customer->getCustomerNumber());
  54.         $listingService $this->getListingService($type);
  55.         $fileList $listingService->fetchDocumentList($request$context);
  56.         if ($type == ExistingOrdersPdfService::DOCUMENT_TYPE_TEXT) {
  57.             return $this->getExistingOrdersResponse($fileList$customer$type);
  58.         }
  59.         $mode 'standard';
  60.         if ($this->isB2b()) {
  61.             $mode 'b2bsuite';
  62.         }
  63.         $page $this->pageLoader->load($request$context);
  64.         return $this->renderStorefront(sprintf('@UandiEfbErpSynchronization/storefront/page/erp-documents/%s-wrapper.html.twig'$mode), [
  65.             'page' => $page,
  66.             'type' => $type,
  67.             'availableErpDocumentTypes' => $this->listingServiceFactory->getAvailableTypes(),
  68.             'availableFilters' => $listingService->getAvailableFilters(),
  69.             'searchResult' => $fileList,
  70.         ]);
  71.     }
  72.     /**
  73.      * @LoginRequired()
  74.      * @Route("/documentList/download/{type}/{id}", name="frontend.erpdocuments.download", methods={"GET"})
  75.      *
  76.      * @throws CustomerNotLoggedInException
  77.      */
  78.     public function download(Request $requestCustomerEntity $customerSalesChannelContext $salesChannelContext): Response
  79.     {
  80.         $file $this->fileService->getFile(
  81.             $salesChannelContext,
  82.             $customer->getCustomerNumber(),
  83.             $request->get('id'),
  84.             $request->get('type')
  85.         );
  86.         if (empty($file)) {
  87.             throw new FileNotFoundException($request->get('id'));
  88.         }
  89.         return $this->createDownloadResponse($file['tempFile'], $file['fileName']);
  90.     }
  91.     /**
  92.      * Get the required service from the factory.
  93.      *
  94.      * @param string $type
  95.      *
  96.      * @return DocumentListServiceInterface|null
  97.      */
  98.     private function getListingService(string $type): ?DocumentListServiceInterface
  99.     {
  100.         if (false === $this->listingServiceFactory->has($type)) {
  101.             throw $this->createNotFoundException();
  102.         }
  103.         return $this->listingServiceFactory->create($type);
  104.     }
  105.     /**
  106.      * @return bool
  107.      */
  108.     private function isB2b(): bool
  109.     {
  110.         try {
  111.             return $this->container->get('b2b_front_auth.authentication_service')->isB2b();
  112.         } catch (NotFoundExceptionInterface ContainerExceptionInterface $exception) {
  113.             return false;
  114.         }
  115.     }
  116.     /**
  117.      * For the supplied filenames (stored file / display to the user) create an appropriate download response.
  118.      *
  119.      * @param string $filePath
  120.      * @param string $frontendFileName
  121.      *
  122.      * @return BinaryFileResponse
  123.      */
  124.     private function createDownloadResponse(string $filePathstring $frontendFileName): BinaryFileResponse
  125.     {
  126.         $response = new BinaryFileResponse($filePath);
  127.         $mimeTypeGuesser = new FileinfoMimeTypeGuesser();
  128.         $response->headers->set('Content-Type'$mimeTypeGuesser->guessMimeType($filePath));
  129.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$frontendFileName);
  130.         return $response;
  131.     }
  132.     /**
  133.      * Handle the existing orders response.
  134.      *
  135.      * @param array $fileList
  136.      * @param CustomerEntity $customer
  137.      * @param string $type
  138.      *
  139.      * @return BinaryFileResponse
  140.      */
  141.     public function getExistingOrdersResponse(array $fileListCustomerEntity $customerstring $type): BinaryFileResponse
  142.     {
  143.         if (!array_key_exists('base64_data_stream'$fileList)) {
  144.             $this->logger->error(
  145.                 sprintf(
  146.                     '%s: Customer %s: %s: no stream on API response (response: %s)',
  147.                     __METHOD__,
  148.                     $customer->getCustomerNumber(),
  149.                     $type,
  150.                     print_r($fileListtrue)
  151.                 )
  152.             );
  153.             throw $this->createNotFoundException();
  154.         }
  155.         $fileName $this->fileService->storePdf($fileList);
  156.         return $this->createDownloadResponse($fileName'Auftragsbestandsliste.pdf');
  157.     }
  158. }