diff --git a/src/API/TimesheetController.php b/src/API/TimesheetController.php index 3851c255..d8b8e6e2 100644 --- a/src/API/TimesheetController.php +++ b/src/API/TimesheetController.php @@ -434,7 +434,7 @@ class TimesheetController extends BaseApiController return $this->viewHandler->handle($view); } - $this->repository->save($timesheet); + $this->service->updateTimesheet($timesheet); $view = new View($timesheet, Response::HTTP_OK); $view->getContext()->setGroups(['Default', 'Entity', 'Timesheet']); @@ -759,7 +759,7 @@ class TimesheetController extends BaseApiController $timesheet->setExported(!$timesheet->isExported()); - $this->repository->save($timesheet); + $this->service->updateTimesheet($timesheet); $view = new View($timesheet, 200); $view->getContext()->setGroups(['Default', 'Entity', 'Timesheet']); @@ -812,7 +812,7 @@ class TimesheetController extends BaseApiController $meta->setValue($value); - $this->repository->save($timesheet); + $this->service->updateTimesheet($timesheet); $view = new View($timesheet, 200); $view->getContext()->setGroups(['Default', 'Entity', 'Timesheet']); diff --git a/src/Repository/TimesheetRepository.php b/src/Repository/TimesheetRepository.php index 85741c02..33dc22de 100644 --- a/src/Repository/TimesheetRepository.php +++ b/src/Repository/TimesheetRepository.php @@ -91,6 +91,25 @@ class TimesheetRepository extends EntityRepository } } + public function add(Timesheet $timesheet, int $maxRunningEntries) + { + $em = $this->getEntityManager(); + $em->beginTransaction(); + + try { + if (null === $timesheet->getEnd()) { + $this->stopActiveEntries($timesheet->getUser(), $maxRunningEntries, false); + } + + $em->persist($timesheet); + $em->flush(); + $em->commit(); + } catch (\Exception $ex) { + $em->rollback(); + throw $ex; + } + } + /** * @param Timesheet $timesheet * @throws \Doctrine\ORM\ORMException @@ -126,12 +145,13 @@ class TimesheetRepository extends EntityRepository /** * @param Timesheet $entry + * @param bool $flush * @return bool * @throws RepositoryException * @throws \Doctrine\ORM\ORMException * @throws \Doctrine\ORM\OptimisticLockException */ - public function stopRecording(Timesheet $entry) + public function stopRecording(Timesheet $entry, bool $flush = true) { if (null !== $entry->getEnd()) { throw new RepositoryException('Timesheet entry already stopped'); @@ -146,7 +166,9 @@ class TimesheetRepository extends EntityRepository $entityManager = $this->getEntityManager(); $entityManager->persist($entry); - $entityManager->flush(); + if ($flush) { + $entityManager->flush(); + } return true; } @@ -486,12 +508,13 @@ class TimesheetRepository extends EntityRepository /** * @param User $user * @param int $hardLimit + * @param bool $flush * @return int * @throws RepositoryException * @throws \Doctrine\ORM\ORMException * @throws \Doctrine\ORM\OptimisticLockException */ - public function stopActiveEntries(User $user, int $hardLimit) + public function stopActiveEntries(User $user, int $hardLimit, bool $flush = true) { $counter = 0; $activeEntries = $this->getActiveEntries($user); @@ -509,7 +532,7 @@ class TimesheetRepository extends EntityRepository throw new \Exception('timesheet.start.exceeded_limit'); } - $this->stopRecording($activeEntry); + $this->stopRecording($activeEntry, $flush); $counter++; } $i++; diff --git a/src/Timesheet/TimesheetService.php b/src/Timesheet/TimesheetService.php index 6015b573..0157eae4 100644 --- a/src/Timesheet/TimesheetService.php +++ b/src/Timesheet/TimesheetService.php @@ -57,7 +57,7 @@ final class TimesheetService } /** - * Calls prepareNewTimesheet() automatically. + * Calls prepareNewTimesheet() automatically if $request is not null. * * @param User $user * @param Request|null $request @@ -96,21 +96,20 @@ final class TimesheetService throw new \InvalidArgumentException('Cannot create timesheet, already persisted'); } - if (null === $timesheet->getEnd()) { - if (!$this->auth->isGranted('start', $timesheet)) { - throw new AccessDeniedHttpException('You are not allowed to start this timesheet record'); - } - $this->repository->stopActiveEntries( - $timesheet->getUser(), - $this->configuration->getActiveEntriesHardLimit() - ); + if (null === $timesheet->getEnd() && !$this->auth->isGranted('start', $timesheet)) { + throw new AccessDeniedHttpException('You are not allowed to start this timesheet record'); } - $this->repository->save($timesheet); + $this->repository->add($timesheet, $this->configuration->getActiveEntriesHardLimit()); return $timesheet; } + public function updateTimesheet(Timesheet $timesheet) + { + return $this->repository->save($timesheet); + } + public function stopTimesheet(Timesheet $timesheet) { return $this->repository->stopRecording($timesheet); diff --git a/tests/Export/Renderer/HtmlRendererFactoryTest.php b/tests/Export/Renderer/HtmlRendererFactoryTest.php index bf45c29b..c7f2ecc4 100644 --- a/tests/Export/Renderer/HtmlRendererFactoryTest.php +++ b/tests/Export/Renderer/HtmlRendererFactoryTest.php @@ -29,7 +29,6 @@ class HtmlRendererFactoryTest extends TestCase $this->createMock(ProjectRepository::class) ); - /** @var HtmlRenderer $renderer */ $renderer = $sut->create('foo', 'bar.html.twig'); self::assertInstanceOf(HtmlRenderer::class, $renderer); diff --git a/tests/Export/Renderer/PdfRendererFactoryTest.php b/tests/Export/Renderer/PdfRendererFactoryTest.php index 526b1716..828912d3 100644 --- a/tests/Export/Renderer/PdfRendererFactoryTest.php +++ b/tests/Export/Renderer/PdfRendererFactoryTest.php @@ -9,7 +9,6 @@ namespace App\Tests\Export\Renderer; -use App\Export\Renderer\HtmlRenderer; use App\Export\Renderer\PDFRenderer; use App\Export\Renderer\PdfRendererFactory; use App\Repository\ProjectRepository; @@ -32,7 +31,6 @@ class PdfRendererFactoryTest extends TestCase $this->createMock(ProjectRepository::class) ); - /** @var HtmlRenderer $renderer */ $renderer = $sut->create('foo', 'bar.pdf.twig'); self::assertInstanceOf(PDFRenderer::class, $renderer);