Easy_solution/src/EventSubscriber/UserSubscriber.php

50 lines
1.5 KiB
PHP

<?php
namespace App\EventSubscriber;
use App\Event\UserCreatedEvent;
use App\Service\ActionService;
use App\Service\EmailService;
use App\Service\LoggerService;
use App\Service\UserService; // Only if you need helper methods, otherwise avoid to prevent circular ref
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly EmailService $emailService,
private readonly LoggerService $loggerService,
private readonly ActionService $actionService,
) {
}
public static function getSubscribedEvents(): array
{
return [
UserCreatedEvent::class => 'onUserCreated',
];
}
public function onUserCreated(UserCreatedEvent $event): void
{
$user = $event->getNewUser();
$actingUser = $event->getActingUser();
// 1. Send Email
if ($user->getPasswordToken()) {
$this->emailService->sendPasswordSetupEmail($user, $user->getPasswordToken());
}
// 2. Logic-based Logging (Moved from Service)
if (in_array('ROLE_ADMIN', $actingUser->getRoles(), true)) {
$this->loggerService->logSuperAdmin(
$user->getId(),
$actingUser->getId(),
"Super Admin created new user: " . $user->getUserIdentifier()
);
}
// 3. General Audit Trail
$this->actionService->createAction("USER_CREATED", $actingUser, null, $user->getUserIdentifier());
}
}