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

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 App\Event\PublicDocumentWithEntityEvent;
  10. use Bluue\SalesBundle\Repository\CreditNoteRepository;
  11. use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
  12. use Bluue\SalesBundle\Repository\InvoiceRepository;
  13. use Bluue\SalesBundle\Repository\OrderRepository;
  14. use Bluue\SalesBundle\Repository\QuotationRepository;
  15. use Bluue\SalesBundle\Services\PdfGenerator;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class PublicDocumentWithEntityEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var InvoiceRepository
  21.      */
  22.     private InvoiceRepository $invoiceRepo;
  23.     /**
  24.      * @var PdfGenerator
  25.      */
  26.     private PdfGenerator $pdfGenerator;
  27.     /**
  28.      * @var QuotationRepository
  29.      */
  30.     private QuotationRepository $quotationRepo;
  31.     /**
  32.      * @var OrderRepository
  33.      */
  34.     private OrderRepository $orderRepo;
  35.     /**
  36.      * @var CreditNoteRepository
  37.      */
  38.     private CreditNoteRepository $cnRepo;
  39.     /**
  40.      * @var DeliveryNoteRepository
  41.      */
  42.     private DeliveryNoteRepository $dnRepo;
  43.     public function __construct(
  44.         InvoiceRepository $invoiceRepo,
  45.         PdfGenerator $pdfGenerator,
  46.         QuotationRepository $quotationRepo,
  47.         OrderRepository $orderRepo,
  48.         CreditNoteRepository $cnRepo,
  49.         DeliveryNoteRepository $dnRepo
  50.     ) {
  51.         $this->invoiceRepo $invoiceRepo;
  52.         $this->pdfGenerator $pdfGenerator;
  53.         $this->quotationRepo $quotationRepo;
  54.         $this->orderRepo $orderRepo;
  55.         $this->cnRepo $cnRepo;
  56.         $this->dnRepo $dnRepo;
  57.     }
  58.     /**
  59.      * @return string[]
  60.      */
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             PublicDocumentWithEntityEvent::NAME => 'onLoad'
  65.         ];
  66.     }
  67.     /**
  68.      * @param PublicDocumentWithEntityEvent $event
  69.      * @return void
  70.      */
  71.     public function onLoad(PublicDocumentWithEntityEvent $event): void
  72.     {
  73.         $entityId $event->getEntityId();
  74.         $entityName $event->getEntityName();
  75.         $content null;
  76.         switch ($entityName) {
  77.             case 'invoice':
  78.                 $invoice $this->invoiceRepo->find($entityId);
  79.                 if ($invoice) {
  80.                     $content base64_encode($this->invoiceRepo->getDocument($invoice));
  81.                 }
  82.                 break;
  83.             case 'quotation':
  84.                 $quotation $this->quotationRepo->find($entityId);
  85.                 if ($quotation) {
  86.                     $content base64_encode($this->quotationRepo->getDocument($quotation));
  87.                 }
  88.                 break;
  89.             case 'order':
  90.                 $order $this->orderRepo->find($entityId);
  91.                 if ($order) {
  92.                     $content base64_encode($this->orderRepo->getDocument($order));
  93.                 }
  94.                 break;
  95.             case 'credit_note':
  96.                 $creditNote $this->cnRepo->find($entityId);
  97.                 if ($creditNote) {
  98.                     $content base64_encode($this->cnRepo->getDocument($creditNote));
  99.                 }
  100.                 break;
  101.             case 'delivery_note':
  102.                 $deliveryNote $this->dnRepo->find($entityId);
  103.                 if ($deliveryNote) {
  104.                     $content base64_encode($this->pdfGenerator->generate('delivery_note'$deliveryNote));
  105.                 }
  106.                 break;
  107.             default:
  108.                 break;
  109.         }
  110.         if ($content) {
  111.             $event->setContent($content);
  112.         }
  113.     }
  114. }