<?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 Bluue\SalesBundle\Event\CreditNoteMovementsEvent;
use Bluue\SalesBundle\Event\InvoiceMovementsEvent;
use Bluue\SalesBundle\Event\OrderMovementsEvent;
use Bluue\SalesBundle\Event\QuotationMovementsEvent;
use Bluue\SalesBundle\Message\DocumentValidationMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DocumentValidationSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private MessageBusInterface $messageBus;
/**
* @param MessageBusInterface $messageBus
*/
public function __construct(
MessageBusInterface $messageBus
) {
$this->messageBus = $messageBus;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
QuotationMovementsEvent::VALIDATE => 'quotationValidation',
OrderMovementsEvent::VALIDATE_ORDER => 'orderValidation',
InvoiceMovementsEvent::VALIDATE => 'invoiceValidation',
CreditNoteMovementsEvent::VALIDATE => 'creditNoteValidation'
];
}
/**
* @param QuotationMovementsEvent $event
* @return void
*/
public function quotationValidation(QuotationMovementsEvent $event): void
{
$this->dispatchMessage($event->getQuotation());
}
/**
* @param OrderMovementsEvent $event
* @return void
*/
public function orderValidation(OrderMovementsEvent $event): void
{
$this->dispatchMessage($event->getOrder());
}
/**
* @param InvoiceMovementsEvent $event
* @return void
*/
public function invoiceValidation(InvoiceMovementsEvent $event): void
{
$this->dispatchMessage($event->getInvoice());
}
/**
* @param CreditNoteMovementsEvent $event
* @return void
*/
public function creditNoteValidation(CreditNoteMovementsEvent $event): void
{
$this->dispatchMessage($event->getCreditNote());
}
/**
* @param $entity
* @return void
*/
private function dispatchMessage($entity): void
{
$documentType = explode('\\', get_class($entity));
$this->messageBus->dispatch(
new DocumentValidationMessage($entity->getId(), array_pop($documentType))
);
}
}