<?php
namespace App\Controller;
use App\Exception\LMDVException;
use App\Exception\NonTranslatedException;
use App\Service\SessionManagerLMDV;
use App\Service\WebServiceLMDVInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Psr\Log\LoggerInterface;
use App\Service\WebServiceLMDV;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\SessionManagerLMDVInterface;
/**
* Controller that handles the retreiving of PDF content.
*
* @author jra
*
*/
class PDFController extends AbstractController
{
use LMDVControllerTrait;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* @var \Symfony\Contracts\Translation\TranslatorInterface
*/
protected $translator;
/**
* @var \App\Service\SessionManagerLMDVInterface
*/
protected $sm;
public function __construct(LoggerInterface $logger, TranslatorInterface $translator, SessionManagerLMDVInterface $sm) {
$this->logger = $logger;
$this->translator = $translator;
$this->sm = $sm;
}
/**
* Gets the PDF file store in SalesForce using a WS request.
*
* @Route("/getPDF/{className}", name="getPDF")
*
* @param Request $request
* @param WebServiceLMDVInterface $ws
* @param string $className
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function getPDF(Request $request, WebServiceLMDVInterface $ws, string $className) {
$this->sm->setEntityNamespace('App\Entity\\'.$className);
$voyage = $this->sm->getDataFromSession();
try {
$result = $ws->getPdfVoyage($voyage);
}
catch(\Exception $e) {
return $this->redirectToErrorPage($e);
}
if(empty($result['content'])) {
$e = new \Exception($this->translator->trans("error.pdf.empty"));
return $this->redirectToErrorPage($e);
}
if($result['type'] == 'content') {
$idOppSF = $voyage->getIdOppSF();
if (empty($idOppSF)) {
$idOppSF = $voyage->getIdOppSFParent();
}
$filenamePdf = 'Voyage_' . $idOppSF . '.pdf';
return new Response($result['content'], 200, ['Content-Type' => 'application/pdf', 'Content-Disposition' => sprintf('attachment; filename="%s"', $filenamePdf)]);
}
elseif($result['type'] == 'url') {
return $this->redirect($result['content']);
}
}
}