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()); + } +}