src/Infrastructure/EventListener/LogEventListener.php line 92

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Infrastructure\EventListener;
  4. use ApiPlatform\Api\IriConverterInterface;
  5. use ApiPlatform\Exception\ItemNotFoundException;
  6. use App\Application\Service\Log\LogService;
  7. use App\Domain\Entity\Activity;
  8. use App\Domain\Entity\Award;
  9. use App\Domain\Entity\Comment;
  10. use App\Domain\Entity\Destination;
  11. use App\Domain\Entity\DirectoryDepartment;
  12. use App\Domain\Entity\Log;
  13. use App\Domain\Entity\Offmap;
  14. use App\Domain\Entity\Partner;
  15. use App\Domain\Entity\Payment;
  16. use App\Domain\Entity\PaymentLog;
  17. use App\Domain\Entity\Person;
  18. use App\Domain\Entity\PersonPointOfInterest;
  19. use App\Domain\Entity\PointOfInterest;
  20. use App\Domain\Entity\PointOfInterest\Picture;
  21. use App\Domain\Entity\PointOfInterest\Revision;
  22. use App\Domain\Entity\PressKit;
  23. use App\Domain\Entity\PressStatement;
  24. use App\Domain\Entity\PressVisualkit;
  25. use App\Domain\Entity\PromoCode;
  26. use App\Domain\Entity\RevisionLog;
  27. use App\Domain\Entity\StatisticLog;
  28. use App\Domain\Entity\Step;
  29. use App\Domain\Entity\TeamMember;
  30. use App\Domain\Entity\Trip;
  31. use App\Domain\Entity\TripCategory;
  32. use App\Domain\Entity\TripCountry;
  33. use Doctrine\DBAL\Types\ConversionException;
  34. use Doctrine\ORM\Event\PostPersistEventArgs;
  35. use Exception;
  36. use InvalidArgumentException;
  37. use Symfony\Component\HttpFoundation\RequestStack;
  38. use Symfony\Component\HttpKernel\Event\RequestEvent;
  39. use Symfony\Component\Routing\RouterInterface;
  40. use Symfony\Component\Security\Core\Security;
  41. class LogEventListener
  42. {
  43.     private array $logEntities = [
  44.         Activity::class,
  45.         Award::class,
  46.         Comment::class,
  47.         Destination::class,
  48.         DirectoryDepartment::class,
  49.         Offmap::class,
  50.         Partner::class,
  51.         Payment::class,
  52.         Person::class,
  53.         PersonPointOfInterest::class,
  54.         Picture::class,
  55.         PointOfInterest::class,
  56.         PressKit::class,
  57.         PressStatement::class,
  58.         PressVisualkit::class,
  59.         PromoCode::class,
  60.         Step::class,
  61.         TeamMember::class,
  62.         Trip::class,
  63.         TripCategory::class,
  64.         TripCountry::class,
  65.     ];
  66.     private array $logRoutes = [
  67.         'payment_export' => PaymentLog::EXPORT,
  68.         'get_revision'   => StatisticLog::READ,
  69.         'get_activity'   => StatisticLog::READ,
  70.     ];
  71.     private array $excludeRoutes = [
  72.         'search_main_revisions',
  73.         'search_activities'
  74.     ];
  75.     public function __construct(
  76.         public IriConverterInterface $converter,
  77.         public RouterInterface $router,
  78.         private readonly LogService $logService,
  79.         private readonly Security $security,
  80.         private readonly RequestStack $request,
  81.     ) {}
  82.     /**
  83.      * @throws Exception
  84.      */
  85.     public function onKernelView(RequestEvent $event): void
  86.     {
  87.         $request $event->getRequest();
  88.         $requestRoute $request->attributes->get('_route');
  89.         if (!$requestRoute || in_array($requestRoute$this->excludeRoutestrue)) {
  90.             return;
  91.         }
  92.         $entityClass $request->attributes->get('_api_resource_class');
  93.         if (
  94.             !$entityClass ||
  95.             (!array_key_exists($requestRoute$this->logRoutes) && !in_array($entityClass$this->logEntitiestrue))
  96.         ) {
  97.             return;
  98.         }
  99.         $logEvent null;
  100.         $data null;
  101.         $method $request->getMethod();
  102.         if ($method === "POST") {
  103.             return;
  104.         }
  105.         if ($method === "DELETE") {
  106.             $logEvent Log::DELETE;
  107.         }
  108.         if ($method === "PUT") {
  109.             $logEvent Log::UPDATE;
  110.             $data $request->getContent();
  111.         }
  112.         if (array_key_exists($requestRoute$this->logRoutes)) {
  113.             $logEvent $this->logRoutes[$requestRoute];
  114.         }
  115.         if (!$logEvent) {
  116.             return;
  117.         }
  118.         try {
  119.             $iri $request->getRequestUri();
  120.             $iri explode('/'$iri);
  121.             array_splice($iri4);
  122.             $iri implode('/'$iri);
  123.             $entity $this->converter->getResourceFromIri($iri);
  124.         } catch (ItemNotFoundException InvalidArgumentException ConversionException) {
  125.             $entity null;
  126.         }
  127.         $user $this->security->getUser();
  128.         $this->logService->createLog(
  129.             logClass Log::class,
  130.             action   $logEvent,
  131.             entity   $entityClass,
  132.             author   $user,
  133.             entityId $entity?->id,
  134.             data     : (string)$data,
  135.             flush    true,
  136.             httpAgent$request->headers->get('user-agent'),
  137.         );
  138.     }
  139.     /**
  140.      * @throws Exception
  141.      */
  142.     public function postPersist(PostPersistEventArgs $args): void
  143.     {
  144.         $object $args->getObject();
  145.         if (!in_array($object::class, $this->logEntitiestrue)) {
  146.             return;
  147.         }
  148.         $user $this->security->getUser();
  149.         $request $this->request->getMainRequest();
  150.         if (!$request) {
  151.             return;
  152.         }
  153.         $data $request->getContent();
  154.         if ($request->getContentType() === 'form') {
  155.             $data $request->request->all();
  156.         }
  157.         if ($object?->id === null) {
  158.             return;
  159.         }
  160.         $this->logService->createLog(
  161.             logClass Log::class,
  162.             action   Log::CREATE,
  163.             entity   $object::class,
  164.             author   $user,
  165.             entityId $object->id,
  166.             data     json_encode($dataJSON_THROW_ON_ERROR),
  167.             flush    true,
  168.             httpAgent$request->headers->get('user-agent'),
  169.         );
  170.     }
  171. }