diff --git a/doc/5-events.md b/doc/5-events.md index 0a0da09..c793e39 100644 --- a/doc/5-events.md +++ b/doc/5-events.md @@ -69,7 +69,7 @@ class LogSecurityTokenErrors implements EventSubscriberInterface { $this->logger->warning( 'Security token was not found', - ['purpose' => $event->getPurpose(), 'value' => $event->getValue()] + ['purpose' => $event->purpose, 'value' => $event->value] ); } @@ -77,7 +77,7 @@ class LogSecurityTokenErrors implements EventSubscriberInterface { $this->logger->warning( 'Security token was expired', - ['purpose' => $event->getPurpose(), 'value' => $event->getValue()] + ['purpose' => $event->purpose, 'value' => $event->value] ); } @@ -85,7 +85,7 @@ class LogSecurityTokenErrors implements EventSubscriberInterface { $this->logger->warning( 'Security token was already consumed', - ['purpose' => $event->getPurpose(), 'value' => $event->getValue()] + ['purpose' => $event->purpose, 'value' => $event->value] ); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e7a567f..921a62b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,23 +1,11 @@ parameters: ignoreErrors: - - - message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#' - identifier: property.unusedType - count: 1 - path: src/Entity/Token.php - - message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id is never written, only read\.$#' identifier: property.onlyRead count: 1 path: src/Entity/Token.php - - - message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#' - identifier: property.unusedType - count: 1 - path: src/Entity/TokenUsage.php - - message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id is never written, only read\.$#' identifier: property.onlyRead diff --git a/src/Archive/ArchivistInterface.php b/src/Archive/ArchivistInterface.php index 9a808e6..e462c82 100644 --- a/src/Archive/ArchivistInterface.php +++ b/src/Archive/ArchivistInterface.php @@ -16,7 +16,7 @@ interface ArchivistInterface * * @param string|null $purpose The token purpose * - * @return integer + * @return int Count archived tokens */ public function archive(string|null $purpose = null): int; } diff --git a/src/Archive/DeleteArchivist.php b/src/Archive/DeleteArchivist.php index 5948ab2..795c51d 100644 --- a/src/Archive/DeleteArchivist.php +++ b/src/Archive/DeleteArchivist.php @@ -15,17 +15,12 @@ */ final class DeleteArchivist implements ArchivistInterface { - /** - * @var EntityRepository - */ - private $tokenRepository; - - /** - * @param EntityRepository $tokenRepository The token entity repository - */ - public function __construct(EntityRepository $tokenRepository) - { - $this->tokenRepository = $tokenRepository; + public function __construct( + /** + * @var EntityRepository The token entity repository + */ + private readonly EntityRepository $tokenRepository, + ) { } public function archive(string|null $purpose = null): int diff --git a/src/Command/ArchiveTokenCommand.php b/src/Command/ArchiveTokenCommand.php index 3530815..2ef3d8d 100644 --- a/src/Command/ArchiveTokenCommand.php +++ b/src/Command/ArchiveTokenCommand.php @@ -15,14 +15,9 @@ */ final class ArchiveTokenCommand extends Command { - /** - * @var ArchivistInterface - */ - private $archivist; - - public function __construct(ArchivistInterface $archivist) - { - $this->archivist = $archivist; + public function __construct( + private readonly ArchivistInterface $archivist, + ) { parent::__construct(); } @@ -45,6 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int \sprintf('Successfully archived %d security token(s).', $count), ); - return 0; + return self::SUCCESS; } } diff --git a/src/Configuration/TokenConfiguration.php b/src/Configuration/TokenConfiguration.php index a674878..bcbe16c 100644 --- a/src/Configuration/TokenConfiguration.php +++ b/src/Configuration/TokenConfiguration.php @@ -11,79 +11,13 @@ */ final class TokenConfiguration { - /** - * @var string - */ - private $purpose; - - /** - * @var TokenGeneratorInterface - */ - private $generator; - - /** - * @var string - */ - private $duration; - - /** - * @var int - */ - private $usages; - - /** - * @var string - */ - private $keep; - - /** - * @var bool - */ - private $unique; - public function __construct( - string $purpose, - TokenGeneratorInterface $generator, - string $duration, - int $usages, - string $keep, - bool $unique, + public readonly string $purpose, + public readonly TokenGeneratorInterface $generator, + public readonly string $duration, + public readonly int $usages, + public readonly string $keep, + public readonly bool $unique, ) { - $this->purpose = $purpose; - $this->generator = $generator; - $this->duration = $duration; - $this->usages = $usages; - $this->keep = $keep; - $this->unique = $unique; - } - - public function getPurpose(): string - { - return $this->purpose; - } - - public function getGenerator(): TokenGeneratorInterface - { - return $this->generator; - } - - public function getDuration(): string - { - return $this->duration; - } - - public function getUsages(): int - { - return $this->usages; - } - - public function getKeep(): string - { - return $this->keep; - } - - public function isUnique(): bool - { - return $this->unique; } } diff --git a/src/Configuration/TokenConfigurationRegistry.php b/src/Configuration/TokenConfigurationRegistry.php index 5568eaa..f046c41 100644 --- a/src/Configuration/TokenConfigurationRegistry.php +++ b/src/Configuration/TokenConfigurationRegistry.php @@ -12,19 +12,20 @@ final class TokenConfigurationRegistry { /** - * @var array + * @var array */ - private $configurations; + private readonly array $configurations; /** - * @param array $configurations + * @param list $configurations */ public function __construct(array $configurations) { - $this->configurations = []; + $indexedConfigurations = []; foreach ($configurations as $configuration) { - $this->configurations[$configuration->getPurpose()] = $configuration; + $indexedConfigurations[$configuration->purpose] = $configuration; } + $this->configurations = $indexedConfigurations; } /** @@ -36,12 +37,9 @@ public function __construct(array $configurations) */ public function get(string $purpose): TokenConfiguration { - if (!isset($this->configurations[$purpose])) { - throw new BadMethodCallException( + return $this->configurations[$purpose] + ?? throw new BadMethodCallException( \sprintf('There is no configured security token on "%s" purpose.', $purpose), ); - } - - return $this->configurations[$purpose]; } } diff --git a/src/Entity/Token.php b/src/Entity/Token.php index 71cf2fa..b0215f7 100644 --- a/src/Entity/Token.php +++ b/src/Entity/Token.php @@ -14,65 +14,41 @@ */ class Token { - /** - * @var int|null - */ - private $id; + private int|null $id; /** * @var class-string */ - private $userClass; + private string $userClass; - /** - * @var string - */ - private $userId; + private string $userId; - /** - * @var string - */ - private $value; + private string $value; - /** - * @var string - */ - private $purpose; + private string $purpose; /** * @var array */ - private $payload = []; + private array $payload; - /** - * @var DateTime - */ - private $createdAt; + private DateTime $createdAt; /** * @var array */ - private $createdInformation = []; + private array $createdInformation; - /** - * @var integer - */ - private $allowedUsages; + private int $allowedUsages; - /** - * @var DateTime - */ - private $expiresAt; + private DateTime $expiresAt; - /** - * @var DateTime - */ - private $keepUntil; + private DateTime $keepUntil; /** * @var Collection */ - private $usages; + private Collection $usages; /** * @param class-string $userClass @@ -188,7 +164,7 @@ public function getCountUsages(): int } /** - * @return array + * @return array */ public function getUsages(): array { diff --git a/src/Entity/TokenUsage.php b/src/Entity/TokenUsage.php index 28822c9..6798645 100644 --- a/src/Entity/TokenUsage.php +++ b/src/Entity/TokenUsage.php @@ -8,25 +8,16 @@ class TokenUsage { - /** - * @var int|null - */ - private $id; + private int|null $id; - /** - * @var Token - */ - private $token; + private Token $token; - /** - * @var DateTime - */ - private $createdAt; + private DateTime $createdAt; /** * @var array */ - private $information = []; + private array $information = []; /** * @param array $information diff --git a/src/Event/ConsumeTokenEvent.php b/src/Event/ConsumeTokenEvent.php index eb86142..1232850 100644 --- a/src/Event/ConsumeTokenEvent.php +++ b/src/Event/ConsumeTokenEvent.php @@ -15,56 +15,20 @@ */ final class ConsumeTokenEvent extends Event { - /** - * @var Token - */ - private $token; - - /** - * @var DateTime|null - */ - private $at; - - /** - * @var array - */ - private $information; - - /** - * @param Token $token The consumed token - * @param DateTime|null $at Date/time at which the token has been consumed - * @param array $information Some context information - */ - public function __construct(Token $token, DateTime|null $at, array $information) - { - $this->token = $token; - $this->at = $at; - $this->information = $information; - } - - /** - * The consumed token - */ - public function getToken(): Token - { - return $this->token; - } - - /** - * Date/time at which the token has been consumed - */ - public function getAt(): DateTime|null - { - return $this->at; - } - - /** - * Some context information - * - * @return array - */ - public function getInformation(): array - { - return $this->information; + public function __construct( + /** + * The consumed token + */ + public readonly Token $token, + /** + * Date/time at which the token has been consumed + */ + public readonly DateTime|null $at, + /** + * Some context information + * @var array + */ + public readonly array $information, + ) { } } diff --git a/src/Event/CreateTokenEvent.php b/src/Event/CreateTokenEvent.php index 7934a30..92bff4d 100644 --- a/src/Event/CreateTokenEvent.php +++ b/src/Event/CreateTokenEvent.php @@ -13,51 +13,21 @@ */ final class CreateTokenEvent extends Event { - /** - * @var string - */ - private $purpose; - - /** - * @var mixed - */ - private $user; - - /** - * @var array - */ - private $payload; - - /** - * @param string $purpose The token purpose - * @param mixed $user The associated user - * @param array $payload The token payload - */ - public function __construct(string $purpose, $user, array $payload) - { - $this->purpose = $purpose; - $this->user = $user; - $this->payload = $payload; - } - - /** - * The token purpose - * - * @return string - */ - public function getPurpose() - { - return $this->purpose; - } - - /** - * The associated user - * - * @return mixed - */ - public function getUser() - { - return $this->user; + public function __construct( + /** + * The token purpose + */ + public readonly string $purpose, + /** + * The associated user + */ + public readonly mixed $user, + /** + * The token payload + * @var array + */ + private array $payload, + ) { } /** diff --git a/src/Event/TokenAlreadyConsumedEvent.php b/src/Event/TokenAlreadyConsumedEvent.php index b9fc6f4..7ef8d85 100644 --- a/src/Event/TokenAlreadyConsumedEvent.php +++ b/src/Event/TokenAlreadyConsumedEvent.php @@ -13,39 +13,15 @@ */ final class TokenAlreadyConsumedEvent extends Event { - /** - * @var string - */ - private $purpose; - - /** - * @var string - */ - private $value; - - /** - * @param string $purpose The token purpose - * @param string $value The token value - */ - public function __construct(string $purpose, string $value) - { - $this->purpose = $purpose; - $this->value = $value; - } - - /** - * The token purpose - */ - public function getPurpose(): string - { - return $this->purpose; - } - - /** - * The token value - */ - public function getValue(): string - { - return $this->value; + public function __construct( + /** + * The token purpose + */ + public readonly string $purpose, + /** + * The token value + */ + public readonly string $value, + ) { } } diff --git a/src/Event/TokenConsumedEvent.php b/src/Event/TokenConsumedEvent.php index b8b3193..673328f 100644 --- a/src/Event/TokenConsumedEvent.php +++ b/src/Event/TokenConsumedEvent.php @@ -14,24 +14,11 @@ */ final class TokenConsumedEvent extends Event { - /** - * @var Token - */ - private $token; - - /** - * @param Token $token The consumed token - */ - public function __construct(Token $token) - { - $this->token = $token; - } - - /** - * The consumed token - */ - public function getToken(): Token - { - return $this->token; + public function __construct( + /** + * The consumed token + */ + public readonly Token $token, + ) { } } diff --git a/src/Event/TokenCreatedEvent.php b/src/Event/TokenCreatedEvent.php index cbb52ba..fc01826 100644 --- a/src/Event/TokenCreatedEvent.php +++ b/src/Event/TokenCreatedEvent.php @@ -14,24 +14,11 @@ */ final class TokenCreatedEvent extends Event { - /** - * @var Token - */ - private $token; - - /** - * @param Token $token The created token - */ - public function __construct(Token $token) - { - $this->token = $token; - } - - /** - * The created token - */ - public function getToken(): Token - { - return $this->token; + public function __construct( + /** + * The consumed token + */ + public readonly Token $token, + ) { } } diff --git a/src/Event/TokenExpiredEvent.php b/src/Event/TokenExpiredEvent.php index 85f92cc..808177c 100644 --- a/src/Event/TokenExpiredEvent.php +++ b/src/Event/TokenExpiredEvent.php @@ -13,39 +13,15 @@ */ final class TokenExpiredEvent extends Event { - /** - * @var string - */ - private $purpose; - - /** - * @var string - */ - private $value; - - /** - * @param string $purpose The token purpose - * @param string $value The token value - */ - public function __construct(string $purpose, string $value) - { - $this->purpose = $purpose; - $this->value = $value; - } - - /** - * The token purpose - */ - public function getPurpose(): string - { - return $this->purpose; - } - - /** - * The token value - */ - public function getValue(): string - { - return $this->value; + public function __construct( + /** + * The token purpose + */ + public readonly string $purpose, + /** + * The token value + */ + public readonly string $value, + ) { } } diff --git a/src/Event/TokenNotFoundEvent.php b/src/Event/TokenNotFoundEvent.php index eb95f16..4c5a70f 100644 --- a/src/Event/TokenNotFoundEvent.php +++ b/src/Event/TokenNotFoundEvent.php @@ -13,39 +13,15 @@ */ final class TokenNotFoundEvent extends Event { - /** - * @var string - */ - private $purpose; - - /** - * @var string - */ - private $value; - - /** - * @param string $purpose The token purpose - * @param string $value The token value - */ - public function __construct(string $purpose, string $value) - { - $this->purpose = $purpose; - $this->value = $value; - } - - /** - * The token purpose - */ - public function getPurpose(): string - { - return $this->purpose; - } - - /** - * The token value - */ - public function getValue(): string - { - return $this->value; + public function __construct( + /** + * The token purpose + */ + public readonly string $purpose, + /** + * The token value + */ + public readonly string $value, + ) { } } diff --git a/src/Event/TokenRetrievedEvent.php b/src/Event/TokenRetrievedEvent.php index fcca274..fc285f1 100644 --- a/src/Event/TokenRetrievedEvent.php +++ b/src/Event/TokenRetrievedEvent.php @@ -14,24 +14,11 @@ */ final class TokenRetrievedEvent extends Event { - /** - * @var Token - */ - private $token; - - /** - * @param Token $token The retrieved token - */ - public function __construct(Token $token) - { - $this->token = $token; - } - - /** - * The retrieved token - */ - public function getToken(): Token - { - return $this->token; + public function __construct( + /** + * The consumed token + */ + public readonly Token $token, + ) { } } diff --git a/src/Event/TokenTotallyConsumedEvent.php b/src/Event/TokenTotallyConsumedEvent.php index 209a858..29f102b 100644 --- a/src/Event/TokenTotallyConsumedEvent.php +++ b/src/Event/TokenTotallyConsumedEvent.php @@ -14,24 +14,11 @@ */ final class TokenTotallyConsumedEvent extends Event { - /** - * @var Token - */ - private $token; - - /** - * @param Token $token The totally consumed token - */ - public function __construct(Token $token) - { - $this->token = $token; - } - - /** - * The totally consumed token - */ - public function getToken(): Token - { - return $this->token; + public function __construct( + /** + * The consumed token + */ + public readonly Token $token, + ) { } } diff --git a/src/EventDispatcher.php b/src/EventDispatcher.php index 40d33d8..5718db3 100644 --- a/src/EventDispatcher.php +++ b/src/EventDispatcher.php @@ -22,21 +22,15 @@ */ final class EventDispatcher { - /** - * @var EventDispatcherInterface - */ - private $eventDispatcher; - - public function __construct(EventDispatcherInterface $eventDispatcher) - { - $this->eventDispatcher = $eventDispatcher; + public function __construct( + private readonly EventDispatcherInterface $eventDispatcher, + ) { } /** - * @param mixed $user * @param array $payload */ - public function createToken(string $purpose, $user, array $payload): CreateTokenEvent + public function createToken(string $purpose, mixed $user, array $payload): CreateTokenEvent { $this->eventDispatcher->dispatch( $event = new CreateTokenEvent($purpose, $user, $payload), diff --git a/src/Factory/TokenFactory.php b/src/Factory/TokenFactory.php index 9034751..fab4823 100644 --- a/src/Factory/TokenFactory.php +++ b/src/Factory/TokenFactory.php @@ -17,45 +17,15 @@ */ final class TokenFactory implements TokenFactoryInterface { - /** - * @var TokenConfigurationRegistry - */ - private $registry; - - /** - * @var InformationGuesserInterface - */ - private $informationGuesser; - - /** - * @var UserManagerInterface - */ - private $userManager; - - /** - * @var TokenRepositoryInterface - */ - private $repository; - - /** - * @param TokenConfigurationRegistry $registry The configuration registry - * @param InformationGuesserInterface $informationGuesser The information guesser - * @param UserManagerInterface $userManager The user manager - * @param TokenRepositoryInterface $repository The token repository - */ public function __construct( - TokenConfigurationRegistry $registry, - InformationGuesserInterface $informationGuesser, - UserManagerInterface $userManager, - TokenRepositoryInterface $repository, + private readonly TokenConfigurationRegistry $registry, + private readonly InformationGuesserInterface $informationGuesser, + private readonly UserManagerInterface $userManager, + private readonly TokenRepositoryInterface $repository, ) { - $this->registry = $registry; - $this->informationGuesser = $informationGuesser; - $this->userManager = $userManager; - $this->repository = $repository; } - public function create($user, string $purpose, array $payload = []): Token + public function create(mixed $user, string $purpose, array $payload = []): Token { // get configuration for this token purpose $configuration = $this->registry->get($purpose); @@ -65,7 +35,7 @@ public function create($user, string $purpose, array $payload = []): Token $userId = $this->userManager->getId($user); // if configuration for this token tells that it can only exists one Token for this user - if ($configuration->isUnique()) { + if ($configuration->unique) { $token = $this->repository->findExisting($userClass, $userId, $purpose); // a token already exists for this user and this purpose, return it @@ -77,17 +47,19 @@ public function create($user, string $purpose, array $payload = []): Token // enforce token uniqueness // generate a value while it exists already do { - $value = $configuration->getGenerator()->generate(); + $value = $configuration->generator->generate(); } while ($this->repository->exists($value, $purpose)); - // extract configuration values - $duration = $configuration->getDuration(); - $keep = $configuration->getKeep(); - $usages = $configuration->getUsages(); - - // extract context information - $information = $this->informationGuesser->get(); - - return new Token($userClass, $userId, $value, $purpose, $duration, $keep, $usages, $payload, $information); + return new Token( + userClass: $userClass, + userId: $userId, + value: $value, + purpose: $purpose, + validDuration: $configuration->duration, + keepDuration: $configuration->keep, + allowedUsages: $configuration->usages, + payload: $payload, + information: $this->informationGuesser->get(), + ); } } diff --git a/src/Factory/TokenFactoryInterface.php b/src/Factory/TokenFactoryInterface.php index 30ee843..b10967e 100644 --- a/src/Factory/TokenFactoryInterface.php +++ b/src/Factory/TokenFactoryInterface.php @@ -22,5 +22,5 @@ interface TokenFactoryInterface * * @return Token The created token */ - public function create($user, string $purpose, array $payload = []): Token; + public function create(mixed $user, string $purpose, array $payload = []): Token; } diff --git a/src/Generator/OpenSslTokenGenerator.php b/src/Generator/OpenSslTokenGenerator.php index 84f4f77..05d5cdc 100644 --- a/src/Generator/OpenSslTokenGenerator.php +++ b/src/Generator/OpenSslTokenGenerator.php @@ -13,24 +13,16 @@ */ final class OpenSslTokenGenerator implements TokenGeneratorInterface { - private const DEFAULT_LENGTH = 32; - - /** - * @var int - */ - private $length; - - public function __construct(int $length = self::DEFAULT_LENGTH) - { + public function __construct( + private readonly int $length = 32, + ) { if (!\function_exists('openssl_random_pseudo_bytes')) { throw new LogicException('The extension "openssl" is required to use "open ssl" token generator.'); } - - $this->length = $length; } public function generate(): string { - return \rtrim(\strtr(\base64_encode((string)\openssl_random_pseudo_bytes($this->length)), '+/', '-_'), '='); + return \rtrim(\strtr(\base64_encode(\openssl_random_pseudo_bytes($this->length)), '+/', '-_'), '='); } } diff --git a/src/InformationGuesser/InformationGuesser.php b/src/InformationGuesser/InformationGuesser.php index 648fb9d..935cd64 100644 --- a/src/InformationGuesser/InformationGuesser.php +++ b/src/InformationGuesser/InformationGuesser.php @@ -13,23 +13,15 @@ */ final class InformationGuesser implements InformationGuesserInterface { - /** - * @var RequestStack - */ - private $requestStack; - - /** - * @param RequestStack $requestStack The request stack - */ - public function __construct(RequestStack $requestStack) - { - $this->requestStack = $requestStack; + public function __construct( + private RequestStack $requestStack, + ) { } public function get(): array { $request = $this->requestStack->getMainRequest(); - if (!$request) { + if ($request === null) { return []; } diff --git a/src/Manager/ChainUserManager.php b/src/Manager/ChainUserManager.php index ad0221d..70ceb77 100644 --- a/src/Manager/ChainUserManager.php +++ b/src/Manager/ChainUserManager.php @@ -11,17 +11,12 @@ */ final class ChainUserManager implements UserManagerInterface { - /** - * @var iterable - */ - private $managers; - - /** - * @param iterable $managers A list of user managers - */ - public function __construct(iterable $managers) - { - $this->managers = $managers; + public function __construct( + /** + * @var iterable $managers A list of user managers + */ + private readonly iterable $managers, + ) { } public function supportsClass(string $class): bool @@ -35,7 +30,7 @@ public function supportsClass(string $class): bool return true; } - public function supportsUser($user): bool + public function supportsUser(mixed $user): bool { try { $this->getManagerForUser($user); @@ -51,12 +46,12 @@ public function get(string $class, string $id) return $this->getManagerForClass($class)->get($class, $id); } - public function getClass($user): string + public function getClass(mixed $user): string { return $this->getManagerForUser($user)->getClass($user); } - public function getId($user): string + public function getId(mixed $user): string { return $this->getManagerForUser($user)->getId($user); } @@ -96,7 +91,7 @@ private function getManagerForClass(string $class): UserManagerInterface * * @throws \InvalidArgumentException */ - private function getManagerForUser($user): UserManagerInterface + private function getManagerForUser(mixed $user): UserManagerInterface { $tries = []; diff --git a/src/Manager/DoctrineUserManager.php b/src/Manager/DoctrineUserManager.php index 531009e..890594c 100644 --- a/src/Manager/DoctrineUserManager.php +++ b/src/Manager/DoctrineUserManager.php @@ -15,17 +15,12 @@ */ final class DoctrineUserManager implements UserManagerInterface { - /** - * @var ManagerRegistry - */ - private $doctrine; - - /** - * @param ManagerRegistry $doctrine The doctrine registry - */ - public function __construct(ManagerRegistry $doctrine) - { - $this->doctrine = $doctrine; + public function __construct( + /** + * @var ManagerRegistry $doctrine The doctrine registry + */ + private readonly ManagerRegistry $doctrine, + ) { } public function supportsClass(string $class): bool @@ -39,7 +34,7 @@ public function supportsClass(string $class): bool return true; } - public function supportsUser($user): bool + public function supportsUser(mixed $user): bool { return $this->supportsClass( $this->getClass($user), @@ -51,14 +46,14 @@ public function get(string $class, string $id) return $this->getManagerFor($class)->find($class, $id); } - public function getClass($user): string + public function getClass(mixed $user): string { /** @var object $user */ return ClassUtils::getClass($user); } - public function getId($user): string + public function getId(mixed $user): string { /** @var object $user */ /** @var class-string $class */ diff --git a/src/Manager/TokenManager.php b/src/Manager/TokenManager.php index 2f4bec8..ce5fca8 100644 --- a/src/Manager/TokenManager.php +++ b/src/Manager/TokenManager.php @@ -19,50 +19,13 @@ */ final class TokenManager implements TokenManagerInterface { - /** - * @var TokenFactoryInterface - */ - private $factory; - - /** - * @var TokenRepositoryInterface - */ - private $repository; - - /** - * @var InformationGuesserInterface - */ - private $informationGuesser; - - /** - * @var UserManagerInterface - */ - private $userManager; - - /** - * @var EventDispatcher - */ - private $eventDispatcher; - - /** - * @param TokenFactoryInterface $factory The token factory - * @param TokenRepositoryInterface $repository The token repository - * @param InformationGuesserInterface $informationGuesser The information guesser - * @param UserManagerInterface $userManager The user manager - * @param EventDispatcher $eventDispatcher The event dispatcher - */ public function __construct( - TokenFactoryInterface $factory, - TokenRepositoryInterface $repository, - InformationGuesserInterface $informationGuesser, - UserManagerInterface $userManager, - EventDispatcher $eventDispatcher, + private readonly TokenFactoryInterface $factory, + private readonly TokenRepositoryInterface $repository, + private readonly InformationGuesserInterface $informationGuesser, + private readonly UserManagerInterface $userManager, + private readonly EventDispatcher $eventDispatcher, ) { - $this->factory = $factory; - $this->repository = $repository; - $this->informationGuesser = $informationGuesser; - $this->userManager = $userManager; - $this->eventDispatcher = $eventDispatcher; } public function get(string $purpose, string $value): Token @@ -88,7 +51,7 @@ public function get(string $purpose, string $value): Token return $token; } - public function create(string $purpose, $user, array $payload = []): Token + public function create(string $purpose, mixed $user, array $payload = []): Token { $event = $this->eventDispatcher->createToken($purpose, $user, $payload); @@ -105,7 +68,7 @@ public function consume(Token $token, DateTime|null $at = null): void { $event = $this->eventDispatcher->consumeToken($token, $at, $this->informationGuesser->get()); - $token->consume($event->getInformation(), $at); + $token->consume($event->information, $at); $this->repository->update($token); @@ -115,7 +78,7 @@ public function consume(Token $token, DateTime|null $at = null): void } } - public function getUser(Token $token) + public function getUser(Token $token): mixed { return $this->userManager->get($token->getUserClass(), $token->getUserId()); } diff --git a/src/Manager/TokenManagerInterface.php b/src/Manager/TokenManagerInterface.php index e7003b9..65e8e0e 100644 --- a/src/Manager/TokenManagerInterface.php +++ b/src/Manager/TokenManagerInterface.php @@ -40,7 +40,7 @@ public function get(string $purpose, string $value): Token; * * @return Token The created token */ - public function create(string $purpose, $user, array $payload = []): Token; + public function create(string $purpose, mixed $user, array $payload = []): Token; /** * Consume a token. @@ -57,5 +57,5 @@ public function consume(Token $token, DateTime|null $at = null): void; * * @return mixed The user associated to the provided token */ - public function getUser(Token $token); + public function getUser(Token $token): mixed; } diff --git a/src/Manager/UserManagerInterface.php b/src/Manager/UserManagerInterface.php index c83bc73..2df345d 100644 --- a/src/Manager/UserManagerInterface.php +++ b/src/Manager/UserManagerInterface.php @@ -27,7 +27,7 @@ public function supportsClass(string $class): bool; * * @return bool Whether the provided user is supported */ - public function supportsUser($user): bool; + public function supportsUser(mixed $user): bool; /** * Get user of certain class with certain id. @@ -46,7 +46,7 @@ public function get(string $class, string $id); * * @return class-string The class of the provided user */ - public function getClass($user): string; + public function getClass(mixed $user): string; /** * Get the id of a user. @@ -55,5 +55,5 @@ public function getClass($user): string; * * @return string The id of the provided user */ - public function getId($user): string; + public function getId(mixed $user): string; } diff --git a/src/Repository/DoctrineORMTokenRepository.php b/src/Repository/DoctrineORMTokenRepository.php index d68d508..dcee22b 100644 --- a/src/Repository/DoctrineORMTokenRepository.php +++ b/src/Repository/DoctrineORMTokenRepository.php @@ -4,7 +4,7 @@ namespace Yokai\SecurityTokenBundle\Repository; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Yokai\SecurityTokenBundle\Entity\Token; use Yokai\SecurityTokenBundle\Exception\TokenConsumedException; @@ -18,24 +18,17 @@ */ final class DoctrineORMTokenRepository implements TokenRepositoryInterface { - /** - * @var EntityManager - */ - private $manager; - - /** - * @var EntityRepository - */ - private $repository; - - /** - * @param EntityManager $manager The token entity manager - * @param EntityRepository $repository The token entity repository - */ - public function __construct(EntityManager $manager, EntityRepository $repository) - { - $this->manager = $manager; - $this->repository = $repository; + public function __construct( + /** + * The token entity manager + */ + private readonly EntityManagerInterface $manager, + /** + * The token entity repository + * @var EntityRepository + */ + private readonly EntityRepository $repository, + ) { } public function get(string $value, string $purpose): Token diff --git a/tests/DependencyInjection/DependencyInjectionTest.php b/tests/DependencyInjection/DependencyInjectionTest.php index e2b53da..3841e17 100644 --- a/tests/DependencyInjection/DependencyInjectionTest.php +++ b/tests/DependencyInjection/DependencyInjectionTest.php @@ -110,10 +110,10 @@ public function process(ContainerBuilder $container) $token = $this->container->get('yokai_security_token.configuration_registry')->get($tokenId); self::assertInstanceOf(TokenConfiguration::class, $token); - self::assertInstanceOf($tokenConfig['generator'], $token->getGenerator()); - self::assertSame($tokenId, $token->getPurpose()); - self::assertSame($tokenConfig['duration'], $token->getDuration()); - self::assertSame($tokenConfig['usages'], $token->getUsages()); + self::assertInstanceOf($tokenConfig['generator'], $token->generator); + self::assertSame($tokenId, $token->purpose); + self::assertSame($tokenConfig['duration'], $token->duration); + self::assertSame($tokenConfig['usages'], $token->usages); } foreach ($aliases as $alias => $expectedId) { diff --git a/tests/Manager/TokenManagerTest.php b/tests/Manager/TokenManagerTest.php index 1d0be00..42aaf5f 100644 --- a/tests/Manager/TokenManagerTest.php +++ b/tests/Manager/TokenManagerTest.php @@ -101,8 +101,8 @@ public function test_it_dispatch_not_found_exceptions_on_get_token_from_reposito $notFoundEvent = self::callback(function ($event) { return $event instanceof TokenNotFoundEvent - && $event->getPurpose() === 'forgot_password' - && $event->getValue() === 'unique-token'; + && $event->purpose === 'forgot_password' + && $event->value === 'unique-token'; }); $this->eventDispatcher->expects(self::once()) ->method('dispatch') @@ -122,8 +122,8 @@ public function test_it_dispatch_expired_exceptions_on_get_token_from_repository $expiredEvent = self::callback(function ($event) { return $event instanceof TokenExpiredEvent - && $event->getPurpose() === 'forgot_password' - && $event->getValue() === 'unique-token'; + && $event->purpose === 'forgot_password' + && $event->value === 'unique-token'; }); $this->eventDispatcher->expects(self::once()) ->method('dispatch') @@ -143,8 +143,8 @@ public function test_it_dispatch_used_exceptions_on_get_token_from_repository(): $alreadyConsumedEvent = self::callback(function ($event) { return $event instanceof TokenAlreadyConsumedEvent - && $event->getPurpose() === 'forgot_password' - && $event->getValue() === 'unique-token'; + && $event->purpose === 'forgot_password' + && $event->value === 'unique-token'; }); $this->eventDispatcher->expects(self::once()) ->method('dispatch') @@ -162,7 +162,7 @@ public function test_it_get_token_from_repository(): void $retrievedEvent = self::callback(function ($event) use ($expected) { return $event instanceof TokenRetrievedEvent - && $event->getToken() === $expected; + && $event->token === $expected; }); $this->eventDispatcher->expects(self::once()) ->method('dispatch') @@ -198,11 +198,11 @@ public function test_it_create_unique_token(): void $events = self::callback(function ($event) use ($expectedToken) { $isCreateTokenEvent = $event instanceof CreateTokenEvent - && $event->getPurpose() === 'forgot_password' - && $event->getUser() === 'john-doe' + && $event->purpose === 'forgot_password' + && $event->user === 'john-doe' && $event->getPayload() === ['payload', 'information']; $isCreatedTokenEvent = $event instanceof TokenCreatedEvent - && $event->getToken() === $expectedToken; + && $event->token === $expectedToken; return $isCreateTokenEvent || $isCreatedTokenEvent; }); @@ -229,12 +229,12 @@ public function test_it_consume_token(): void $events = self::callback(function ($event) use ($token) { $isConsumeEvent = $event instanceof ConsumeTokenEvent - && $event->getToken() === $token - && $event->getInformation() === ['some', 'precious', 'information']; + && $event->token === $token + && $event->information === ['some', 'precious', 'information']; $isConsumedEvent = $event instanceof TokenConsumedEvent - && $event->getToken() === $token; + && $event->token === $token; $isTotallyConsumedEvent = $event instanceof TokenTotallyConsumedEvent - && $event->getToken() === $token; + && $event->token === $token; return $isConsumeEvent || $isConsumedEvent || $isTotallyConsumedEvent; });