<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\SalesBundle\EventSubscriber;
use Symfony\Component\Uid\UuidV6;
use App\Event\PublicDocumentWithEntityEvent;
use App\Repository\FileManagerRepository;
use Bluue\SalesBundle\Repository\CreditNoteRepository;
use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
use Bluue\SalesBundle\Repository\InvoiceRepository;
use Bluue\SalesBundle\Repository\OrderRepository;
use Bluue\SalesBundle\Repository\QuotationRepository;
use Bluue\SalesBundle\Services\PdfGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PublicDocumentWithEntityEventSubscriber implements EventSubscriberInterface
{
/**
* @var InvoiceRepository
*/
private InvoiceRepository $invoiceRepo;
/**
* @var PdfGenerator
*/
private PdfGenerator $pdfGenerator;
/**
* @var QuotationRepository
*/
private QuotationRepository $quotationRepo;
/**
* @var OrderRepository
*/
private OrderRepository $orderRepo;
/**
* @var CreditNoteRepository
*/
private CreditNoteRepository $cnRepo;
/**
* @var DeliveryNoteRepository
*/
private DeliveryNoteRepository $dnRepo;
/**
* @param FileManagerRepository $fileManagerRepo
*/
public function __construct(
InvoiceRepository $invoiceRepo,
PdfGenerator $pdfGenerator,
QuotationRepository $quotationRepo,
OrderRepository $orderRepo,
CreditNoteRepository $cnRepo,
DeliveryNoteRepository $dnRepo
) {
$this->invoiceRepo = $invoiceRepo;
$this->pdfGenerator = $pdfGenerator;
$this->quotationRepo = $quotationRepo;
$this->orderRepo = $orderRepo;
$this->cnRepo = $cnRepo;
$this->dnRepo = $dnRepo;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
PublicDocumentWithEntityEvent::NAME => 'onLoad'
];
}
/**
* @param PublicDocumentWithEntityEvent $event
* @return void
*/
public function onLoad(PublicDocumentWithEntityEvent $event): void
{
$entityId = $event->getEntityId();
$entityName = $event->getEntityName();
$content = null;
switch ($entityName) {
case 'invoice':
$invoice = $this->invoiceRepo->find($entityId);
if ($invoice) {
$content = base64_encode($this->invoiceRepo->getDocument($invoice));
}
break;
case 'quotation':
$quotation = $this->quotationRepo->find($entityId);
if ($quotation) {
$content = base64_encode($this->quotationRepo->getDocument($quotation));
}
break;
case 'order':
$order = $this->orderRepo->find($entityId);
if ($order) {
$content = base64_encode($this->orderRepo->getDocument($order));
}
break;
case 'credit_note':
$creditNote = $this->cnRepo->find($entityId);
if ($creditNote) {
$content = base64_encode($this->cnRepo->getDocument($creditNote));
}
break;
case 'delivery_note':
$deliveryNote = $this->dnRepo->find($entityId);
if ($deliveryNote) {
$content = base64_encode($this->pdfGenerator->generate('delivery_note', $deliveryNote));
}
break;
default:
break;
}
if ($content) {
$event->setContent($content);
}
}
}