configuration = $configuration; $this->repository = $repository; $this->trackingModeService = $service; $this->dispatcher = $dispatcher; $this->auth = $security; } /** * Calls prepareNewTimesheet() automatically if $request is not null. * * @param User $user * @param Request|null $request * @return Timesheet */ public function createNewTimesheet(User $user, ?Request $request = null): Timesheet { $timesheet = new Timesheet(); $timesheet->setUser($user); if (null !== $request) { $this->prepareNewTimesheet($timesheet, $request); } return $timesheet; } public function prepareNewTimesheet(Timesheet $timesheet, ?Request $request = null): Timesheet { if (null !== $timesheet->getId()) { throw new \InvalidArgumentException('Cannot prepare timesheet, already persisted'); } $event = new TimesheetMetaDefinitionEvent($timesheet); $this->dispatcher->dispatch($event); $mode = $this->trackingModeService->getActiveMode(); $mode->create($timesheet, $request); return $timesheet; } public function saveNewTimesheet(Timesheet $timesheet): Timesheet { if (null !== $timesheet->getId()) { throw new \InvalidArgumentException('Cannot create timesheet, already persisted'); } if (null === $timesheet->getEnd() && !$this->auth->isGranted('start', $timesheet)) { 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): Timesheet { $this->dispatcher->dispatch(new TimesheetUpdatePreEvent($timesheet)); $this->repository->save($timesheet); $this->dispatcher->dispatch(new TimesheetUpdatePostEvent($timesheet)); return $timesheet; } public function updateMultipleTimesheets(array $timesheets): array { $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); } }