DigiComp.Sequence/Classes/DigiComp/Sequence/Service/SequenceGenerator.php

127 lines
3.5 KiB
PHP
Raw Normal View History

2014-04-07 15:25:16 +02:00
<?php
namespace DigiComp\Sequence\Service;
/* *
* This script belongs to the FLOW3 package "DigiComp.Sequence". *
* *
* */
use Doctrine\DBAL\DBALException;
use TYPO3\Flow\Annotations as Flow;
/**
2016-06-24 19:40:43 +02:00
* A SequenceNumber generator working for transactional databases
2014-04-07 15:25:16 +02:00
*
2014-04-16 10:24:05 +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
{
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @var \Doctrine\Common\Persistence\ObjectManager
* @Flow\Inject
*/
protected $entityManager;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @var \TYPO3\Flow\Reflection\ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @var \TYPO3\Flow\Log\SystemLoggerInterface
* @Flow\Inject
*/
protected $systemLogger;
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
/**
* @param string|object $type
*
* @throws \DigiComp\Sequence\Service\Exception
* @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
2016-06-24 19:40:43 +02:00
//TODO: Check for maximal tries, or similar
//TODO: Let increment be configurable per type
do {
$count = $count + 1;
} while (!$this->validateFreeNumber($count, $type));
return $count;
}
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
protected function validateFreeNumber($count, $type)
{
$em = $this->entityManager;
/** @var $em \Doctrine\ORM\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
} else {
$this->systemLogger->logException($e);
}
} catch (\Exception $e) {
$this->systemLogger->logException($e);
}
return false;
}
2014-04-07 15:25:16 +02:00
2016-06-24 19:40:43 +02:00
public function advanceTo($to, $type)
{
$type = $this->inferTypeFromSource($type);
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
/**
* @param string|object $type
2016-06-24 19:40:43 +02:00
*
* @return int
*/
public function getLastNumberFor($type)
{
$type = $this->inferTypeFromSource($type);
2016-06-24 19:40:43 +02:00
/** @var $em \Doctrine\ORM\EntityManager */
$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',
['type' => $type]
2016-06-24 19:40:43 +02:00
);
$count = $result->fetchAll();
$count = $count[0]['count'];
return $count;
}
/**
* @param string|object $stringOrObject
*
* @throws Exception
* @return string
*/
protected function inferTypeFromSource($stringOrObject) {
if (is_object($stringOrObject)) {
$stringOrObject = $this->reflectionService->getClassNameByObject($stringOrObject);
}
if (!$stringOrObject) {
throw new Exception('No Type given');
}
return $stringOrObject;
}
2016-06-24 19:40:43 +02:00
}