Skip to content

Commit

Permalink
Deprecate instantiating TokenStorageUsernameCallable with Container
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Jan 16, 2022
1 parent ce853dc commit fea8e07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Resources/config/auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
->args([new ReferenceConfigurator('simplethings_entityaudit.manager')])

->set('simplethings_entityaudit.username_callable.token_storage', TokenStorageUsernameCallable::class)
->args([new ReferenceConfigurator('service_container')])
->args([new ReferenceConfigurator('security.token_storage')])

->set('simplethings_entityaudit.config', AuditConfiguration::class)
->public()
Expand Down
45 changes: 35 additions & 10 deletions src/User/TokenStorageUsernameCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,45 @@
namespace SimpleThings\EntityAudit\User;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class TokenStorageUsernameCallable
{
/**
* NEXT_MAJOR: Inject the required services instead of using the container.
*
* @var Container
* @var TokenStorageInterface
*/
private $container;
private $tokenStorage;

public function __construct(Container $container)
/**
* NEXT_MAJOR: remove Container type.
*
* @param Container|TokenStorageInterface $tokenStorageOrContainer
*/
public function __construct(object $tokenStorageOrContainer)
{
$this->container = $container;
if ($tokenStorageOrContainer instanceof TokenStorageInterface) {
$this->tokenStorage = $tokenStorageOrContainer;
} elseif ($tokenStorageOrContainer instanceof Container) {
@trigger_error(sprintf(
'Passing as argument 1 an instance of "%s" to "%s" is deprecated since'
.' sonata-project/entity-audit-bundle 1.x and will throw an "%s" in version 2.0.'
.' You must pass an instance of "%s" instead.',
Container::class,
__METHOD__,
\TypeError::class,
TokenStorageInterface::class
), \E_USER_DEPRECATED);

$this->tokenStorage = $tokenStorageOrContainer->get('security.token_storage');
} else {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or %s, instance of "%s" given.',
__METHOD__,
TokenStorageInterface::class,
Container::class,
\get_class($tokenStorageOrContainer)
));
}
}

/**
Expand All @@ -37,10 +62,10 @@ public function __construct(Container $container)
*/
public function __invoke()
{
/** @var TokenInterface $token */
$token = $this->container->get('security.token_storage')->getToken();
$token = $this->tokenStorage->getToken();

if (null !== $token && null !== $token->getUser()) {
// @phpstan-ignore-next-line
// @phpstan-ignore-next-line Use only "getUserIdentifier" when dropping support of Symfony < 5.3
return method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testItRegistersDefaultServices(): void
$this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.create_schema_listener', 'doctrine.event_subscriber', ['connection' => 'default']);

$this->assertContainerBuilderHasService('simplethings_entityaudit.username_callable.token_storage', 'SimpleThings\EntityAudit\User\TokenStorageUsernameCallable');
$this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.username_callable.token_storage', 0, 'service_container');
$this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.username_callable.token_storage', 0, 'security.token_storage');

$this->assertContainerBuilderHasService('simplethings_entityaudit.config', 'SimpleThings\EntityAudit\AuditConfiguration');
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('simplethings_entityaudit.config', 'setAuditedEntityClasses', ['%simplethings.entityaudit.audited_entities%']);
Expand Down

0 comments on commit fea8e07

Please sign in to comment.