diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index 47e388b..d91a6aa 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace DigiComp\Sequence\Command; +use DigiComp\Sequence\Domain\Model\Insert; use DigiComp\Sequence\Service\SequenceGenerator; +use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; use Neos\Flow\Cli\CommandController; @@ -21,6 +23,12 @@ class SequenceCommandController extends CommandController */ protected $sequenceGenerator; + /** + * @Flow\Inject + * @var EntityManagerInterface + */ + protected $entityManager; + /** * Sets minimum number for sequence generator * @@ -32,5 +40,28 @@ class SequenceCommandController extends CommandController $this->sequenceGenerator->advanceTo($to, $type); } - // TODO: make clean up job to delete all but the biggest number to save resources + /** + * @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]); + } + } }