<?php
namespace App\Logger;
use App\Utils\Constantes;
use App\Utils\JWTEncoder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class BgRequestProcessor
{
// private $session;
// private $sessionId;
private $jwtFactory;
private $request;
private $requestStack;
public function __construct(RequestStack $requestStack)
{
// $this->session = $session;
$this->requestStack = $requestStack;
$this->jwtFactory = JWTEncoder::getInstance();
$this->request = Request::createFromGlobals();
}
// this method is called for each log record; optimize it to not hurt performance
public function __invoke(array $record)
{
$apiKey = $this->request->headers->get(Constantes::AUTH_TOKEN);
$dataToken = $this->jwtFactory->decode($apiKey);
if ($this->requestStack->getMasterRequest())
$clientIp = @$this->requestStack->getMasterRequest()->getClientIp();
else
$clientIp = '';
// if (!$this->session->isStarted()) {
// return $record;
// }
// if (!$this->sessionId) {
// $this->sessionId = substr($this->session->getId(), 0, 8) ?: '????????';
// }
// $record['extra']['token'] = $this->sessionId.'-'.substr(uniqid('', true), -8);
// var_dump('OK');die;
// dump($dataToken);die;
$record['extra']['user']['id'] = @$dataToken['id'] ?: '';
$record['extra']['user']['username'] = @$dataToken['username'] ?: '';
$record['extra']['user']['roles'] = @$dataToken['roles'] ?: '';
$record['extra']['entity'] = @$dataToken['codeClient'] ?: '';
// $record['extra']['entity'] = 'bgpartners';
$record['extra']['service'] = 'Station Service';
$record['extra']['ip_address'] = @$clientIp?:'';
// var_dump($dataToken);
// var_dump($record);
// die;
return $record;
}
}