<?php
namespace App\EventSubscriber;
use App\Entity\Notification;
use App\Event\FriendConfirmationEvent;
use App\Event\FriendInvitationEvent;
use App\Event\FriendRejectionEvent;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FriendEventSubscriber implements EventSubscriberInterface
{
private $notificationService;
private $entityManager;
public function __construct(NotificationService $notificationService, EntityManagerInterface $entityManager)
{
$this->notificationService = $notificationService;
$this->entityManager = $entityManager;
}
public function onFriendConfirmation(FriendConfirmationEvent $event)
{
$friendRequest = $event->getFriendRequest();
$this->notificationService->create(
$friendRequest->getInvitedBy(),
Notification::TYPE_FRIEND_INVITATION_ACCEPTED,
[
'user_id' => $friendRequest->getInvited()->getId(),
'user_name' => $friendRequest->getInvited()->getName(),
'user_avatar_url' => $friendRequest->getInvited()->getAvatarUrl(),
]
);
}
public function onFriendInvitation(FriendInvitationEvent $event)
{
$friendRequest = $event->getFriendRequest();
$this->notificationService->create(
$friendRequest->getInvited(),
Notification::TYPE_FRIEND_INVITATION,
[
'user_id' => $friendRequest->getInvitedBy()->getId(),
'user_name' => $friendRequest->getInvitedBy()->getName(),
'user_avatar_url' => $friendRequest->getInvitedBy()->getAvatarUrl(),
]
);
}
public function onFriendRejection(FriendRejectionEvent $event)
{
$this->entityManager->remove($event->getFriendRequest());
$this->entityManager->flush();
}
public static function getSubscribedEvents()
{
return [
FriendConfirmationEvent::NAME => 'onFriendConfirmation',
FriendInvitationEvent::NAME => 'onFriendInvitation',
FriendRejectionEvent::NAME => 'onFriendRejection'
];
}
}