From cabf69c59528398d48a7dc0cc28e7c9af90fee0a Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Wed, 11 Mar 2020 12:00:42 +0100 Subject: [PATCH 01/12] replaces tabs with spaces --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3bbf571..58a609e 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ integrity of the database of your choice. Usage is quite simple also: - /** - * @param \DigiComp\Sequence\Service\SequenceNumberGenerator $sequenceNumberGenerator - */ - public function __construct(SequenceNumberGenerator $sequenceNumberGenerator) - { - $this->orderId = $sequenceNumberGenerator->getNextNumberFor($this); - } + /** + * @param \DigiComp\Sequence\Service\SequenceNumberGenerator $sequenceNumberGenerator + */ + public function __construct(SequenceNumberGenerator $sequenceNumberGenerator) + { + $this->orderId = $sequenceNumberGenerator->getNextNumberFor($this); + } ``getNextNumberFor`` allows you to give an object which will be resolved to its FQCN or a custom sequence name. From 3fcffcd1f39c802e5df7c052e404c78da47506ec Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Wed, 11 Mar 2020 12:09:37 +0100 Subject: [PATCH 02/12] removed trailing spaces --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 58a609e..a446bd4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ DigiComp.Sequence ------------------------- -This is a very simple and stupid tool, helping in generation of gapless sequences. For this task it relies on key +This is a very simple and stupid tool, helping in generation of gapless sequences. For this task it relies on key integrity of the database of your choice. Usage is quite simple also: @@ -10,9 +10,9 @@ Usage is quite simple also: /** * @param \DigiComp\Sequence\Service\SequenceNumberGenerator $sequenceNumberGenerator */ - public function __construct(SequenceNumberGenerator $sequenceNumberGenerator) + public function __construct(SequenceNumberGenerator $sequenceNumberGenerator) { - $this->orderId = $sequenceNumberGenerator->getNextNumberFor($this); + $this->orderId = $sequenceNumberGenerator->getNextNumberFor($this); } ``getNextNumberFor`` allows you to give an object which will be resolved to its FQCN or a custom sequence name. From 583b1f9d48a3071deb900f1f777efd1d8522768b Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Wed, 11 Mar 2020 17:24:48 +0100 Subject: [PATCH 03/12] optimized migrations --- Migrations/Mysql/Version20140505093853.php | 10 ++++------ Migrations/Mysql/Version20160624203903.php | 10 ++-------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/Migrations/Mysql/Version20140505093853.php b/Migrations/Mysql/Version20140505093853.php index 4f4f5e7..de885e6 100644 --- a/Migrations/Mysql/Version20140505093853.php +++ b/Migrations/Mysql/Version20140505093853.php @@ -25,11 +25,9 @@ class Version20140505093853 extends AbstractMigration public function up(Schema $schema) { // this up() migration is autogenerated, please modify it to your needs - $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".'); - $this->addSql( - "CREATE TABLE digicomp_sequence_domain_model_insert (number INT NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(number, type))" - ); + $this->addSql('CREATE TABLE digicomp_sequence_domain_model_insert (number INT NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(number, type))'); } /** @@ -39,8 +37,8 @@ class Version20140505093853 extends AbstractMigration public function down(Schema $schema) { // this down() migration is autogenerated, please modify it to your needs - $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".'); - $this->addSql("DROP TABLE digicomp_sequence_domain_model_insert"); + $this->addSql('DROP TABLE digicomp_sequence_domain_model_insert'); } } diff --git a/Migrations/Mysql/Version20160624203903.php b/Migrations/Mysql/Version20160624203903.php index 9046786..854cb29 100644 --- a/Migrations/Mysql/Version20160624203903.php +++ b/Migrations/Mysql/Version20160624203903.php @@ -25,10 +25,7 @@ class Version20160624203903 extends AbstractMigration public function up(Schema $schema) { // this up() migration is autogenerated, please modify it to your needs - $this->abortIf( - $this->connection->getDatabasePlatform()->getName() != 'mysql', - 'Migration can only be executed safely on "mysql".' - ); + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".'); $this->addSql('CREATE INDEX type_idx ON digicomp_sequence_domain_model_insert (type)'); } @@ -40,10 +37,7 @@ class Version20160624203903 extends AbstractMigration public function down(Schema $schema) { // this down() migration is autogenerated, please modify it to your needs - $this->abortIf( - $this->connection->getDatabasePlatform()->getName() != 'mysql', - 'Migration can only be executed safely on "mysql".' - ); + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".'); $this->addSql('DROP INDEX type_idx ON digicomp_sequence_domain_model_insert'); } From 39a98c481474f5374e2fc7d57b4c8a1621b8a238 Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Thu, 12 Mar 2020 14:21:32 +0100 Subject: [PATCH 04/12] removed empty lines between @param and @return/@throws --- Classes/Service/SequenceGenerator.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Classes/Service/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index dc43fff..ba8a0ea 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -42,7 +42,6 @@ class SequenceGenerator /** * @param string|object $type - * * @return int */ public function getNextNumberFor($type) @@ -62,7 +61,6 @@ class SequenceGenerator /** * @param int $count * @param string|object $type - * * @return bool */ protected function validateFreeNumber($count, $type) @@ -91,7 +89,6 @@ class SequenceGenerator /** * @param int $to * @param string|object $type - * * @return bool */ public function advanceTo($to, $type) @@ -103,7 +100,6 @@ class SequenceGenerator /** * @param string|object $type - * * @return int */ public function getLastNumberFor($type) @@ -119,7 +115,6 @@ class SequenceGenerator /** * @param string|object $stringOrObject - * * @return string * @throws Exception */ From 3ee24e5381dc80a8295f26c7da62e96c27a1e431 Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Mon, 16 Mar 2020 15:44:37 +0100 Subject: [PATCH 05/12] empty line between opening php tag and namespace --- Migrations/Mysql/Version20140505093853.php | 1 + Migrations/Mysql/Version20160624203903.php | 1 + 2 files changed, 2 insertions(+) diff --git a/Migrations/Mysql/Version20140505093853.php b/Migrations/Mysql/Version20140505093853.php index de885e6..05b424e 100644 --- a/Migrations/Mysql/Version20140505093853.php +++ b/Migrations/Mysql/Version20140505093853.php @@ -1,4 +1,5 @@ Date: Thu, 15 Oct 2020 14:52:58 +0200 Subject: [PATCH 06/12] 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 07/12] 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 08/12] 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 09/12] 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 10/12] 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 11/12] 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 12/12] 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": [