vendor/bluue/sales-bundle/src/EventSubscriber/PublicDocumentWithEntityEventSubscriber.php line 89

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\SalesBundle\EventSubscriber;
  9. use Symfony\Component\Uid\UuidV6;
  10. use App\Event\PublicDocumentWithEntityEvent;
  11. use App\Repository\FileManagerRepository;
  12. use Bluue\SalesBundle\Repository\CreditNoteRepository;
  13. use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
  14. use Bluue\SalesBundle\Repository\InvoiceRepository;
  15. use Bluue\SalesBundle\Repository\OrderRepository;
  16. use Bluue\SalesBundle\Repository\QuotationRepository;
  17. use Bluue\SalesBundle\Services\PdfGenerator;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class PublicDocumentWithEntityEventSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var InvoiceRepository
  23.      */
  24.     private InvoiceRepository $invoiceRepo;
  25.     /**
  26.      * @var PdfGenerator
  27.      */
  28.     private PdfGenerator $pdfGenerator;
  29.     /**
  30.      * @var QuotationRepository
  31.      */
  32.     private QuotationRepository $quotationRepo;
  33.     /**
  34.      * @var OrderRepository
  35.      */
  36.     private OrderRepository $orderRepo;
  37.     /**
  38.      * @var CreditNoteRepository
  39.      */
  40.     private CreditNoteRepository $cnRepo;
  41.     /**
  42.      * @var DeliveryNoteRepository
  43.      */
  44.     private DeliveryNoteRepository $dnRepo;
  45.     /**
  46.      * @param FileManagerRepository $fileManagerRepo
  47.      */
  48.     public function __construct(
  49.         InvoiceRepository $invoiceRepo,
  50.         PdfGenerator $pdfGenerator,
  51.         QuotationRepository $quotationRepo,
  52.         OrderRepository $orderRepo,
  53.         CreditNoteRepository $cnRepo,
  54.         DeliveryNoteRepository $dnRepo
  55.     ) {
  56.         $this->invoiceRepo $invoiceRepo;
  57.         $this->pdfGenerator $pdfGenerator;
  58.         $this->quotationRepo $quotationRepo;
  59.         $this->orderRepo $orderRepo;
  60.         $this->cnRepo $cnRepo;
  61.         $this->dnRepo $dnRepo;
  62.     }
  63.     /**
  64.      * @return string[]
  65.      */
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             PublicDocumentWithEntityEvent::NAME => 'onLoad'
  70.         ];
  71.     }
  72.     /**
  73.      * @param PublicDocumentWithEntityEvent $event
  74.      * @return void
  75.      */
  76.     public function onLoad(PublicDocumentWithEntityEvent $event): void
  77.     {
  78.         $entityId $event->getEntityId();
  79.         $entityName $event->getEntityName();
  80.         $content null;
  81.         switch ($entityName) {
  82.             case 'invoice':
  83.                 $invoice $this->invoiceRepo->find($entityId);
  84.                 if ($invoice) {
  85.                     $content base64_encode($this->invoiceRepo->getDocument($invoice));
  86.                 }
  87.                 break;
  88.             case 'quotation':
  89.                 $quotation $this->quotationRepo->find($entityId);
  90.                 if ($quotation) {
  91.                     $content base64_encode($this->quotationRepo->getDocument($quotation));
  92.                 }
  93.                 break;
  94.             case 'order':
  95.                 $order $this->orderRepo->find($entityId);
  96.                 if ($order) {
  97.                     $content base64_encode($this->orderRepo->getDocument($order));
  98.                 }
  99.                 break;
  100.             case 'credit_note':
  101.                 $creditNote $this->cnRepo->find($entityId);
  102.                 if ($creditNote) {
  103.                     $content base64_encode($this->cnRepo->getDocument($creditNote));
  104.                 }
  105.                 break;
  106.             case 'delivery_note':
  107.                 $deliveryNote $this->dnRepo->find($entityId);
  108.                 if ($deliveryNote) {
  109.                     $content base64_encode($this->pdfGenerator->generate('delivery_note'$deliveryNote));
  110.                 }
  111.                 break;
  112.             default:
  113.                 break;
  114.         }
  115.         if ($content) {
  116.             $event->setContent($content);
  117.         }
  118.     }
  119. }