src/Log/LogEnricherHandler.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Log;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  5. class LogEnricherHandler
  6. {
  7.     private ?string $sessionId null;
  8.     private string|bool|null $requestId null;
  9.     private $userId;
  10.     private ?array $_server null;
  11.     private ?array $_user null;
  12.     public function __construct(private readonly TokenStorageInterface $tokenStorage, private readonly RequestStack $requestStack)
  13.     {
  14.     }
  15.     public function __invoke(array $record): array
  16.     {
  17.         if (php_sapi_name() == 'cli') {
  18.             return $record;
  19.         }
  20.         if (null === $this->sessionId && 
  21.             ($request $this->requestStack->getCurrentRequest()) && 
  22.             $request->hasSession() && 
  23.             $request->getSession()->isStarted() 
  24.         ) {
  25.             $this->sessionId substr($request->getSession()->getId(), 08) ?: '????????';
  26.         }
  27.         if ($this->requestId === null) {
  28.             $this->requestId substr(uniqid(), -8);
  29.             $this->_server = [
  30.                 'http.url' => (@$_SERVER['HTTP_HOST']) . (@$_SERVER['REQUEST_URI']),
  31.                 'http.method' => @$_SERVER['REQUEST_METHOD'],
  32.                 'http.useragent' => @$_SERVER['HTTP_USER_AGENT'],
  33.                 'http.referer' => @$_SERVER['HTTP_REFERER'],
  34.                 'http.x_forwarded_for' => @$_SERVER['HTTP_X_FORWARDED_FOR'],
  35.                 'http.x_country_code' => @$_SERVER['HTTP_X_COUNTRY_CODE'],
  36.             ];
  37.         }
  38.         if ($this->userId === null && ($user $this->getUser())) {
  39.             $this->userId $user->getId();
  40.             $this->_user = [
  41.                 'user.id' => $user->getId(),
  42.                 'user.username' => $user->getUsername(),
  43.             ];
  44.         }
  45.         $record['http.request_id'] = $this->requestId;
  46.         $record['http.url'] = $this->_server['http.url'];
  47.         $record['http.method'] = $this->_server['http.method'];
  48.         $record['http.useragent'] = $this->_server['http.useragent'];
  49.         $record['http.referer'] = $this->_server['http.referer'];
  50.         $record['http.x_forwarded_for'] = $this->_server['http.x_forwarded_for'];
  51.         $record['token'] = $this->sessionId '-' substr(uniqid(''true), -8);
  52.         $record['user'] = $this->_user;
  53.         $record['country_code'] =  $this->_server['http.x_country_code'];
  54.         $record['source'] = 'symfony';
  55.         $record['ddsource'] = 'symfony';
  56.         return $record;
  57.     }
  58.     protected function getUser(): ?\Symfony\Component\Security\Core\User\UserInterface
  59.     {
  60.         $token $this->tokenStorage->getToken();
  61.         $user  $token === null null $token->getUser();
  62.         if ($user === 'anon.') {
  63.             return null;
  64.         }
  65.         return $user;
  66.     }
  67. }