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 @@ +