added events for timesheet actions (#1598)

This commit is contained in:
Lukas
2020-04-15 18:07:14 +02:00
committed by GitHub
parent be00a519b3
commit 7b25e9acaf
27 changed files with 572 additions and 10 deletions

View File

@@ -478,7 +478,7 @@ class TimesheetController extends BaseApiController
throw $this->createAccessDeniedException('You are not allowed to delete this timesheet');
}
$this->repository->delete($timesheet);
$this->service->deleteTimesheet($timesheet);
$view = new View(null, Response::HTTP_NO_CONTENT);

View File

@@ -154,7 +154,7 @@ abstract class TimesheetAbstractController extends AbstractController
if ($editForm->isSubmitted() && $editForm->isValid()) {
try {
$this->repository->save($entry);
$this->service->updateTimesheet($entry);
$this->flashSuccess('action.update.success');
return $this->redirectToRoute($this->getTimesheetRoute(), ['page' => $request->get('page', 1)]);
@@ -344,7 +344,7 @@ abstract class TimesheetAbstractController extends AbstractController
if ($execute) {
try {
$this->repository->saveMultiple($dto->getEntities());
$this->service->updateMultipleTimesheets($dto->getEntities());
$this->flashSuccess('action.update.success');
return $this->redirectToRoute($this->getTimesheetRoute());
@@ -378,7 +378,7 @@ abstract class TimesheetAbstractController extends AbstractController
$dto->setEntities($timesheets);
try {
$this->repository->deleteMultiple($dto->getEntities());
$this->service->deleteMultipleTimesheets($dto->getEntities());
$this->flashSuccess('action.delete.success');
} catch (\Exception $ex) {
$this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]);

View File

@@ -0,0 +1,34 @@
<?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\Timesheet;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Base event class to used with timesheet manipulations.
*/
abstract class AbstractTimesheetEvent extends Event
{
/**
* @var Timesheet
*/
private $timesheet;
public function __construct(Timesheet $timesheet)
{
$this->timesheet = $timesheet;
}
public function getTimesheet(): Timesheet
{
return $this->timesheet;
}
}

View File

@@ -0,0 +1,34 @@
<?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\Timesheet;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Base event class to used with timesheet manipulations.
*/
abstract class AbstractTimesheetMultipleEvent extends Event
{
/**
* @var Timesheet[]
*/
private $timesheets;
public function __construct(array $timesheets)
{
$this->timesheets = $timesheets;
}
public function getTimesheets(): array
{
return $this->timesheets;
}
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetCreatePostEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetCreatePreEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetDeleteMultiplePreEvent extends AbstractTimesheetMultipleEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetDeletePreEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetStopPostEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetStopPreEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetUpdateMultiplePostEvent extends AbstractTimesheetMultipleEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetUpdateMultiplePreEvent extends AbstractTimesheetMultipleEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetUpdatePostEvent extends AbstractTimesheetEvent
{
}

View File

@@ -0,0 +1,14 @@
<?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;
final class TimesheetUpdatePreEvent extends AbstractTimesheetEvent
{
}

View File

@@ -12,7 +12,17 @@ namespace App\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Event\TimesheetCreatePostEvent;
use App\Event\TimesheetCreatePreEvent;
use App\Event\TimesheetDeleteMultiplePreEvent;
use App\Event\TimesheetDeletePreEvent;
use App\Event\TimesheetMetaDefinitionEvent;
use App\Event\TimesheetStopPostEvent;
use App\Event\TimesheetStopPreEvent;
use App\Event\TimesheetUpdateMultiplePostEvent;
use App\Event\TimesheetUpdateMultiplePreEvent;
use App\Event\TimesheetUpdatePostEvent;
use App\Event\TimesheetUpdatePreEvent;
use App\Repository\TimesheetRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
@@ -75,7 +85,7 @@ final class TimesheetService
return $timesheet;
}
public function prepareNewTimesheet(Timesheet $timesheet, ?Request $request = null)
public function prepareNewTimesheet(Timesheet $timesheet, ?Request $request = null): Timesheet
{
if (null !== $timesheet->getId()) {
throw new \InvalidArgumentException('Cannot prepare timesheet, already persisted');
@@ -90,7 +100,7 @@ final class TimesheetService
return $timesheet;
}
public function saveNewTimesheet(Timesheet $timesheet)
public function saveNewTimesheet(Timesheet $timesheet): Timesheet
{
if (null !== $timesheet->getId()) {
throw new \InvalidArgumentException('Cannot create timesheet, already persisted');
@@ -100,18 +110,47 @@ final class TimesheetService
throw new AccessDeniedHttpException('You are not allowed to start this timesheet record');
}
$this->dispatcher->dispatch(new TimesheetCreatePreEvent($timesheet));
$this->repository->add($timesheet, $this->configuration->getActiveEntriesHardLimit());
$this->dispatcher->dispatch(new TimesheetCreatePostEvent($timesheet));
return $timesheet;
}
public function updateTimesheet(Timesheet $timesheet)
public function updateTimesheet(Timesheet $timesheet): Timesheet
{
return $this->repository->save($timesheet);
$this->dispatcher->dispatch(new TimesheetUpdatePreEvent($timesheet));
$this->repository->save($timesheet);
$this->dispatcher->dispatch(new TimesheetUpdatePostEvent($timesheet));
return $timesheet;
}
public function stopTimesheet(Timesheet $timesheet)
public function updateMultipleTimesheets(array $timesheets): array
{
return $this->repository->stopRecording($timesheet);
$this->dispatcher->dispatch(new TimesheetUpdateMultiplePreEvent($timesheets));
$this->repository->saveMultiple($timesheets);
$this->dispatcher->dispatch(new TimesheetUpdateMultiplePostEvent($timesheets));
return $timesheets;
}
public function stopTimesheet(Timesheet $timesheet): void
{
$this->dispatcher->dispatch(new TimesheetStopPreEvent($timesheet));
$this->repository->stopRecording($timesheet);
$this->dispatcher->dispatch(new TimesheetStopPostEvent($timesheet));
}
public function deleteTimesheet(Timesheet $timesheet): void
{
$this->dispatcher->dispatch(new TimesheetDeletePreEvent($timesheet));
$this->repository->delete($timesheet);
}
public function deleteMultipleTimesheets(array $timesheets): void
{
$this->dispatcher->dispatch(new TimesheetDeleteMultiplePreEvent($timesheets));
$this->repository->deleteMultiple($timesheets);
}
}

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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\EventDispatcher\Event;
abstract class AbstractTimesheetEventTest extends TestCase
{
abstract protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent;
public function testGetterAndSetter()
{
$timesheet = new Timesheet();
$sut = $this->createTimesheetEvent($timesheet);
self::assertInstanceOf(Event::class, $sut);
self::assertSame($timesheet, $sut->getTimesheet());
}
}

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\Timesheet;
use App\Event\AbstractTimesheetMultipleEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\EventDispatcher\Event;
abstract class AbstractTimesheetMultipleEventTest extends TestCase
{
abstract protected function createTimesheetMultipleEvent(array $timesheets): AbstractTimesheetMultipleEvent;
public function testGetterAndSetter()
{
$timesheets = [new Timesheet(), new Timesheet()];
$sut = $this->createTimesheetMultipleEvent($timesheets);
self::assertInstanceOf(Event::class, $sut);
self::assertSame($timesheets, $sut->getTimesheets());
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetCreatePostEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetCreatePostEvent
*/
class TimesheetCreatePostEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetCreatePostEvent($timesheet);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetCreatePreEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetCreatePreEvent
*/
class TimesheetCreatePreEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetCreatePreEvent($timesheet);
}
}

View File

@@ -0,0 +1,25 @@
<?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\Event\AbstractTimesheetMultipleEvent;
use App\Event\TimesheetDeleteMultiplePreEvent;
/**
* @covers \App\Event\AbstractTimesheetMultipleEvent
* @covers \App\Event\TimesheetDeleteMultiplePreEvent
*/
class TimesheetDeleteMultiplePreEventTest extends AbstractTimesheetMultipleEventTest
{
protected function createTimesheetMultipleEvent(array $timesheets): AbstractTimesheetMultipleEvent
{
return new TimesheetDeleteMultiplePreEvent($timesheets);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetDeletePreEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetDeletePreEvent
*/
class TimesheetDeletePreEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetDeletePreEvent($timesheet);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetStopPostEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetStopPostEvent
*/
class TimesheetStopPostEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetStopPostEvent($timesheet);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetStopPreEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetStopPreEvent
*/
class TimesheetStopPreEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetStopPreEvent($timesheet);
}
}

View File

@@ -0,0 +1,25 @@
<?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\Event\AbstractTimesheetMultipleEvent;
use App\Event\TimesheetUpdateMultiplePostEvent;
/**
* @covers \App\Event\AbstractTimesheetMultipleEvent
* @covers \App\Event\TimesheetUpdateMultiplePostEvent
*/
class TimesheetUpdateMultiplePostEventTest extends AbstractTimesheetMultipleEventTest
{
protected function createTimesheetMultipleEvent(array $timesheets): AbstractTimesheetMultipleEvent
{
return new TimesheetUpdateMultiplePostEvent($timesheets);
}
}

View File

@@ -0,0 +1,25 @@
<?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\Event\AbstractTimesheetMultipleEvent;
use App\Event\TimesheetUpdateMultiplePreEvent;
/**
* @covers \App\Event\AbstractTimesheetMultipleEvent
* @covers \App\Event\TimesheetUpdateMultiplePreEvent
*/
class TimesheetUpdateMultiplePreEventTest extends AbstractTimesheetMultipleEventTest
{
protected function createTimesheetMultipleEvent(array $timesheets): AbstractTimesheetMultipleEvent
{
return new TimesheetUpdateMultiplePreEvent($timesheets);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetUpdatePostEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetUpdatePostEvent
*/
class TimesheetUpdatePostEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetUpdatePostEvent($timesheet);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Timesheet;
use App\Event\AbstractTimesheetEvent;
use App\Event\TimesheetUpdatePreEvent;
/**
* @covers \App\Event\AbstractTimesheetEvent
* @covers \App\Event\TimesheetUpdatePreEvent
*/
class TimesheetUpdatePreEventTest extends AbstractTimesheetEventTest
{
protected function createTimesheetEvent(Timesheet $timesheet): AbstractTimesheetEvent
{
return new TimesheetUpdatePreEvent($timesheet);
}
}