src/Listener/WebLoggerListener.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\User;
  4. use App\Helpers\ApiLogger;
  5. use App\Services\ActionLogService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. /**
  12.  * Class ApiLoggerListener
  13.  * @package App\Listener
  14.  */
  15. class WebLoggerListener
  16. {
  17.     /**
  18.      * @var TokenStorageInterface
  19.      */
  20.     private TokenStorageInterface $tokenStorage;
  21.     /**
  22.      * @var ActionLogService
  23.      */
  24.     private ActionLogService $actionLogManager;
  25.     /**
  26.      * @param TokenStorageInterface $tokenStorage
  27.      * @param ActionLogService $actionLogManager
  28.      */
  29.     public function __construct(
  30.         TokenStorageInterface $tokenStorage,
  31.         ActionLogService $actionLogManager
  32.     ) {
  33.         $this->tokenStorage $tokenStorage;
  34.         $this->actionLogManager $actionLogManager;
  35.     }
  36.     /**
  37.      * @param ControllerEvent $event
  38.      * @return void
  39.      */
  40.     public function onKernelController(ControllerEvent $event): void
  41.     {
  42.         $token $this->tokenStorage->getToken();
  43.         /** @var User|null $user */
  44.         $user null;
  45.         if ($token instanceof UsernamePasswordToken) {
  46.             /** @var User $user */
  47.             $user $token->getUser();
  48.         }
  49.         $controller $event->getController();
  50.         if (!is_object($controller) && $controller[0] instanceof AbstractController) {
  51.             $request $event->getRequest();
  52.             $rawContent $request->getContent();
  53.             $content json_decode($rawContenttrue);
  54.             if (is_null($content)) {
  55.                 parse_str($request->getContent(), $parsed);
  56.                 if (!empty($parsed)) {
  57.                     $content json_decode(json_encode($parsed), true);
  58.                 }
  59.             }
  60.             ApiLogger::encryptPassword($content);
  61.             $this->actionLogManager->create($user$request$content);
  62.         }
  63.     }
  64. }