From 7b25e9acafa43c5a46701511dae758835d692f8e Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 15 Apr 2020 18:07:14 +0200 Subject: [PATCH] added events for timesheet actions (#1598) --- src/API/TimesheetController.php | 2 +- .../TimesheetAbstractController.php | 6 +-- src/Event/AbstractTimesheetEvent.php | 34 +++++++++++++ src/Event/AbstractTimesheetMultipleEvent.php | 34 +++++++++++++ src/Event/TimesheetCreatePostEvent.php | 14 +++++ src/Event/TimesheetCreatePreEvent.php | 14 +++++ src/Event/TimesheetDeleteMultiplePreEvent.php | 14 +++++ src/Event/TimesheetDeletePreEvent.php | 14 +++++ src/Event/TimesheetStopPostEvent.php | 14 +++++ src/Event/TimesheetStopPreEvent.php | 14 +++++ .../TimesheetUpdateMultiplePostEvent.php | 14 +++++ src/Event/TimesheetUpdateMultiplePreEvent.php | 14 +++++ src/Event/TimesheetUpdatePostEvent.php | 14 +++++ src/Event/TimesheetUpdatePreEvent.php | 14 +++++ src/Timesheet/TimesheetService.php | 51 ++++++++++++++++--- tests/Event/AbstractTimesheetEventTest.php | 29 +++++++++++ .../AbstractTimesheetMultipleEventTest.php | 29 +++++++++++ tests/Event/TimesheetCreatePostEventTest.php | 26 ++++++++++ tests/Event/TimesheetCreatePreEventTest.php | 26 ++++++++++ .../TimesheetDeleteMultiplePreEventTest.php | 25 +++++++++ tests/Event/TimesheetDeletePreEventTest.php | 26 ++++++++++ tests/Event/TimesheetStopPostEventTest.php | 26 ++++++++++ tests/Event/TimesheetStopPreEventTest.php | 26 ++++++++++ .../TimesheetUpdateMultiplePostEventTest.php | 25 +++++++++ .../TimesheetUpdateMultiplePreEventTest.php | 25 +++++++++ tests/Event/TimesheetUpdatePostEventTest.php | 26 ++++++++++ tests/Event/TimesheetUpdatePreEventTest.php | 26 ++++++++++ 27 files changed, 572 insertions(+), 10 deletions(-) create mode 100644 src/Event/AbstractTimesheetEvent.php create mode 100644 src/Event/AbstractTimesheetMultipleEvent.php create mode 100644 src/Event/TimesheetCreatePostEvent.php create mode 100644 src/Event/TimesheetCreatePreEvent.php create mode 100644 src/Event/TimesheetDeleteMultiplePreEvent.php create mode 100644 src/Event/TimesheetDeletePreEvent.php create mode 100644 src/Event/TimesheetStopPostEvent.php create mode 100644 src/Event/TimesheetStopPreEvent.php create mode 100644 src/Event/TimesheetUpdateMultiplePostEvent.php create mode 100644 src/Event/TimesheetUpdateMultiplePreEvent.php create mode 100644 src/Event/TimesheetUpdatePostEvent.php create mode 100644 src/Event/TimesheetUpdatePreEvent.php create mode 100644 tests/Event/AbstractTimesheetEventTest.php create mode 100644 tests/Event/AbstractTimesheetMultipleEventTest.php create mode 100644 tests/Event/TimesheetCreatePostEventTest.php create mode 100644 tests/Event/TimesheetCreatePreEventTest.php create mode 100644 tests/Event/TimesheetDeleteMultiplePreEventTest.php create mode 100644 tests/Event/TimesheetDeletePreEventTest.php create mode 100644 tests/Event/TimesheetStopPostEventTest.php create mode 100644 tests/Event/TimesheetStopPreEventTest.php create mode 100644 tests/Event/TimesheetUpdateMultiplePostEventTest.php create mode 100644 tests/Event/TimesheetUpdateMultiplePreEventTest.php create mode 100644 tests/Event/TimesheetUpdatePostEventTest.php create mode 100644 tests/Event/TimesheetUpdatePreEventTest.php diff --git a/src/API/TimesheetController.php b/src/API/TimesheetController.php index 97315265..f89af7c0 100644 --- a/src/API/TimesheetController.php +++ b/src/API/TimesheetController.php @@ -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); diff --git a/src/Controller/TimesheetAbstractController.php b/src/Controller/TimesheetAbstractController.php index 9b64e5ea..6a8bffac 100644 --- a/src/Controller/TimesheetAbstractController.php +++ b/src/Controller/TimesheetAbstractController.php @@ -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()]); diff --git a/src/Event/AbstractTimesheetEvent.php b/src/Event/AbstractTimesheetEvent.php new file mode 100644 index 00000000..f927017f --- /dev/null +++ b/src/Event/AbstractTimesheetEvent.php @@ -0,0 +1,34 @@ +timesheet = $timesheet; + } + + public function getTimesheet(): Timesheet + { + return $this->timesheet; + } +} diff --git a/src/Event/AbstractTimesheetMultipleEvent.php b/src/Event/AbstractTimesheetMultipleEvent.php new file mode 100644 index 00000000..dcc5cdf1 --- /dev/null +++ b/src/Event/AbstractTimesheetMultipleEvent.php @@ -0,0 +1,34 @@ +timesheets = $timesheets; + } + + public function getTimesheets(): array + { + return $this->timesheets; + } +} diff --git a/src/Event/TimesheetCreatePostEvent.php b/src/Event/TimesheetCreatePostEvent.php new file mode 100644 index 00000000..88f59562 --- /dev/null +++ b/src/Event/TimesheetCreatePostEvent.php @@ -0,0 +1,14 @@ +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); } } diff --git a/tests/Event/AbstractTimesheetEventTest.php b/tests/Event/AbstractTimesheetEventTest.php new file mode 100644 index 00000000..cef65681 --- /dev/null +++ b/tests/Event/AbstractTimesheetEventTest.php @@ -0,0 +1,29 @@ +createTimesheetEvent($timesheet); + + self::assertInstanceOf(Event::class, $sut); + self::assertSame($timesheet, $sut->getTimesheet()); + } +} diff --git a/tests/Event/AbstractTimesheetMultipleEventTest.php b/tests/Event/AbstractTimesheetMultipleEventTest.php new file mode 100644 index 00000000..104a0782 --- /dev/null +++ b/tests/Event/AbstractTimesheetMultipleEventTest.php @@ -0,0 +1,29 @@ +createTimesheetMultipleEvent($timesheets); + + self::assertInstanceOf(Event::class, $sut); + self::assertSame($timesheets, $sut->getTimesheets()); + } +} diff --git a/tests/Event/TimesheetCreatePostEventTest.php b/tests/Event/TimesheetCreatePostEventTest.php new file mode 100644 index 00000000..da0e50ea --- /dev/null +++ b/tests/Event/TimesheetCreatePostEventTest.php @@ -0,0 +1,26 @@ +