2014-04-07 15:25:16 +02:00
|
|
|
<?php
|
2020-03-10 11:13:12 +01:00
|
|
|
|
2021-04-20 01:18:17 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2014-04-07 15:25:16 +02:00
|
|
|
namespace DigiComp\Sequence\Service;
|
|
|
|
|
2018-04-04 23:48:06 +02:00
|
|
|
use DigiComp\Sequence\Domain\Model\Insert;
|
2021-06-01 18:46:16 +02:00
|
|
|
use Doctrine\DBAL\Exception as DBALException;
|
2020-10-15 21:14:03 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-03-13 16:59:04 +01:00
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Utility\TypeHandling;
|
2020-10-15 21:14:03 +02:00
|
|
|
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
|
2020-10-15 21:14:03 +02:00
|
|
|
* @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
|
2020-10-15 21:14:03 +02:00
|
|
|
* @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
|
2021-06-01 18:46:16 +02:00
|
|
|
*
|
2016-06-24 19:40:43 +02:00
|
|
|
* @return int
|
2021-06-01 18:46:16 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @throws DBALException
|
2016-06-24 19:40:43 +02:00
|
|
|
*/
|
2021-04-20 01:18:17 +02:00
|
|
|
public function getNextNumberFor($type): int
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
2016-06-24 20:35:59 +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
|
2021-06-01 18:46:16 +02:00
|
|
|
* @param string $type
|
2017-03-13 16:59:04 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-01 18:46:16 +02:00
|
|
|
protected function validateFreeNumber(int $count, string $type): bool
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
|
|
|
$em = $this->entityManager;
|
|
|
|
try {
|
|
|
|
$em->getConnection()->insert(
|
2018-04-04 23:48:06 +02:00
|
|
|
$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) {
|
2019-12-04 12:48:02 +01:00
|
|
|
if (! $e->getPrevious() instanceof \PDOException) {
|
2020-10-15 21:14:03 +02:00
|
|
|
$this->systemLogger->critical('Exception occured: ' . $e->getMessage());
|
2016-06-24 19:40:43 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2020-10-15 21:14:03 +02:00
|
|
|
$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
|
2021-06-01 18:46:16 +02:00
|
|
|
*
|
2017-03-13 16:59:04 +01:00
|
|
|
* @return bool
|
2021-06-01 18:46:16 +02:00
|
|
|
* @throws Exception
|
2017-03-13 16:59:04 +01:00
|
|
|
*/
|
2021-04-20 01:18:17 +02:00
|
|
|
public function advanceTo(int $to, $type): bool
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
2016-06-24 20:35:59 +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
|
|
|
/**
|
2016-06-24 20:35:59 +02:00
|
|
|
* @param string|object $type
|
2021-06-01 18:46:16 +02:00
|
|
|
*
|
2016-06-24 19:40:43 +02:00
|
|
|
* @return int
|
2021-06-01 18:46:16 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @throws DBALException
|
2016-06-24 19:40:43 +02:00
|
|
|
*/
|
2021-04-20 01:18:17 +02:00
|
|
|
public function getLastNumberFor($type): int
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
2021-06-01 18:46:16 +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)]
|
2018-04-04 23:50:19 +02:00
|
|
|
)->fetchAll(\PDO::FETCH_COLUMN)[0];
|
2016-06-24 19:40:43 +02:00
|
|
|
}
|
2016-06-24 20:35:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|object $stringOrObject
|
|
|
|
* @return string
|
2018-04-03 10:23:53 +02:00
|
|
|
* @throws Exception
|
2016-06-24 20:35:59 +02:00
|
|
|
*/
|
2021-04-20 01:18:17 +02:00
|
|
|
protected function inferTypeFromSource($stringOrObject): string
|
2017-03-13 16:59:04 +01:00
|
|
|
{
|
2016-06-24 20:35:59 +02:00
|
|
|
if (is_object($stringOrObject)) {
|
2017-03-13 16:59:04 +01:00
|
|
|
$stringOrObject = TypeHandling::getTypeForValue($stringOrObject);
|
2016-06-24 20:35:59 +02:00
|
|
|
}
|
2017-03-13 16:59:04 +01:00
|
|
|
if (! $stringOrObject) {
|
2016-06-24 20:35:59 +02:00
|
|
|
throw new Exception('No Type given');
|
|
|
|
}
|
2017-03-13 16:59:04 +01:00
|
|
|
|
2016-06-24 20:35:59 +02:00
|
|
|
return $stringOrObject;
|
|
|
|
}
|
2016-06-24 19:40:43 +02:00
|
|
|
}
|