From 8d2c150141cf6726298751d23917f476b9b430a6 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 1 Jun 2021 18:46:16 +0200 Subject: [PATCH] Better type handling and exception annotations --- Classes/Command/SequenceCommandController.php | 2 +- Classes/Service/SequenceGenerator.php | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index da8a129..47e388b 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -27,7 +27,7 @@ class SequenceCommandController extends CommandController * @param int $to * @param string $type */ - public function advanceCommand($to, $type) + public function advanceCommand(int $to, string $type): void { $this->sequenceGenerator->advanceTo($to, $type); } diff --git a/Classes/Service/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index cdbc9d6..12aca7e 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -5,8 +5,7 @@ declare(strict_types=1); namespace DigiComp\Sequence\Service; use DigiComp\Sequence\Domain\Model\Insert; -use Doctrine\DBAL\DBALException; -use Doctrine\ORM\EntityManager; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; use Neos\Utility\TypeHandling; @@ -36,7 +35,10 @@ class SequenceGenerator /** * @param string|object $type + * * @return int + * @throws Exception + * @throws DBALException */ public function getNextNumberFor($type): int { @@ -54,12 +56,11 @@ class SequenceGenerator /** * @param int $count - * @param string|object $type + * @param string $type * @return bool */ - protected function validateFreeNumber(int $count, $type) + protected function validateFreeNumber(int $count, string $type): bool { - /* @var EntityManager $em */ $em = $this->entityManager; try { $em->getConnection()->insert( @@ -83,7 +84,9 @@ class SequenceGenerator /** * @param int $to * @param string|object $type + * * @return bool + * @throws Exception */ public function advanceTo(int $to, $type): bool { @@ -94,15 +97,17 @@ class SequenceGenerator /** * @param string|object $type + * * @return int + * @throws Exception + * @throws DBALException */ public function getLastNumberFor($type): int { - /* @var EntityManager $em */ - $em = $this->entityManager; - - return (int) $em->getConnection()->executeQuery( - 'SELECT MAX(number) FROM ' . $em->getClassMetadata(Insert::class)->getTableName() . ' WHERE type = :type', + return (int) $this->entityManager->getConnection()->executeQuery( + 'SELECT MAX(number) FROM ' + . $this->entityManager->getClassMetadata(Insert::class)->getTableName() + . ' WHERE type = :type', ['type' => $this->inferTypeFromSource($type)] )->fetchAll(\PDO::FETCH_COLUMN)[0]; }