<?phpnamespace App\EventListener;use App\Entity\InvoiceBatch;use App\Event\InvoiceBatchEvent;use App\Model\InvoiceBatchFactory;use App\Model\InvoiceFactory;use App\Model\InvoiceProcessor;use App\Service\SupplierConnectionService;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class InvoiceBatchSubscriber implements EventSubscriberInterface{ /** * @var InvoiceFactory */ private $invoiceFactory; private $invoiceBatchFactory; private $invoiceProcessor; /** @var EventDispatcherInterface $eventDispatcher */ private $eventDispatcher; /** * @var EntityManagerInterface */ private $entityManager; /** * @var SupplierConnectionService */ private $supplierConnectionService; public function __construct( InvoiceFactory $invoiceFactory, InvoiceBatchFactory $invoiceBatchFactory, EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher, InvoiceProcessor $invoiceProcessor, SupplierConnectionService $supplierConnectionService ) { $this->invoiceFactory = $invoiceFactory; $this->invoiceBatchFactory = $invoiceBatchFactory; $this->entityManager = $entityManager; $this->eventDispatcher = $eventDispatcher; $this->invoiceProcessor = $invoiceProcessor; $this->supplierConnectionService = $supplierConnectionService; } public static function getSubscribedEvents(): array { return [ InvoiceBatchEvent::STATUS_REFRESH_EVENT => [ 'onInvoiceBatchStatusRefresh' ], InvoiceBatchEvent::PROCESS_EVENT => [ 'onInvoiceBatchProcess' ] ]; } public function onInvoiceBatchStatusRefresh(InvoiceBatchEvent $event) { $invoiceBatchEntity = $event->getInvoiceBatch(); $cptPending = 0; $cptProcessing = 0; $cptProcessed = 0; $invoiceEntities = $invoiceBatchEntity->getInvoices(); if ($invoiceEntities) { foreach ($invoiceEntities as $invoiceEntity) { $invoice = $this->invoiceFactory->createModel($invoiceEntity); if ($invoice->isPending()) { $cptPending++; } elseif ($invoice->isProcessing()) { $cptProcessing++; } else { $cptProcessed++; } } if ($cptPending + $cptProcessing == 0) { $newStatus = InvoiceBatch::STATUS_PROCESSED; } elseif ($cptPending === $invoiceBatchEntity->getInvoices()->count()) { $newStatus = InvoiceBatch::STATUS_PENDING; } else { $newStatus = InvoiceBatch::STATUS_PROCESSING; } $invoiceBatchEntity->setStatus($newStatus); $this->entityManager->flush(); $invoiceBatch = $this->invoiceBatchFactory->createModel($invoiceBatchEntity); if ($invoiceBatch->isProcessed()) { $invoiceBatchEvent = new InvoiceBatchEvent($invoiceBatchEntity); $this->eventDispatcher->dispatch( $invoiceBatchEvent, InvoiceBatchEvent::PROCESS_EVENT ); } } } public function onInvoiceBatchProcess(InvoiceBatchEvent $event) { $invoiceBatchEntity = $event->getInvoiceBatch(); foreach ($invoiceBatchEntity->getInvoices() as $invoiceEntity) { $invoice = $this->invoiceFactory->createModel($invoiceEntity); // On ne prend pas en compte les rejetées if ($invoice->isProcessed()) { $this->invoiceProcessor->setInvoice($invoiceEntity); $supplierCustomer = $this->invoiceProcessor->getSupplierCustomer(); $supplierCustomerNoTP = $this->supplierConnectionService->getSupplierCustomerNoTP(); if (is_null($supplierCustomer->getId())) { $this->entityManager->persist($supplierCustomer); } if (! is_null($supplierCustomerNoTP)) { $this->entityManager->persist($supplierCustomerNoTP); } $this->entityManager->flush(); } // Enable the products if ($products = $invoiceEntity->getProducts()) { foreach ($products as $product) { $product->setEnabled(true); $this->entityManager->flush(); } } } }}