Easy_solution/src/Service/NotificationService.php

193 lines
7.3 KiB
PHP

<?php
namespace App\Service;
use App\Entity\Organizations;
use App\Entity\User;
use App\Message\NotificationMessage;
use Symfony\Component\Messenger\MessageBusInterface;
class NotificationService
{
public const TYPE_USER_JOINED = 'user_joined';
public const TYPE_USER_INVITED = 'user_invited';
public const TYPE_USER_ACCEPTED = 'user_accepted';
public const TYPE_USER_REMOVED = 'user_removed';
public const TYPE_USER_DEACTIVATED = 'user_deactivated';
public const TYPE_ORG_UPDATE = 'org_update';
public const TYPE_APP_ACCESS = 'app_access';
public const TYPE_ROLE_CHANGED = 'role_changed';
public function __construct(
private readonly MessageBusInterface $messageBus
) {
}
public function notifyUserInvited(User $recipient, User $invitedUser, Organizations $organization): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_USER_INVITED,
title: 'Invitation envoyée',
message: sprintf('%s %s a été invité à rejoindre %s', $invitedUser->getName(), $invitedUser->getSurname(), $organization->getName()),
data: [
'userId' => $invitedUser->getId(),
'userName' => $invitedUser->getName() . ' ' . $invitedUser->getSurname(),
'userEmail' => $invitedUser->getEmail(),
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
],
organization: $organization
);
}
public function notifyUserAcceptedInvite(User $recipient, User $acceptedUser, Organizations $organization): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_USER_ACCEPTED,
title: 'Invitation acceptée',
message: sprintf('%s %s a accepté l\'invitation à %s', $acceptedUser->getName(), $acceptedUser->getSurname(), $organization->getName()),
data: [
'userId' => $acceptedUser->getId(),
'userName' => $acceptedUser->getName() . ' ' . $acceptedUser->getSurname(),
'userEmail' => $acceptedUser->getEmail(),
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
],
organization: $organization
);
}
public function notifyUserDeactivated(User $recipient, User $removedUser, Organizations $organization): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_USER_DEACTIVATED,
title: 'Membre retiré',
message: sprintf('%s %s a été désactivé de %s', $removedUser->getName(), $removedUser->getSurname(), $organization->getName()),
data: [
'userId' => $removedUser->getId(),
'userName' => $removedUser->getName() . ' ' . $removedUser->getSurname(),
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
],
organization: $organization
);
}
public function notifyUserActivated(User $recipient, User $activatedUser, Organizations $organization): void
{
$this->send(
recipient: $recipient,
type: 'user_activated',
title: 'Membre réactivé',
message: sprintf('%s %s a été réactivé dans %s', $activatedUser->getName(), $activatedUser->getSurname(), $organization->getName()),
data: [
'userId' => $activatedUser->getId(),
'userName' => $activatedUser->getName() . ' ' . $activatedUser->getSurname(),
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
],
organization: $organization
);
}
public function notifyOrganizationUpdate(User $recipient, Organizations $organization, string $action): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_ORG_UPDATE,
title: 'Organisation mise à jour',
message: sprintf('L\'organisation %s a été %s', $organization->getName(), $action),
data: [
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
'action' => $action,
],
organization: $organization
);
}
public function notifyAppAccessChanged(User $recipient, Organizations $organization, string $appName, bool $granted): void
{
$action = $granted ? 'autorisé' : 'retiré';
$this->send(
recipient: $recipient,
type: self::TYPE_APP_ACCESS,
title: 'Accès application modifié',
message: sprintf('L\'accès à %s a été %s pour %s', $appName, $action, $organization->getName()),
data: [
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
'appName' => $appName,
'granted' => $granted,
],
organization: $organization
);
}
public function notifyRoleChanged(User $recipient, User $targetUser, Organizations $organization, string $newRole): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_ROLE_CHANGED,
title: 'Rôle modifié',
message: sprintf('Le rôle de %s %s a été changé en %s dans %s',
$targetUser->getName(),
$targetUser->getSurname(),
$newRole,
$organization->getName()
),
data: [
'userId' => $targetUser->getId(),
'userName' => $targetUser->getName() . ' ' . $targetUser->getSurname(),
'organizationId' => $organization->getId(),
'organizationName' => $organization->getName(),
'newRole' => $newRole,
],
organization: $organization
);
}
public function notifyUserDeleted(User $recipient, User $deletedUser, ?Organizations $organization = null): void
{
$this->send(
recipient: $recipient,
type: self::TYPE_USER_REMOVED,
title: 'Utilisateur supprimé',
message: sprintf('L\'utilisateur %s %s a été supprimé du système',
$deletedUser->getName(),
$deletedUser->getSurname()
),
data: [
'userId' => $deletedUser->getId(),
'userName' => $deletedUser->getName() . ' ' . $deletedUser->getSurname(),
'userEmail' => $deletedUser->getEmail(),
],
organization: $organization
);
}
private function send(
User $recipient,
string $type,
string $title,
string $message,
?array $data = null,
?Organizations $organization = null
): void {
$notificationMessage = new NotificationMessage(
userId: $recipient->getId(),
type: $type,
title: $title,
message: $message,
data: $data,
organizationId: $organization?->getId()
);
$this->messageBus->dispatch($notificationMessage);
}
}