src/EventSubscriber/FriendEventSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Notification;
  4. use App\Event\FriendConfirmationEvent;
  5. use App\Event\FriendInvitationEvent;
  6. use App\Event\FriendRejectionEvent;
  7. use App\Service\NotificationService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class FriendEventSubscriber implements EventSubscriberInterface
  11. {
  12.   private $notificationService;
  13.   private $entityManager;
  14.   public function __construct(NotificationService $notificationServiceEntityManagerInterface $entityManager)
  15.   {
  16.     $this->notificationService $notificationService;
  17.     $this->entityManager $entityManager;
  18.   }
  19.   public function onFriendConfirmation(FriendConfirmationEvent $event)
  20.   {
  21.     $friendRequest $event->getFriendRequest();
  22.     $this->notificationService->create(
  23.       $friendRequest->getInvitedBy(),
  24.       Notification::TYPE_FRIEND_INVITATION_ACCEPTED,
  25.       [
  26.         'user_id' => $friendRequest->getInvited()->getId(),
  27.         'user_name' => $friendRequest->getInvited()->getName(),
  28.         'user_avatar_url' => $friendRequest->getInvited()->getAvatarUrl(),
  29.       ]
  30.     );
  31.   }
  32.   public function onFriendInvitation(FriendInvitationEvent $event)
  33.   {
  34.     $friendRequest $event->getFriendRequest();
  35.     $this->notificationService->create(
  36.       $friendRequest->getInvited(),
  37.       Notification::TYPE_FRIEND_INVITATION,
  38.       [
  39.         'user_id' => $friendRequest->getInvitedBy()->getId(),
  40.         'user_name' => $friendRequest->getInvitedBy()->getName(),
  41.         'user_avatar_url' => $friendRequest->getInvitedBy()->getAvatarUrl(),
  42.       ]
  43.     );
  44.   }
  45.   public function onFriendRejection(FriendRejectionEvent $event)
  46.   {
  47.     $this->entityManager->remove($event->getFriendRequest());
  48.     $this->entityManager->flush();
  49.   }
  50.   public static function getSubscribedEvents()
  51.   {
  52.     return [
  53.       FriendConfirmationEvent::NAME => 'onFriendConfirmation',
  54.       FriendInvitationEvent::NAME => 'onFriendInvitation',
  55.       FriendRejectionEvent::NAME => 'onFriendRejection'
  56.     ];
  57.   }
  58. }