src/Controller/PDFController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Exception\LMDVException;
  4. use App\Exception\NonTranslatedException;
  5. use App\Service\SessionManagerLMDV;
  6. use App\Service\WebServiceLMDVInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Psr\Log\LoggerInterface;
  12. use App\Service\WebServiceLMDV;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use App\Service\SessionManagerLMDVInterface;
  15. /**
  16.  * Controller that handles the retreiving of PDF content.
  17.  *
  18.  * @author jra
  19.  *
  20.  */
  21. class PDFController extends AbstractController
  22. {
  23.   use LMDVControllerTrait;
  24.   
  25.   /**
  26.    * @var \Psr\Log\LoggerInterface
  27.    */
  28.   protected $logger;
  29.   /**
  30.    * @var \Symfony\Contracts\Translation\TranslatorInterface
  31.    */
  32.   protected $translator;
  33.   /**
  34.    * @var \App\Service\SessionManagerLMDVInterface
  35.    */
  36.   protected $sm;
  37.   public function __construct(LoggerInterface $loggerTranslatorInterface $translatorSessionManagerLMDVInterface $sm) {
  38.     $this->logger $logger;
  39.     $this->translator $translator;
  40.     $this->sm $sm;
  41.   }
  42.   /**
  43.    * Gets the PDF file store in SalesForce using a WS request.
  44.    *
  45.    * @Route("/getPDF/{className}", name="getPDF")
  46.    *
  47.    * @param Request $request
  48.    * @param WebServiceLMDVInterface $ws
  49.    * @param string $className
  50.    * @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\RedirectResponse
  51.    */
  52.   public function getPDF(Request $requestWebServiceLMDVInterface $wsstring $className) {
  53.     $this->sm->setEntityNamespace('App\Entity\\'.$className);
  54.     $voyage $this->sm->getDataFromSession();
  55.     try {
  56.       $result $ws->getPdfVoyage($voyage);
  57.     }
  58.     catch(\Exception $e) {
  59.       return $this->redirectToErrorPage($e);
  60.     }
  61.     if(empty($result['content'])) {
  62.       $e = new \Exception($this->translator->trans("error.pdf.empty"));
  63.       return $this->redirectToErrorPage($e);
  64.     }
  65.     if($result['type'] == 'content') {
  66.       $idOppSF $voyage->getIdOppSF();
  67.       if (empty($idOppSF)) {
  68.           $idOppSF $voyage->getIdOppSFParent();
  69.       }
  70.       $filenamePdf 'Voyage_' $idOppSF '.pdf';
  71.       return new Response($result['content'], 200, ['Content-Type' => 'application/pdf''Content-Disposition' => sprintf('attachment; filename="%s"'$filenamePdf)]);
  72.     }
  73.     elseif($result['type'] == 'url') {
  74.       return $this->redirect($result['content']);
  75.     }
  76.   }
  77. }