diff --git a/Factories/RollbarHandlerFactory.php b/Factories/RollbarHandlerFactory.php index 34135fa..c45e30a 100755 --- a/Factories/RollbarHandlerFactory.php +++ b/Factories/RollbarHandlerFactory.php @@ -5,16 +5,16 @@ use Psr\Log\LogLevel; use Monolog\Handler\RollbarHandler; use Rollbar\Rollbar; -use Rollbar\Symfony\RollbarBundle\DependencyInjection\RollbarExtension; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Rollbar\Symfony\RollbarBundle\Service\PersonProviderInterface; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Serializer\SerializerInterface; +use Throwable; +use function json_decode; class RollbarHandlerFactory { - public function __construct(ContainerInterface $container) + public function __construct(array $config, SerializerInterface $serializer, PersonProviderInterface $personProvider) { - $config = $container->getParameter(RollbarExtension::ALIAS . '.config'); - if (isset($_ENV['ROLLBAR_TEST_TOKEN']) && $_ENV['ROLLBAR_TEST_TOKEN']) { $config['access_token'] = $_ENV['ROLLBAR_TEST_TOKEN']; } @@ -22,18 +22,16 @@ public function __construct(ContainerInterface $container) if (!empty($config['person_fn']) && is_callable($config['person_fn'])) { $config['person'] = null; } elseif (empty($config['person'])) { - $config['person_fn'] = static function () use ($container) { + $config['person_fn'] = static function () use ($personProvider, $serializer) { try { - $token = $container->get('security.token_storage')->getToken(); - - if ($token) { - $user = $token->getUser(); - $serializer = $container->get('serializer'); - return \json_decode($serializer->serialize($user, 'json'), true, 512, JSON_THROW_ON_ERROR); - } - } catch (\Throwable $exception) { - // Ignore + $person = call_user_func([$personProvider, 'getUserInfo']); + + return json_decode($serializer->serialize($person, 'json'), true, 512, JSON_THROW_ON_ERROR); + } catch (Throwable) { + // do nothing } + + return []; }; } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 37f1c65..85ff9b9 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,8 +1,15 @@ services: + rollbar.person-provider: + class: Rollbar\Symfony\RollbarBundle\Service\PersonProvider + arguments: + - '@security.token_storage' + - '@serializer.normalizer.object' Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory: arguments: - - '@service_container' + - '%rollbar.config%' + - '@serializer' + - '@rollbar.person-provider' Rollbar\Monolog\Handler\RollbarHandler: factory: ['@Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory', createRollbarHandler] diff --git a/Service/PersonProvider.php b/Service/PersonProvider.php new file mode 100644 index 0000000..9bf65e6 --- /dev/null +++ b/Service/PersonProvider.php @@ -0,0 +1,28 @@ +tokenStorage->getToken(); + + if ($token) { + $user = $token->getUser(); + + return $this->normalizer->normalize($user, 'json'); + } + + return null; + } +} diff --git a/Service/PersonProviderInterface.php b/Service/PersonProviderInterface.php new file mode 100644 index 0000000..9207578 --- /dev/null +++ b/Service/PersonProviderInterface.php @@ -0,0 +1,8 @@ +tokenStorage = $this->getMockBuilder(TokenStorageInterface::class) + ->onlyMethods(['getToken', 'setToken']) + ->getMock(); + + $this->normalizer = $this->getMockBuilder(NormalizerInterface::class) + ->disableOriginalConstructor() + ->onlyMethods(['normalize', 'supportsNormalization']) + ->getMock(); + + $this->personProvider = new PersonProvider($this->tokenStorage, $this->normalizer); + } + + public function testNullToken(): void + { + $this->tokenStorage->method('getToken') + ->willReturn(null); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals(null, $info); + } + + public function testNullUser(): void + { + $token = $this->getMockBuilder(UsernamePasswordToken::class) + ->disableOriginalConstructor() + ->onlyMethods(['getUser']) + ->getMock(); + + $token->method('getUser') + ->willReturn(null); + + $this->tokenStorage->method('getToken') + ->willReturn($token); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals(null, $info); + } + + public static function normalizedUserData(): Generator + { + yield 'normalized data: null' => [null, null]; + yield 'normalized data: int' => [12, 12]; + yield 'normalized data: float' => [12.153, 12.153]; + yield 'normalized data: string' => ['john.doe@example.com', 'john.doe@example.com']; + yield 'normalized data: array' => [ + ['email' => 'john.doe@example.com', 'name' => 'John'], + ['email' => 'john.doe@example.com', 'name' => 'John'], + ]; + yield 'normalized data: ArrayObject' => [ + new ArrayObject(['email' => 'john.doe@example.com', 'name' => 'John']), + new ArrayObject(['email' => 'john.doe@example.com', 'name' => 'John']), + ]; + yield 'normalized data: bool' => [true, true]; + } + + /** + * @dataProvider normalizedUserData + */ + public function testUserInTokenStorage($normalizedData, $expected): void + { + $token = $this->getMockBuilder(UsernamePasswordToken::class) + ->disableOriginalConstructor() + ->onlyMethods(['getUser']) + ->getMock(); + + $token->method('getUser') + ->willReturn(new InMemoryUser('john-doe', 'pass')); + + $this->tokenStorage->method('getToken') + ->willReturn($token); + + $this->normalizer->method('normalize') + ->willReturn($normalizedData); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals($expected, $info); + } +} diff --git a/composer.json b/composer.json index bf76b95..4cd8eb3 100755 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "symfony/http-kernel": "^5.4|^6.2|^7.0", "symfony/monolog-bundle": "^3.8", "symfony/serializer": "^5.4|^6.2|^7.0", + "symfony/security-bundle": "^5.4|^6.4", "ext-json": "*" }, "require-dev": {