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\Command;
|
|
|
|
|
2021-06-29 14:23:25 +02:00
|
|
|
use DigiComp\Sequence\Domain\Model\Insert;
|
2017-03-13 16:59:04 +01:00
|
|
|
use DigiComp\Sequence\Service\SequenceGenerator;
|
2021-06-29 14:23:25 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-03-13 16:59:04 +01:00
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Flow\Cli\CommandController;
|
2014-04-07 15:25:16 +02:00
|
|
|
|
|
|
|
/**
|
2016-06-24 20:35:59 +02:00
|
|
|
* A database agnostic SequenceNumber generator
|
2014-04-07 15:25:16 +02:00
|
|
|
*
|
|
|
|
* @Flow\Scope("singleton")
|
|
|
|
*/
|
2016-06-24 20:35:59 +02:00
|
|
|
class SequenceCommandController extends CommandController
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
|
|
|
/**
|
2017-03-13 16:59:04 +01:00
|
|
|
* @var SequenceGenerator
|
2016-06-24 19:40:43 +02:00
|
|
|
* @Flow\Inject
|
|
|
|
*/
|
|
|
|
protected $sequenceGenerator;
|
2014-04-07 15:25:16 +02:00
|
|
|
|
2021-06-29 14:23:25 +02:00
|
|
|
/**
|
|
|
|
* @Flow\Inject
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
protected $entityManager;
|
|
|
|
|
2016-06-24 19:40:43 +02:00
|
|
|
/**
|
|
|
|
* Sets minimum number for sequence generator
|
|
|
|
*
|
2017-03-13 16:59:04 +01:00
|
|
|
* @param int $to
|
2016-06-24 19:40:43 +02:00
|
|
|
* @param string $type
|
|
|
|
*/
|
2021-06-01 18:46:16 +02:00
|
|
|
public function advanceCommand(int $to, string $type): void
|
2016-06-24 19:40:43 +02:00
|
|
|
{
|
|
|
|
$this->sequenceGenerator->advanceTo($to, $type);
|
|
|
|
}
|
2016-06-24 20:35:59 +02:00
|
|
|
|
2021-06-29 14:23:25 +02:00
|
|
|
/**
|
|
|
|
* @param string[] $typesToClean
|
|
|
|
*/
|
|
|
|
public function cleanSequenceInsertsCommand(array $typesToClean = [])
|
|
|
|
{
|
|
|
|
$cleanArray = [];
|
|
|
|
if (empty($typesToClean)) {
|
|
|
|
$results = $this->entityManager
|
|
|
|
->createQuery('SELECT i.type, MAX(i.number) max_number FROM ' . Insert::class . ' i GROUP BY i.type')
|
|
|
|
->getScalarResult();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$cleanArray[$result['type']] = (int) $result['max_number'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($typesToClean as $typeToClean) {
|
|
|
|
$cleanArray[$typeToClean] = $this->sequenceGenerator->getLastNumberFor($typeToClean);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($cleanArray as $typeToClean => $number) {
|
|
|
|
$this->entityManager
|
|
|
|
->createQuery('DELETE FROM ' . Insert::class . ' i WHERE i.type = ?0 AND i.number < ?1')
|
|
|
|
->execute([$typeToClean, $number]);
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 15:25:16 +02:00
|
|
|
}
|