Skip to content

Commit

Permalink
Possibility to use service as person_fn rather than a static function
Browse files Browse the repository at this point in the history
  • Loading branch information
zinkovskiy committed Dec 6, 2023
1 parent d9cb06f commit 63e0453
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
27 changes: 12 additions & 15 deletions Factories/RollbarHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,32 @@
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;

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'];
}

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 [];
};
}

Expand Down
9 changes: 8 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
27 changes: 27 additions & 0 deletions Service/PersonProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rollbar\Symfony\RollbarBundle\Service;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class PersonProvider implements PersonProviderInterface
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private NormalizerInterface $normalizer,
) {}

public function getUserInfo(): array
{
$token = $this->tokenStorage->getToken();

if ($token) {
$user = $token->getUser();

return $this->normalizer->normalize($user, 'json');
}

return [];
}
}
8 changes: 8 additions & 0 deletions Service/PersonProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rollbar\Symfony\RollbarBundle\Service;

interface PersonProviderInterface
{
public function getUserInfo(): array;
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"symfony/http-kernel": "^5.4|^6.2",
"symfony/monolog-bundle": "^3.8",
"symfony/serializer": "^5.4|^6.2",
"ext-json": "*"
"ext-json": "*",
"symfony/security-core": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6|^10.1",
Expand Down

0 comments on commit 63e0453

Please sign in to comment.