DigiComp.Sequence/Classes/Service/SequenceGenerator.php

137 lines
3.3 KiB
PHP
Raw Normal View History

2014-04-07 15:25:16 +02:00
<?php
namespace DigiComp\Sequence\Service;
use DigiComp\Sequence\Domain\Model\Insert;
2017-03-13 16:59:04 +01:00
use Doctrine\Common\Persistence\ObjectManager;
2014-04-07 15:25:16 +02:00
use Doctrine\DBAL\DBALException;
2017-03-13 16:59:04 +01:00
use Doctrine\ORM\EntityManager;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\SystemLoggerInterface;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Utility\TypeHandling;
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
{
/**
2017-03-13 16:59:04 +01:00
* @var ObjectManager
2016-06-24 19:40:43 +02:00
* @Flow\Inject
*/
protected $entityManager;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
2017-03-13 16:59:04 +01:00
* @var ReflectionService
2016-06-24 19:40:43 +02:00
* @Flow\Inject
2019-10-28 10:31:11 +01:00
* @deprecated
2016-06-24 19:40:43 +02:00
*/
protected $reflectionService;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
2017-03-13 16:59:04 +01:00
* @var SystemLoggerInterface
2016-06-24 19:40:43 +02:00
* @Flow\Inject
*/
protected $systemLogger;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @param string|object $type
*
* @return int
*/
public function getNextNumberFor($type)
{
$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|object $type
*
* @return bool
*/
2016-06-24 19:40:43 +02:00
protected function validateFreeNumber($count, $type)
{
/* @var EntityManager $em */
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) {
2016-06-24 19:40:43 +02:00
$this->systemLogger->logException($e);
}
} catch (\Exception $e) {
$this->systemLogger->logException($e);
}
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
*
* @return bool
*/
2016-06-24 19:40:43 +02:00
public function advanceTo($to, $type)
{
$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
*/
public function getLastNumberFor($type)
{
/* @var EntityManager $em */
2016-06-24 19:40:43 +02:00
$em = $this->entityManager;
2014-04-07 15:25:16 +02:00
return $em->getConnection()->executeQuery(
'SELECT MAX(number) FROM ' . $em->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
*/
2017-03-13 16:59:04 +01:00
protected function inferTypeFromSource($stringOrObject)
{
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
}