<?php
namespace App\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Swagger\Annotations as SWG;
use App\Form\MddPdfDataType;
use App\Entity\MddPdfData;
use Nelmio\ApiDocBundle\Annotation as ApiDoc;
use App\Service\MddPdfCreator;
use App\Service\SignatureCreator;
use FOS\RestBundle\Controller\Annotations as Rest;
use App\Service\MddMailerService;
use App\Entity\MddSentEmail;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Service\TokenGenerator;
class MddController extends AbstractFOSRestController
{
const TRACK_ID = '328c2043-c0cb-46f7-a070-4b02d56e0a26';
private $mddPdfCreator;
private $mddMailerService;
public function __construct(
MddPdfCreator $mddPdfCreator,
MddMailerService $mddMailerService
) {
$this->mddPdfCreator = $mddPdfCreator;
$this->mddMailerService = $mddMailerService;
}
/**
* @SWG\Tag(name="Mdd")
* @ApiDoc\Operation(
* consumes={"application/x-www-form-urlencoded"}
* )
* @SWG\Response(
* response=201,
* description="Returned on successful mdd module1 pdf sending",
* )
* @SWG\Response(
* response=401,
* description="Returned when appClientTrackUuid is not valid",
* )
* @SWG\Parameter(name="appClientTrackUuid", in="formData", type="string")
* @SWG\Parameter(name="qualitaDi", in="formData", type="string")
* @SWG\Parameter(name="nomeCognome", in="formData", type="string")
* @SWG\Parameter(name="imgURL", in="formData", type="string")
* @SWG\Parameter(name="autorizzo", in="formData", type="string")
* @SWG\Parameter(name="sender", in="formData", type="string")
* @SWG\Parameter(name="accEmail", in="formData", type="string")
*
* @Route("/api/mdd/sendPdf", name="api_mdd_send_pdf", methods={"POST"})
* @Rest\View()
*/
public function sendPdf(Request $request)
{
$mddPdfData = new MddPdfData();
$form = $this->createForm(MddPdfDataType::class, $mddPdfData);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($mddPdfData->getAppClientTrackUuid() != self::TRACK_ID) {
throw new HttpException(Response::HTTP_UNAUTHORIZED, 'invalid appClientTrackUuid');
}
if (!$mddPdfData->getImgURL()) {
throw new HttpException(Response::HTTP_BAD_REQUEST, 'no signature specified');
}
// 1st create pdf
$pdfBasename = Uuid::uuid4() . '.pdf';
$pdfFilename = $this->mddPdfCreator->createPdf($mddPdfData, $pdfBasename);
// 2nd send mail with pdf attachment
$this->mddMailerService->sendMail($mddPdfData, $pdfFilename);
$this->createSentEmail($mddPdfData, $pdfBasename);
$this->mddPdfCreator->deleteSignature($mddPdfData);
return $this->handleView($this->view([], Response::HTTP_CREATED));
}
return $this->handleView($this->view([], Response::HTTP_BAD_REQUEST));
}
protected function createSentEmail(MddPdfData $mddPdfData, ?string $pdfBasename): void
{
$sentEmail = new MddSentEmail();
$sentEmail->setSender($mddPdfData->getSender());
$sentEmail->setName($mddPdfData->getNomeCognome());
$sentEmail->setRecipient($mddPdfData->getAccEmail());
$sentEmail->setSubject('Modulo Raccolta Consensi MDD');
$sentEmail->setConsent($mddPdfData->getAutorizzo() == 'autorizzo' ? true : false);
$sentEmail->setPdf($pdfBasename);
$mddPdfData->setSentEmail($sentEmail);
$this->getDoctrine()->getManager()->persist($mddPdfData);
$this->getDoctrine()->getManager()->flush();
}
}