src/Controller/MddController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use FOS\RestBundle\Controller\AbstractFOSRestController;
  4. use Ramsey\Uuid\Uuid;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Swagger\Annotations as SWG;
  8. use App\Form\MddPdfDataType;
  9. use App\Entity\MddPdfData;
  10. use Nelmio\ApiDocBundle\Annotation as ApiDoc;
  11. use App\Service\MddPdfCreator;
  12. use App\Service\SignatureCreator;
  13. use FOS\RestBundle\Controller\Annotations as Rest;
  14. use App\Service\MddMailerService;
  15. use App\Entity\MddSentEmail;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\HttpKernel\Exception\HttpException;
  19. use App\Service\TokenGenerator;
  20. class MddController extends AbstractFOSRestController
  21. {
  22.     const TRACK_ID '328c2043-c0cb-46f7-a070-4b02d56e0a26';
  23.     private $mddPdfCreator;
  24.     private $mddMailerService;
  25.     public function __construct(
  26.         MddPdfCreator $mddPdfCreator,
  27.         MddMailerService $mddMailerService
  28.     ) {
  29.         $this->mddPdfCreator $mddPdfCreator;
  30.         $this->mddMailerService $mddMailerService;
  31.     }
  32.     /**
  33.      * @SWG\Tag(name="Mdd")
  34.      * @ApiDoc\Operation(
  35.      *     consumes={"application/x-www-form-urlencoded"}
  36.      * )
  37.      * @SWG\Response(
  38.      *     response=201,
  39.      *     description="Returned on successful mdd module1 pdf sending",
  40.      * )
  41.      * @SWG\Response(
  42.      *     response=401,
  43.      *     description="Returned when appClientTrackUuid is not valid",
  44.      * )
  45.      * @SWG\Parameter(name="appClientTrackUuid", in="formData", type="string")
  46.      * @SWG\Parameter(name="qualitaDi", in="formData", type="string")
  47.      * @SWG\Parameter(name="nomeCognome", in="formData", type="string")
  48.      * @SWG\Parameter(name="imgURL", in="formData", type="string")
  49.      * @SWG\Parameter(name="autorizzo", in="formData", type="string")
  50.      * @SWG\Parameter(name="sender", in="formData", type="string")
  51.      * @SWG\Parameter(name="accEmail", in="formData", type="string")
  52.      *
  53.      * @Route("/api/mdd/sendPdf", name="api_mdd_send_pdf",  methods={"POST"})
  54.      * @Rest\View()
  55.      */
  56.     public function sendPdf(Request $request)
  57.     {
  58.         $mddPdfData = new MddPdfData();
  59.         $form $this->createForm(MddPdfDataType::class, $mddPdfData);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             if ($mddPdfData->getAppClientTrackUuid() != self::TRACK_ID) {
  63.                 throw new HttpException(Response::HTTP_UNAUTHORIZED'invalid appClientTrackUuid');
  64.             }
  65.             if (!$mddPdfData->getImgURL()) {
  66.                 throw new HttpException(Response::HTTP_BAD_REQUEST'no signature specified');
  67.             }
  68.             // 1st create pdf
  69.             $pdfBasename Uuid::uuid4() . '.pdf';
  70.             $pdfFilename $this->mddPdfCreator->createPdf($mddPdfData$pdfBasename);
  71.             // 2nd send mail with pdf attachment
  72.             $this->mddMailerService->sendMail($mddPdfData$pdfFilename);
  73.             $this->createSentEmail($mddPdfData$pdfBasename);
  74.             $this->mddPdfCreator->deleteSignature($mddPdfData);
  75.             return $this->handleView($this->view([], Response::HTTP_CREATED));
  76.         }
  77.         return $this->handleView($this->view([], Response::HTTP_BAD_REQUEST));
  78.     }
  79.     protected function createSentEmail(MddPdfData $mddPdfData, ?string $pdfBasename): void
  80.     {
  81.         $sentEmail = new MddSentEmail();
  82.         $sentEmail->setSender($mddPdfData->getSender());
  83.         $sentEmail->setName($mddPdfData->getNomeCognome());
  84.         $sentEmail->setRecipient($mddPdfData->getAccEmail());
  85.         $sentEmail->setSubject('Modulo Raccolta Consensi MDD');
  86.         $sentEmail->setConsent($mddPdfData->getAutorizzo() == 'autorizzo' true false);
  87.         $sentEmail->setPdf($pdfBasename);
  88.         $mddPdfData->setSentEmail($sentEmail);
  89.         $this->getDoctrine()->getManager()->persist($mddPdfData);
  90.         $this->getDoctrine()->getManager()->flush();
  91.     }
  92. }