diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index 076cd53..d57b4fc 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -1,8 +1,12 @@ 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]); + } + } } diff --git a/Classes/Domain/Model/Insert.php b/Classes/Domain/Model/Insert.php index 6e95e41..97b0cb8 100644 --- a/Classes/Domain/Model/Insert.php +++ b/Classes/Domain/Model/Insert.php @@ -1,5 +1,7 @@ setNumber($number); $this->setType($type); @@ -42,7 +44,7 @@ class Insert /** * @return int */ - public function getNumber() + public function getNumber(): int { return $this->number; } @@ -50,7 +52,7 @@ class Insert /** * @param int $number */ - public function setNumber($number) + public function setNumber(int $number): void { $this->number = $number; } @@ -58,7 +60,7 @@ class Insert /** * @return string */ - public function getType() + public function getType(): string { return $this->type; } @@ -66,10 +68,10 @@ class Insert /** * @param string|object $type */ - public function setType($type) + public function setType($type): void { - if (is_object($type)) { - $type = get_class($type); + if (\is_object($type)) { + $type = \get_class($type); } $this->type = $type; } diff --git a/Classes/Service/Exception.php b/Classes/Service/Exception.php index 37b66ed..96a3ad7 100644 --- a/Classes/Service/Exception.php +++ b/Classes/Service/Exception.php @@ -1,5 +1,7 @@ inferTypeFromSource($type); $count = $this->getLastNumberFor($type); @@ -60,12 +56,11 @@ class SequenceGenerator /** * @param int $count - * @param string|object $type + * @param string $type * @return bool */ - protected function validateFreeNumber($count, $type) + protected function validateFreeNumber(int $count, string $type): bool { - /* @var EntityManager $em */ $em = $this->entityManager; try { $em->getConnection()->insert( @@ -77,10 +72,10 @@ class SequenceGenerator return false; } catch (DBALException $e) { if (! $e->getPrevious() instanceof \PDOException) { - $this->systemLogger->logException($e); + $this->systemLogger->critical('Exception occured: ' . $e->getMessage()); } } catch (\Exception $e) { - $this->systemLogger->logException($e); + $this->systemLogger->critical('Exception occured: ' . $e->getMessage()); } return false; @@ -89,9 +84,11 @@ class SequenceGenerator /** * @param int $to * @param string|object $type + * * @return bool + * @throws Exception */ - public function advanceTo($to, $type) + public function advanceTo(int $to, $type): bool { $type = $this->inferTypeFromSource($type); @@ -100,15 +97,17 @@ class SequenceGenerator /** * @param string|object $type + * * @return int + * @throws Exception + * @throws DBALException */ - public function getLastNumberFor($type) + public function getLastNumberFor($type): int { - /* @var EntityManager $em */ - $em = $this->entityManager; - - return $em->getConnection()->executeQuery( - 'SELECT MAX(number) FROM ' . $em->getClassMetadata(Insert::class)->getTableName() . ' WHERE type = :type', + return (int) $this->entityManager->getConnection()->executeQuery( + 'SELECT MAX(number) FROM ' + . $this->entityManager->getClassMetadata(Insert::class)->getTableName() + . ' WHERE type = :type', ['type' => $this->inferTypeFromSource($type)] )->fetchAll(\PDO::FETCH_COLUMN)[0]; } @@ -118,9 +117,9 @@ class SequenceGenerator * @return string * @throws Exception */ - protected function inferTypeFromSource($stringOrObject) + protected function inferTypeFromSource($stringOrObject): string { - if (is_object($stringOrObject)) { + if (\is_object($stringOrObject)) { $stringOrObject = TypeHandling::getTypeForValue($stringOrObject); } if (! $stringOrObject) { diff --git a/composer.json b/composer.json index 9500219..806f08f 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "license": "MIT", "homepage": "https://github.com/digicomp/DigiComp.Sequence", "require": { - "neos/flow": "~4.1|~5.3" + "neos/flow": "~5.3 | ^6.3.5", + "php": "^7.4" }, "require-dev": { "phpunit/phpunit": "3.7.*", @@ -33,7 +34,8 @@ "extra": { "branch-alias": { "dev-version/1.x-dev": "1.1.x-dev", - "dev-develop": "2.0.x-dev" + "dev-version/2.x-dev": "2.1.x-dev", + "dev-develop": "3.0.x-dev" }, "applied-flow-migrations": [ "Inwebs.Basket-201409170938", @@ -70,4 +72,4 @@ "Neos.Flow-20180415105700" ] } -} \ No newline at end of file +}