php - Save flash message on database after "addFlash" method is called on Symfony Controller ← (PHP, Symfony, Bootstrap)

I have a Symfony service to add an alert (bootstrap alert in DOM) to database to allow administrators to audit the usage of the application and also this service saves the alert in session storage to display the alerts until the user closes each alert.

This is the code for the service method:

public function addAlert(DateTimeInterface $eventDatetime, string $eventType, string $data, UserInterface $user, string $alertId): bool
{
    $success = false;
    try {
        //Build alert entity
        $alert = new UserAlert();
        $alert->setDatetimeUTC($eventDatetime);
        $alert->setEventType($eventType);
        $alert->setMessage($data);
        $alert->setUser($user);

        //Save in database
        $this->entityManager->persist($alert);
        $this->entityManager->flush();

        //Save in session
        $success = $this->saveAlertInSession($alertId, $eventType, $data);
    } catch (Throwable $throwable) {
        $this->logger->critical("Add User Alert Service: Ko. Details: {$throwable->getMessage()}.");
    }

    return $success;
}

private function saveAlertInSession(string $alertId, string $eventType, string $data): bool
{
    $alerts = [];
    try {
        //Check if any alert is stored to retrieve
        if ($this->session->has('alerts')) {
            $alerts = $this->session->get('alerts');
        }

        //Add new alert
        $alerts[$alertId] = [
            'type' => $eventType,
            'data' => $data,
        ];

        //Save to session
        $this->session->set('alerts', $alerts);

        $success = true;
    } catch (Throwable $throwable) {
        $success = false;
        $message = "Save Alert Session: Ko. Details: {$throwable->getMessage()}.";
        $this->logger->critical($message);
    }

    return $success;
}

My question is if Symfony has an event listener to automatically call this service method each time I write the following lines on controllers:

$this->addFlash('success', $message);
$this->addFlash('error', $message);
$this->addFlash('warning', $message);

Now, after I add a message to the Flash Bag message, my controller executes the following instruction:

$this->alertsService->addAlert($datetimeUTCNow, $eventType, $message, $user, $alertId);

Do you know any event listener to avoid writing this line each time the controller calls to addFlash method? I am using Symfony 5 and PHP 7.3

Thank you.

Answer



Solution:

Here is my solution to override the addFlash method:

protected function addFlash(string $type, string $message): void
{
    // Generate the required arguments on userAlerts service
    try {
        $datetimeUTC = new DateTime('now', new DateTimeZone('UTC'));
        $alertId = Uuid::uuid4();

        $this->userAlertsService->addAlert($datetimeUTC, $type, $message, $this->getUser(), $alertId);
    } catch (Throwable $throwable) {
        $this->logger->critical("Add Flash: Ko. Details: {$throwable->getMessage()}");
    }

    parent::addFlash($type, $message);
}

Source