src/EventSubscriber/TranscriptOfRecordsWriteSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\TranscriptOfRecords;
  5. use App\Pdf\Pdf;
  6. use JetBrains\PhpStorm\ArrayShape;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class TranscriptOfRecordsWriteSubscriber implements EventSubscriberInterface
  12. {
  13.     private Pdf $pdf;
  14.     public function __construct(
  15.         Pdf $pdf
  16.     ) {
  17.         $this->pdf $pdf;
  18.     }
  19.     #[ArrayShape([KernelEvents::VIEW => 'array'])]
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::VIEW => [
  24.                 'generateToRPdf'EventPriorities::PRE_WRITE,
  25.             ],
  26.         ];
  27.     }
  28.     public function generateToRPdf(ViewEvent $viewEvent): void
  29.     {
  30.         $entity $viewEvent->getControllerResult();
  31.         $method $viewEvent->getRequest()->getMethod();
  32.         if (
  33.             !$entity instanceof TranscriptOfRecords
  34.             || !in_array(
  35.                 $method,
  36.                 [
  37.                     Request::METHOD_POST,
  38.                     Request::METHOD_PATCH,
  39.                     Request::METHOD_PUT,
  40.                 ]
  41.             )
  42.         ) {
  43.             return;
  44.         }
  45.         $pdfName 'Transcript of Records - '.$entity->getMobility()->getId().'.pdf';
  46.         $pdfPathSubDirectory 'transcript_of_records';
  47.         // Generate PDF according to ToR's data
  48.         $relativePathWithName $this->pdf->generateFromHtml(
  49.             $pdfName,
  50.             $pdfPathSubDirectory,
  51.             'pdf/transcript_of_records_pdf.html.twig',
  52.             [
  53.                 'transcriptOfRecords' => $entity,
  54.             ]
  55.         );
  56.         // Save PDF url to ToR if changed
  57.         $entity->setPdf($relativePathWithName);
  58.         // Generate Credits Recognition PDF
  59.         $pdfNameCreditsRecognition 'Credits transfer sheet - '.$entity->getMobility()->getId().'.pdf';
  60.         $pdfPathSubDirectoryCreditsRecognition 'credits_recognition';
  61.         $relativePathWithNameCreditsRecognition $this->pdf->generateFromHtml(
  62.             $pdfNameCreditsRecognition,
  63.             $pdfPathSubDirectoryCreditsRecognition,
  64.             'pdf/credits_recognition_pdf.html.twig',
  65.             [
  66.                 'transcriptOfRecords' => $entity,
  67.             ]
  68.         );
  69.         // Save PDF url to ToR if changed
  70.         $entity->getMobility()->setCreditsRecognitionPdf($relativePathWithNameCreditsRecognition);
  71.     }
  72. }