<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\User;
use App\Event\TimeEntryLogEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
/**
* Class TimeEntryEventSubscriber.
*/
class TimeEntryChangeEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private LoggerInterface $timeEntryLogger,
private Security $security
) {
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
TimeEntryLogEvent::class => 'logChangesNotByOwner',
];
}
public function logChangesNotByOwner(TimeEntryLogEvent $event): void
{
$currentUser = $this->security->getUser();
$timeEntry = $event->getTimeEntry();
$timeEntryUser = $timeEntry->getUser();
if ($currentUser instanceof User) {
if ($currentUser !== $timeEntryUser) {
$currentUserEmail = $currentUser->getEmail();
}
} else {
$currentUserEmail = 'Not Authenticated user';
}
if (isset($currentUserEmail)) {
$entityChangeSet = $event->getEntityChangeSet();
$msg = \sprintf(
'%1$s changed next properties in TimeEntry id = %2$s : %3$s',
$currentUserEmail,
$timeEntry->getId(),
\serialize($entityChangeSet)
);
$this->timeEntryLogger->notice($msg);
}
}
}