src/Action/PressSite/Download/ProductTitlesAction.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Action\PressSite\Download;
  3. use App\Action\PressSite\DomainAwareAction;
  4. use App\Enum\PressSite\DomainLanguageEnum;
  5. use App\Service\PressSite\DomainAwareManager;
  6. use App\Service\PressSite\Download\VET\TableHelper;
  7. use App\Util\MappingConstants;
  8. use Sonata\Exporter\Writer\XlsWriter;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. use Symfony\Component\HttpFoundation\StreamedResponse;
  12. use Twig\Environment;
  13. /**
  14.  * Responsible for downloading the VET items table for given product.
  15.  */
  16. class ProductTitlesAction extends DomainAwareAction
  17. {
  18.     /**
  19.      * The custom helper service responsible for providing the data to export.
  20.      *
  21.      * @var \App\Service\PressSite\Download\VET\TableHelper
  22.      */
  23.     private $downloadHelper;
  24.     public function __construct(
  25.         Environment $twig,
  26.         DomainAwareManager $domainAwareManager,
  27.         TableHelper $downloadHelper
  28.     ) {
  29.         parent::__construct($twig$domainAwareManager);
  30.         $this->downloadHelper $downloadHelper;
  31.     }
  32.     /**
  33.      * Responsible for formatting the data for downloading the VET items.
  34.      *
  35.      * @param int $id
  36.      *   The ID of the currently rendered product
  37.      *
  38.      * @return \Symfony\Component\HttpFoundation\Response
  39.      *   The generated response object
  40.      */
  41.     public function __invoke(int $id): Response
  42.     {
  43.         $currentSite $this->getDomainManager()->getCurrentByHostnameAndLocale();
  44.         $locale $currentSite->isBroadcastType()
  45.             ? DomainLanguageEnum::DANISH
  46.             $currentSite->getLanguageCode();
  47.         $data $this->downloadHelper
  48.             ->setProductId($id)
  49.             ->setDomainId($currentSite->getId())
  50.             ->setIsBroadcastSite($currentSite->isBroadcastType())
  51.             ->setTitleLanguageCode(MappingConstants::getAlpha3CodeFromVodTitleLanguage($locale))
  52.             ->getExportData()
  53.         ;
  54.         $response = new StreamedResponse();
  55.         $response->setCallback(function () use ($data) {
  56.             $writer = new XlsWriter('php://output'false);
  57.             $writer->open();
  58.             $writer->write($data['headers']);
  59.             foreach ($data['collection'] as $row) {
  60.                 $writer->write($row);
  61.             }
  62.             $writer->close();
  63.         });
  64.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  65.         $response->headers->set('Content-Disposition'$response->headers->makeDisposition(
  66.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  67.             "{$id}_table.xls"
  68.         ));
  69.         return $response;
  70.     }
  71. }