From c180a50731d2315a449c2cf6d168bf980d4ecfe4 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 15 Oct 2020 14:52:58 +0200 Subject: [PATCH 1/7] TASK: Allow Neos Flow 6.3 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9500219..8f5d667 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "license": "MIT", "homepage": "https://github.com/digicomp/DigiComp.Sequence", "require": { - "neos/flow": "~4.1|~5.3" + "neos/flow": "~4.1|~5.3|~6.3" }, "require-dev": { "phpunit/phpunit": "3.7.*", @@ -70,4 +70,4 @@ "Neos.Flow-20180415105700" ] } -} \ No newline at end of file +} From e8b75b33dd6907cfb3e6311d5381ec7fb5a476bd Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 15 Oct 2020 21:14:03 +0200 Subject: [PATCH 2/7] FIX: Adjusting to Flow 6.3 (Logger and EntityManager) --- Classes/Service/SequenceGenerator.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/Classes/Service/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index ba8a0ea..2477e1f 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -3,13 +3,12 @@ namespace DigiComp\Sequence\Service; use DigiComp\Sequence\Domain\Model\Insert; -use Doctrine\Common\Persistence\ObjectManager; use Doctrine\DBAL\DBALException; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; -use Neos\Flow\Log\SystemLoggerInterface; -use Neos\Flow\Reflection\ReflectionService; use Neos\Utility\TypeHandling; +use Psr\Log\LoggerInterface; /** * A SequenceNumber generator working for transactional databases @@ -22,21 +21,14 @@ use Neos\Utility\TypeHandling; class SequenceGenerator { /** - * @var ObjectManager * @Flow\Inject + * @var EntityManagerInterface */ protected $entityManager; /** - * @var ReflectionService - * @Flow\Inject - * @deprecated - */ - protected $reflectionService; - - /** - * @var SystemLoggerInterface * @Flow\Inject + * @var LoggerInterface */ protected $systemLogger; @@ -77,10 +69,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; From 7c60f2f55e4f52718c2db9ddd787d20629a1d944 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 20 Apr 2021 01:18:17 +0200 Subject: [PATCH 3/7] TASK: Adding PHP 7.4 type hints everywhere --- Classes/Command/SequenceCommandController.php | 2 ++ Classes/Domain/Model/Insert.php | 16 +++++++++------- Classes/Service/Exception.php | 2 ++ Classes/Service/SequenceGenerator.php | 14 ++++++++------ composer.json | 5 +++-- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index 076cd53..da8a129 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.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,7 +68,7 @@ class Insert /** * @param string|object $type */ - public function setType($type) + public function setType($type): void { if (is_object($type)) { $type = get_class($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); @@ -55,7 +57,7 @@ class SequenceGenerator * @param string|object $type * @return bool */ - protected function validateFreeNumber($count, $type) + protected function validateFreeNumber(int $count, $type) { /* @var EntityManager $em */ $em = $this->entityManager; @@ -83,7 +85,7 @@ class SequenceGenerator * @param string|object $type * @return bool */ - public function advanceTo($to, $type) + public function advanceTo(int $to, $type): bool { $type = $this->inferTypeFromSource($type); @@ -94,12 +96,12 @@ class SequenceGenerator * @param string|object $type * @return int */ - public function getLastNumberFor($type) + public function getLastNumberFor($type): int { /* @var EntityManager $em */ $em = $this->entityManager; - return $em->getConnection()->executeQuery( + return (int) $em->getConnection()->executeQuery( 'SELECT MAX(number) FROM ' . $em->getClassMetadata(Insert::class)->getTableName() . ' WHERE type = :type', ['type' => $this->inferTypeFromSource($type)] )->fetchAll(\PDO::FETCH_COLUMN)[0]; @@ -110,7 +112,7 @@ class SequenceGenerator * @return string * @throws Exception */ - protected function inferTypeFromSource($stringOrObject) + protected function inferTypeFromSource($stringOrObject): string { if (is_object($stringOrObject)) { $stringOrObject = TypeHandling::getTypeForValue($stringOrObject); diff --git a/composer.json b/composer.json index 8f5d667..6e5e1c3 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|~6.3" + "neos/flow": "~5.3 | ^6.3.5", + "php": "^7.4" }, "require-dev": { "phpunit/phpunit": "3.7.*", @@ -33,7 +34,7 @@ "extra": { "branch-alias": { "dev-version/1.x-dev": "1.1.x-dev", - "dev-develop": "2.0.x-dev" + "dev-develop": "3.0.x-dev" }, "applied-flow-migrations": [ "Inwebs.Basket-201409170938", From 8d2c150141cf6726298751d23917f476b9b430a6 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 1 Jun 2021 18:46:16 +0200 Subject: [PATCH 4/7] Better type handling and exception annotations --- Classes/Command/SequenceCommandController.php | 2 +- Classes/Service/SequenceGenerator.php | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index da8a129..47e388b 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -27,7 +27,7 @@ class SequenceCommandController extends CommandController * @param int $to * @param string $type */ - public function advanceCommand($to, $type) + public function advanceCommand(int $to, string $type): void { $this->sequenceGenerator->advanceTo($to, $type); } diff --git a/Classes/Service/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index cdbc9d6..12aca7e 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -5,8 +5,7 @@ declare(strict_types=1); namespace DigiComp\Sequence\Service; use DigiComp\Sequence\Domain\Model\Insert; -use Doctrine\DBAL\DBALException; -use Doctrine\ORM\EntityManager; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; use Neos\Utility\TypeHandling; @@ -36,7 +35,10 @@ class SequenceGenerator /** * @param string|object $type + * * @return int + * @throws Exception + * @throws DBALException */ public function getNextNumberFor($type): int { @@ -54,12 +56,11 @@ class SequenceGenerator /** * @param int $count - * @param string|object $type + * @param string $type * @return bool */ - protected function validateFreeNumber(int $count, $type) + protected function validateFreeNumber(int $count, string $type): bool { - /* @var EntityManager $em */ $em = $this->entityManager; try { $em->getConnection()->insert( @@ -83,7 +84,9 @@ class SequenceGenerator /** * @param int $to * @param string|object $type + * * @return bool + * @throws Exception */ public function advanceTo(int $to, $type): bool { @@ -94,15 +97,17 @@ class SequenceGenerator /** * @param string|object $type + * * @return int + * @throws Exception + * @throws DBALException */ public function getLastNumberFor($type): int { - /* @var EntityManager $em */ - $em = $this->entityManager; - - return (int) $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]; } From c9ce9a90fdcb38789e5963d47749d9b512218df0 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 29 Jun 2021 14:23:25 +0200 Subject: [PATCH 5/7] Added a command to clean up old and obsolete sequence entries --- Classes/Command/SequenceCommandController.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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]); + } + } } From 75528b78b290a6b49e15e4565ec4772000bc2b4e Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 26 Aug 2021 10:50:00 +0200 Subject: [PATCH 6/7] PHPCBF run --- Classes/Command/SequenceCommandController.php | 2 +- Classes/Domain/Model/Insert.php | 8 ++++---- Classes/Service/SequenceGenerator.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index d91a6aa..d57b4fc 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -18,8 +18,8 @@ use Neos\Flow\Cli\CommandController; class SequenceCommandController extends CommandController { /** - * @var SequenceGenerator * @Flow\Inject + * @var SequenceGenerator */ protected $sequenceGenerator; diff --git a/Classes/Domain/Model/Insert.php b/Classes/Domain/Model/Insert.php index 9e1a0fc..97b0cb8 100644 --- a/Classes/Domain/Model/Insert.php +++ b/Classes/Domain/Model/Insert.php @@ -18,16 +18,16 @@ use Neos\Flow\Annotations as Flow; class Insert { /** - * @var int * @Flow\Identity * @ORM\Id + * @var int */ protected int $number; /** - * @var string * @Flow\Identity * @ORM\Id + * @var string */ protected string $type; @@ -70,8 +70,8 @@ class Insert */ 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/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index 12aca7e..715e6ab 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -119,7 +119,7 @@ class SequenceGenerator */ protected function inferTypeFromSource($stringOrObject): string { - if (is_object($stringOrObject)) { + if (\is_object($stringOrObject)) { $stringOrObject = TypeHandling::getTypeForValue($stringOrObject); } if (! $stringOrObject) { From d97849a4e28f7f6dd7a3d31dfa3d5d044a5b4820 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 26 Aug 2021 10:52:22 +0200 Subject: [PATCH 7/7] Adding branch alias vor 2.x --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 6e5e1c3..806f08f 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "extra": { "branch-alias": { "dev-version/1.x-dev": "1.1.x-dev", + "dev-version/2.x-dev": "2.1.x-dev", "dev-develop": "3.0.x-dev" }, "applied-flow-migrations": [