added invoice delete event (#3096)

This commit is contained in:
Kevin Papst
2022-01-24 21:33:13 +01:00
committed by GitHub
parent 9e320674c1
commit 8694a3bfe4
4 changed files with 65 additions and 2 deletions

View File

@@ -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);

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Event;
use App\Entity\Invoice;
use Symfony\Contracts\EventDispatcher\Event;
final class InvoiceDeleteEvent extends Event
{
private $invoice;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
public function getInvoice(): Invoice
{
return $this->invoice;
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Event;
use App\Entity\Invoice;
use App\Event\InvoiceDeleteEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\InvoiceDeleteEvent
*/
class InvoiceDeleteEventTest extends TestCase
{
public function testDefaultValues()
{
$invoice = new Invoice();
$sut = new InvoiceDeleteEvent($invoice);
self::assertSame($invoice, $sut->getInvoice());
}
}