src/EventSubscriber/TimeEntryChangeEventSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\User;
  5. use App\Event\TimeEntryLogEvent;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. /**
  10.  * Class TimeEntryEventSubscriber.
  11.  */
  12. class TimeEntryChangeEventSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private LoggerInterface $timeEntryLogger,
  16.         private Security $security
  17.     ) {
  18.     }
  19.     /**
  20.      * {@inheritDoc}
  21.      */
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             TimeEntryLogEvent::class => 'logChangesNotByOwner',
  26.         ];
  27.     }
  28.     public function logChangesNotByOwner(TimeEntryLogEvent $event): void
  29.     {
  30.         $currentUser $this->security->getUser();
  31.         $timeEntry $event->getTimeEntry();
  32.         $timeEntryUser $timeEntry->getUser();
  33.         if ($currentUser instanceof User) {
  34.             if ($currentUser !== $timeEntryUser) {
  35.                 $currentUserEmail $currentUser->getEmail();
  36.             }
  37.         } else {
  38.             $currentUserEmail 'Not Authenticated user';
  39.         }
  40.         if (isset($currentUserEmail)) {
  41.             $entityChangeSet $event->getEntityChangeSet();
  42.             $msg \sprintf(
  43.                 '%1$s changed next properties in TimeEntry id = %2$s : %3$s',
  44.                 $currentUserEmail,
  45.                 $timeEntry->getId(),
  46.                 \serialize($entityChangeSet)
  47.             );
  48.             $this->timeEntryLogger->notice($msg);
  49.         }
  50.     }
  51. }