From 3d439a05cddd9e70127da282d4c798d8b1311470 Mon Sep 17 00:00:00 2001 From: siganushka Date: Sat, 16 Nov 2024 12:56:08 +0800 Subject: [PATCH] refactor: remove TimestampTokenGenerator & improving UuidTokenGenerator --- .../SiganushkaRequestTokenExtension.php | 6 ++++++ src/Generator/TimestampTokenGenerator.php | 15 --------------- src/Generator/UniqidTokenGenerator.php | 2 +- src/Generator/UuidTokenGenerator.php | 18 ++++++++++++------ .../SiganushkaRequestTokenExtensionTest.php | 3 --- .../Generator/TimestampTokenGeneratorTest.php | 19 ------------------- 6 files changed, 19 insertions(+), 44 deletions(-) delete mode 100644 src/Generator/TimestampTokenGenerator.php delete mode 100644 tests/Generator/TimestampTokenGeneratorTest.php diff --git a/src/DependencyInjection/SiganushkaRequestTokenExtension.php b/src/DependencyInjection/SiganushkaRequestTokenExtension.php index 4f2a887..739b6ca 100644 --- a/src/DependencyInjection/SiganushkaRequestTokenExtension.php +++ b/src/DependencyInjection/SiganushkaRequestTokenExtension.php @@ -6,12 +6,14 @@ use Siganushka\RequestTokenBundle\EventListener\RequestTokenListener; use Siganushka\RequestTokenBundle\Generator\RequestTokenGeneratorInterface; +use Siganushka\RequestTokenBundle\Generator\UuidTokenGenerator; use Siganushka\RequestTokenBundle\Monolog\Processor\RequestTokenProcessor; use Symfony\Bundle\MonologBundle\MonologBundle; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; +use Symfony\Component\Uid\Uuid; class SiganushkaRequestTokenExtension extends Extension { @@ -34,6 +36,10 @@ public function load(array $configs, ContainerBuilder $container): void $requestTokenProcessorDef->setArgument('$headerName', $config['header_name']); $requestTokenProcessorDef->addTag('monolog.processor'); + if (!class_exists(Uuid::class)) { + $container->removeDefinition(UuidTokenGenerator::class); + } + if (!class_exists(MonologBundle::class)) { $container->removeDefinition(RequestTokenProcessor::class); } diff --git a/src/Generator/TimestampTokenGenerator.php b/src/Generator/TimestampTokenGenerator.php deleted file mode 100644 index c0d8819..0000000 --- a/src/Generator/TimestampTokenGenerator.php +++ /dev/null @@ -1,15 +0,0 @@ -factory = $factory ?? new UuidFactory(); + } - return (string) Uuid::v1(); + public function generate(): string + { + return (string) $this->factory->create(); } } diff --git a/tests/DependencyInjection/SiganushkaRequestTokenExtensionTest.php b/tests/DependencyInjection/SiganushkaRequestTokenExtensionTest.php index d08445f..e791845 100644 --- a/tests/DependencyInjection/SiganushkaRequestTokenExtensionTest.php +++ b/tests/DependencyInjection/SiganushkaRequestTokenExtensionTest.php @@ -8,7 +8,6 @@ use Siganushka\RequestTokenBundle\DependencyInjection\SiganushkaRequestTokenExtension; use Siganushka\RequestTokenBundle\EventListener\RequestTokenListener; use Siganushka\RequestTokenBundle\Generator\RequestTokenGeneratorInterface; -use Siganushka\RequestTokenBundle\Generator\TimestampTokenGenerator; use Siganushka\RequestTokenBundle\Generator\UniqidTokenGenerator; use Siganushka\RequestTokenBundle\Generator\UuidTokenGenerator; use Siganushka\RequestTokenBundle\Monolog\Processor\RequestTokenProcessor; @@ -23,7 +22,6 @@ public function testLoadDefaultConfig(): void static::assertFalse($container->hasAlias(RequestTokenGeneratorInterface::class)); static::assertFalse($container->hasDefinition(RequestTokenListener::class)); - static::assertFalse($container->hasDefinition(TimestampTokenGenerator::class)); static::assertFalse($container->hasDefinition(UniqidTokenGenerator::class)); static::assertFalse($container->hasDefinition(UuidTokenGenerator::class)); static::assertFalse($container->hasDefinition(RequestTokenProcessor::class)); @@ -41,7 +39,6 @@ public function testLoadCustomConfig(): void static::assertTrue($container->hasAlias(RequestTokenGeneratorInterface::class)); static::assertTrue($container->hasDefinition(RequestTokenListener::class)); - static::assertTrue($container->hasDefinition(TimestampTokenGenerator::class)); static::assertTrue($container->hasDefinition(UniqidTokenGenerator::class)); static::assertTrue($container->hasDefinition(UuidTokenGenerator::class)); static::assertTrue($container->hasDefinition(RequestTokenProcessor::class)); diff --git a/tests/Generator/TimestampTokenGeneratorTest.php b/tests/Generator/TimestampTokenGeneratorTest.php deleted file mode 100644 index 1697d53..0000000 --- a/tests/Generator/TimestampTokenGeneratorTest.php +++ /dev/null @@ -1,19 +0,0 @@ -generate(); - - static::assertNotEmpty($token); - } -}