added invoice delete event (#3096)
This commit is contained in:
@@ -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);
|
||||
|
||||
28
src/Event/InvoiceDeleteEvent.php
Normal file
28
src/Event/InvoiceDeleteEvent.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
29
tests/Event/InvoiceDeleteEventTest.php
Normal file
29
tests/Event/InvoiceDeleteEventTest.php
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user