Skip to content

Commit

Permalink
refactor: remove TimestampTokenGenerator & improving UuidTokenGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
siganushka committed Nov 16, 2024
1 parent 18e2efc commit 3d439a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 44 deletions.
6 changes: 6 additions & 0 deletions src/DependencyInjection/SiganushkaRequestTokenExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
15 changes: 0 additions & 15 deletions src/Generator/TimestampTokenGenerator.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Generator/UniqidTokenGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class UniqidTokenGenerator implements RequestTokenGeneratorInterface
{
public function generate(): string
{
return uniqid();
return uniqid('', true);
}
}
18 changes: 12 additions & 6 deletions src/Generator/UuidTokenGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace Siganushka\RequestTokenBundle\Generator;

use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\Factory\NameBasedUuidFactory;
use Symfony\Component\Uid\Factory\RandomBasedUuidFactory;
use Symfony\Component\Uid\Factory\TimeBasedUuidFactory;
use Symfony\Component\Uid\Factory\UuidFactory;

class UuidTokenGenerator implements RequestTokenGeneratorInterface
{
public function generate(): string
private UuidFactory|NameBasedUuidFactory|RandomBasedUuidFactory|TimeBasedUuidFactory $factory;

public function __construct(UuidFactory $factory = null)
{
if (!class_exists(Uuid::class)) {
throw new \LogicException(\sprintf('The "%s" class requires the "Uid" component. Try running "composer require symfony/uid".', self::class));
}
$this->factory = $factory ?? new UuidFactory();
}

return (string) Uuid::v1();
public function generate(): string
{
return (string) $this->factory->create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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));
Expand Down
19 changes: 0 additions & 19 deletions tests/Generator/TimestampTokenGeneratorTest.php

This file was deleted.

0 comments on commit 3d439a0

Please sign in to comment.