DigiComp.Sequence/Classes/Service/SequenceGenerator.php

132 lines
3.4 KiB
PHP
Raw Normal View History

2014-04-07 15:25:16 +02:00
<?php
declare(strict_types=1);
2014-04-07 15:25:16 +02:00
namespace DigiComp\Sequence\Service;
use DigiComp\Sequence\Domain\Model\Insert;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManagerInterface;
2017-03-13 16:59:04 +01:00
use Neos\Flow\Annotations as Flow;
use Neos\Utility\TypeHandling;
use Psr\Log\LoggerInterface;
2014-04-07 15:25:16 +02:00
/**
2016-06-24 19:40:43 +02:00
* A SequenceNumber generator working for transactional databases
2014-04-07 15:25:16 +02:00
*
2016-06-24 19:40:43 +02:00
* Thoughts: We could make the step-range configurable, and if > 1 we could return new keys immediately for this
* request, as we "reserved" the space between.
2014-04-16 10:24:05 +02:00
*
2014-04-07 15:25:16 +02:00
* @Flow\Scope("singleton")
*/
2016-06-24 19:40:43 +02:00
class SequenceGenerator
{
/**
* @Flow\Inject
* @var EntityManagerInterface
2016-06-24 19:40:43 +02:00
*/
protected $entityManager;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @Flow\Inject
* @var LoggerInterface
2016-06-24 19:40:43 +02:00
*/
protected $systemLogger;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @param string|object $type
*
2016-06-24 19:40:43 +02:00
* @return int
* @throws Exception
* @throws DBALException
2016-06-24 19:40:43 +02:00
*/
public function getNextNumberFor($type): int
2016-06-24 19:40:43 +02:00
{
$type = $this->inferTypeFromSource($type);
2016-06-24 19:40:43 +02:00
$count = $this->getLastNumberFor($type);
2014-04-07 15:25:16 +02:00
2017-03-13 16:59:04 +01:00
// TODO: Check for maximal tries, or similar
// TODO: Let increment be configurable per type
2016-06-24 19:40:43 +02:00
do {
2017-03-13 16:59:04 +01:00
$count++;
} while (! $this->validateFreeNumber($count, $type));
2016-06-24 19:40:43 +02:00
return $count;
}
2014-04-07 15:25:16 +02:00
2017-03-13 16:59:04 +01:00
/**
* @param int $count
* @param string $type
2017-03-13 16:59:04 +01:00
* @return bool
*/
protected function validateFreeNumber(int $count, string $type): bool
2016-06-24 19:40:43 +02:00
{
$em = $this->entityManager;
try {
$em->getConnection()->insert(
$em->getClassMetadata(Insert::class)->getTableName(),
2016-06-24 19:40:43 +02:00
['number' => $count, 'type' => $type]
);
return true;
} catch (\PDOException $e) {
return false;
} catch (DBALException $e) {
if (! $e->getPrevious() instanceof \PDOException) {
$this->systemLogger->critical('Exception occured: ' . $e->getMessage());
2016-06-24 19:40:43 +02:00
}
} catch (\Exception $e) {
$this->systemLogger->critical('Exception occured: ' . $e->getMessage());
2016-06-24 19:40:43 +02:00
}
2017-03-13 16:59:04 +01:00
2016-06-24 19:40:43 +02:00
return false;
}
2014-04-07 15:25:16 +02:00
2017-03-13 16:59:04 +01:00
/**
* @param int $to
* @param string|object $type
*
2017-03-13 16:59:04 +01:00
* @return bool
* @throws Exception
2017-03-13 16:59:04 +01:00
*/
public function advanceTo(int $to, $type): bool
2016-06-24 19:40:43 +02:00
{
$type = $this->inferTypeFromSource($type);
2017-03-13 16:59:04 +01:00
2018-04-04 22:48:18 +02:00
return $this->validateFreeNumber($to, $type);
2016-06-24 19:40:43 +02:00
}
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @param string|object $type
*
2016-06-24 19:40:43 +02:00
* @return int
* @throws Exception
* @throws DBALException
2016-06-24 19:40:43 +02:00
*/
public function getLastNumberFor($type): int
2016-06-24 19:40:43 +02:00
{
return (int) $this->entityManager->getConnection()->executeQuery(
'SELECT MAX(number) FROM '
. $this->entityManager->getClassMetadata(Insert::class)->getTableName()
. ' WHERE type = :type',
2017-03-13 16:59:04 +01:00
['type' => $this->inferTypeFromSource($type)]
)->fetchAll(\PDO::FETCH_COLUMN)[0];
2016-06-24 19:40:43 +02:00
}
/**
* @param string|object $stringOrObject
* @return string
2018-04-03 10:23:53 +02:00
* @throws Exception
*/
protected function inferTypeFromSource($stringOrObject): string
2017-03-13 16:59:04 +01:00
{
if (is_object($stringOrObject)) {
2017-03-13 16:59:04 +01:00
$stringOrObject = TypeHandling::getTypeForValue($stringOrObject);
}
2017-03-13 16:59:04 +01:00
if (! $stringOrObject) {
throw new Exception('No Type given');
}
2017-03-13 16:59:04 +01:00
return $stringOrObject;
}
2016-06-24 19:40:43 +02:00
}