2014-04-07 15:25:16 +02:00
|
|
|
<?php
|
|
|
|
namespace DigiComp\Sequence\Service;
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
|
* @param string|object $type
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-06-24 19:40:43 +02:00
|
|
|
protected function validateFreeNumber($count, $type)
|
|
|
|
{
|
2017-03-13 16:59:04 +01:00
|
|
|
/** @var $em EntityManager */
|
2016-06-24 19:40:43 +02:00
|
|
|
$em = $this->entityManager;
|
|
|
|
try {
|
|
|
|
$em->getConnection()->insert(
|
|
|
|
'digicomp_sequence_domain_model_insert',
|
|
|
|
['number' => $count, 'type' => $type]
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) {
|
|
|
|
// Do nothing, new Doctrine handling hides the above error
|
2018-04-03 10:23:53 +02:00
|
|
|
} else {
|
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)
|
|
|
|
{
|
2016-06-24 20:35:59 +02:00
|
|
|
$type = $this->inferTypeFromSource($type);
|
2017-03-13 16:59:04 +01:00
|
|
|
|
2016-06-24 19:40:43 +02:00
|
|
|
return ($this->validateFreeNumber($to, $type));
|
|
|
|
}
|
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
|
2016-06-24 19:40:43 +02:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLastNumberFor($type)
|
|
|
|
{
|
2017-03-13 16:59:04 +01:00
|
|
|
/** @var $em EntityManager */
|
2016-06-24 19:40:43 +02:00
|
|
|
$em = $this->entityManager;
|
2014-04-07 15:25:16 +02:00
|
|
|
|
2016-06-24 19:40:43 +02:00
|
|
|
$result = $em->getConnection()->executeQuery(
|
|
|
|
'SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type',
|
2017-03-13 16:59:04 +01:00
|
|
|
['type' => $this->inferTypeFromSource($type)]
|
2016-06-24 19:40:43 +02:00
|
|
|
);
|
|
|
|
$count = $result->fetchAll();
|
|
|
|
$count = $count[0]['count'];
|
2017-03-13 16:59:04 +01:00
|
|
|
|
2016-06-24 19:40:43 +02:00
|
|
|
return $count;
|
|
|
|
}
|
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
|
|
|
*/
|
2017-03-13 16:59:04 +01:00
|
|
|
protected function inferTypeFromSource($stringOrObject)
|
|
|
|
{
|
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
|
|
|
}
|