Better type handling and exception annotations

This commit is contained in:
Ferdinand Kuhl 2021-06-01 18:46:16 +02:00
parent 7c60f2f55e
commit 8d2c150141
2 changed files with 16 additions and 11 deletions

View file

@ -27,7 +27,7 @@ class SequenceCommandController extends CommandController
* @param int $to * @param int $to
* @param string $type * @param string $type
*/ */
public function advanceCommand($to, $type) public function advanceCommand(int $to, string $type): void
{ {
$this->sequenceGenerator->advanceTo($to, $type); $this->sequenceGenerator->advanceTo($to, $type);
} }

View file

@ -5,8 +5,7 @@ declare(strict_types=1);
namespace DigiComp\Sequence\Service; namespace DigiComp\Sequence\Service;
use DigiComp\Sequence\Domain\Model\Insert; use DigiComp\Sequence\Domain\Model\Insert;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Neos\Flow\Annotations as Flow; use Neos\Flow\Annotations as Flow;
use Neos\Utility\TypeHandling; use Neos\Utility\TypeHandling;
@ -36,7 +35,10 @@ class SequenceGenerator
/** /**
* @param string|object $type * @param string|object $type
*
* @return int * @return int
* @throws Exception
* @throws DBALException
*/ */
public function getNextNumberFor($type): int public function getNextNumberFor($type): int
{ {
@ -54,12 +56,11 @@ class SequenceGenerator
/** /**
* @param int $count * @param int $count
* @param string|object $type * @param string $type
* @return bool * @return bool
*/ */
protected function validateFreeNumber(int $count, $type) protected function validateFreeNumber(int $count, string $type): bool
{ {
/* @var EntityManager $em */
$em = $this->entityManager; $em = $this->entityManager;
try { try {
$em->getConnection()->insert( $em->getConnection()->insert(
@ -83,7 +84,9 @@ class SequenceGenerator
/** /**
* @param int $to * @param int $to
* @param string|object $type * @param string|object $type
*
* @return bool * @return bool
* @throws Exception
*/ */
public function advanceTo(int $to, $type): bool public function advanceTo(int $to, $type): bool
{ {
@ -94,15 +97,17 @@ class SequenceGenerator
/** /**
* @param string|object $type * @param string|object $type
*
* @return int * @return int
* @throws Exception
* @throws DBALException
*/ */
public function getLastNumberFor($type): int public function getLastNumberFor($type): int
{ {
/* @var EntityManager $em */ return (int) $this->entityManager->getConnection()->executeQuery(
$em = $this->entityManager; 'SELECT MAX(number) FROM '
. $this->entityManager->getClassMetadata(Insert::class)->getTableName()
return (int) $em->getConnection()->executeQuery( . ' WHERE type = :type',
'SELECT MAX(number) FROM ' . $em->getClassMetadata(Insert::class)->getTableName() . ' WHERE type = :type',
['type' => $this->inferTypeFromSource($type)] ['type' => $this->inferTypeFromSource($type)]
)->fetchAll(\PDO::FETCH_COLUMN)[0]; )->fetchAll(\PDO::FETCH_COLUMN)[0];
} }