From 8694a3bfe422a3a9299f6f7444ae0a903dbe09c2 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 24 Jan 2022 21:33:13 +0100 Subject: [PATCH] added invoice delete event (#3096) --- src/Controller/InvoiceController.php | 2 +- src/Event/InvoiceDeleteEvent.php | 28 +++++++++++++++++++++++++ src/Invoice/ServiceInvoice.php | 8 ++++++- tests/Event/InvoiceDeleteEventTest.php | 29 ++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/Event/InvoiceDeleteEvent.php create mode 100644 tests/Event/InvoiceDeleteEventTest.php diff --git a/src/Controller/InvoiceController.php b/src/Controller/InvoiceController.php index 9ecba3de..4c270f83 100644 --- a/src/Controller/InvoiceController.php +++ b/src/Controller/InvoiceController.php @@ -295,7 +295,7 @@ final class InvoiceController extends AbstractController $csrfTokenManager->refreshToken('invoice.status'); try { - $this->service->deleteInvoice($invoice); + $this->service->deleteInvoice($invoice, $this->dispatcher); $this->flashSuccess('action.delete.success'); } catch (Exception $ex) { $this->flashDeleteException($ex); diff --git a/src/Event/InvoiceDeleteEvent.php b/src/Event/InvoiceDeleteEvent.php new file mode 100644 index 00000000..89b49fab --- /dev/null +++ b/src/Event/InvoiceDeleteEvent.php @@ -0,0 +1,28 @@ +invoice = $invoice; + } + + public function getInvoice(): Invoice + { + return $this->invoice; + } +} diff --git a/src/Invoice/ServiceInvoice.php b/src/Invoice/ServiceInvoice.php index 552d99cc..a38af9ac 100644 --- a/src/Invoice/ServiceInvoice.php +++ b/src/Invoice/ServiceInvoice.php @@ -14,6 +14,7 @@ use App\Constants; use App\Entity\Invoice; use App\Entity\InvoiceDocument; use App\Event\InvoiceCreatedEvent; +use App\Event\InvoiceDeleteEvent; use App\Event\InvoicePostRenderEvent; use App\Event\InvoicePreRenderEvent; use App\Repository\InvoiceDocumentRepository; @@ -418,12 +419,17 @@ final class ServiceInvoice return $this->createInvoiceFromModel($model, $dispatcher); } - public function deleteInvoice(Invoice $invoice) + public function deleteInvoice(Invoice $invoice, EventDispatcherInterface $dispatcher) { $invoiceDirectory = $this->getInvoicesDirectory(); + if (is_file($invoiceDirectory . $invoice->getInvoiceFilename())) { $this->fileHelper->removeFile($invoiceDirectory . $invoice->getInvoiceFilename()); } + + $event = new InvoiceDeleteEvent($invoice); + $dispatcher->dispatch($event); + $this->invoiceRepository->deleteInvoice($invoice); } diff --git a/tests/Event/InvoiceDeleteEventTest.php b/tests/Event/InvoiceDeleteEventTest.php new file mode 100644 index 00000000..3847ec5b --- /dev/null +++ b/tests/Event/InvoiceDeleteEventTest.php @@ -0,0 +1,29 @@ +getInvoice()); + } +}