src/Subscriber/User/FriendshipSubscriber.php line 131

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\User;
  3. use App\Dictionary\ConversationMessageSource;
  4. use App\Dictionary\ConversationMessageType;
  5. use App\Dictionary\FriendshipStatus;
  6. use App\Entity\Member;
  7. use App\Event\RedisEventManager;
  8. use App\Event\User\FriendshipConfirmedEvent;
  9. use App\Event\User\FriendshipRemovedEvent;
  10. use App\Event\User\FriendshipRequestedEvent;
  11. use App\Event\User\FriendshipRequestPreconditionCheckEvent;
  12. use App\Exception\User\FriendshipRequestFailedException;
  13. use App\Repository\FriendshipRepository;
  14. use App\Service\Media\MemberMediaService;
  15. use App\Service\Messenger\MessengerService;
  16. use App\Service\User\FriendshipService;
  17. use App\Subscriber\AbstractRedisEventManagerSubscriber;
  18. use Doctrine\ORM\OptimisticLockException;
  19. use Doctrine\ORM\ORMException;
  20. use Frivol\Common\Service\Notification\FriendshipRequestNotification;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Routing\RouterInterface;
  23. class FriendshipSubscriber extends AbstractRedisEventManagerSubscriber implements EventSubscriberInterface
  24. {
  25.     public function __construct(
  26.         private readonly MemberMediaService $memberMediaService,
  27.         private readonly FriendshipService $service,
  28.         private readonly MessengerService $messenger,
  29.         private readonly FriendshipRepository $friendshipRepository,
  30.         RedisEventManager $redisEventManager,
  31.         RouterInterface $router
  32.     ) {
  33.         parent::__construct($redisEventManager$router);
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             FriendshipRemovedEvent::class => [
  39.                 ['onFriendshipRemovedRemoveOpposite'0],
  40.             ],
  41.             FriendshipConfirmedEvent::class => [
  42.                 ['onFriendshipConfirmedConfirmOpposite'0],
  43.                 ['onFriendshipConfirmedSendConversationMessage'0],
  44.             ],
  45.             FriendshipRequestedEvent::class => [
  46.                 ['onFriendshipRequestedSendNotification'0],
  47.                 ['onFriendshipRequestedSendConversationMessage'0],
  48.             ],
  49.             FriendshipRequestPreconditionCheckEvent::class => [
  50.                 ['onFriendshipRequestPreconditionCheck'0],
  51.             ]
  52.         ];
  53.     }
  54.     public function onFriendshipRequestPreconditionCheck(FriendshipRequestPreconditionCheckEvent $event): void
  55.     {
  56.         $sender $event->getSender();
  57.         $recipient $event->getReceiver();
  58.         if ($this->friendshipRepository->canSendFriendshipRequest($sender$recipient)) {
  59.             return;
  60.         }
  61.         $error = new FriendshipRequestFailedException("Freundschaftsanfragen sind nur bei Stammkunden möglich");
  62.         $event->setStopReason($error);
  63.         throw $error;
  64.     }
  65.     public function onFriendshipRequestedSendNotification(FriendshipRequestedEvent $event): void
  66.     {
  67.         $issuer $event->getFriendship()->getOwnerMember();
  68.         $target $event->getFriendship()->getTargetMember();
  69.         $notification = new FriendshipRequestNotification();
  70.         $notification->setData([
  71.             'image' => $this->getMediaUrl($issuer),
  72.             'username' => $issuer->getUsername(),
  73.             'sex' => $issuer->getSex(),
  74.         ]);
  75.         $this->getRedisEventManager()->publishToMemberWithFrom($issuer$target$notification);
  76.     }
  77.     public function getMediaUrl(Member $member): ?string
  78.     {
  79.         if (!$member->getMainPhoto()) {
  80.             return null;
  81.         }
  82.         return $this->memberMediaService->getUrlForMemberMedia($member->getMainPhoto(), false'100x100');
  83.     }
  84.     /**
  85.      * @throws ORMException
  86.      * @throws OptimisticLockException
  87.      */
  88.     public function onFriendshipRemovedRemoveOpposite(FriendshipRemovedEvent $event): void
  89.     {
  90.         $entity $event->getFriendship();
  91.         $opposite $this->service->getOppositeFriendship($entity);
  92.         if ($opposite) {
  93.             // dont trigger event through removeFriendship() for this one
  94.             $this->service->removeEntity($opposite);
  95.         }
  96.     }
  97.     /**
  98.      * @throws ORMException
  99.      * @throws OptimisticLockException
  100.      */
  101.     public function onFriendshipConfirmedConfirmOpposite(FriendshipConfirmedEvent $event): void
  102.     {
  103.         $entity $event->getFriendship();
  104.         $opposite $this->service->getOppositeFriendship($entity);
  105.         if ($opposite && FriendshipStatus::CONFIRMED !== $opposite->getStatus()) {
  106.             // dont trigger event through confirmFriendship() for this one
  107.             $opposite->setStatus(FriendshipStatus::CONFIRMED);
  108.             $this->service->storeEntity($opposite);
  109.         }
  110.     }
  111.     /**
  112.      * @throws ORMException
  113.      * @throws OptimisticLockException
  114.      */
  115.     public function onFriendshipConfirmedSendConversationMessage(FriendshipConfirmedEvent $event): void
  116.     {
  117.         $sender $event->getFriendship()->getOwnerMember();
  118.         $recipient $event->getFriendship()->getTargetMember();
  119.         $type ConversationMessageType::FRIENDSHIP_CONFIRMED;
  120.         $text 'Ich habe Deine Freundschaftsanfrage akzeptiert!';
  121.         $source ConversationMessageSource::SYSTEM;
  122.         $this->messenger->sendDirectMessage($sender$recipient$text$type$source);
  123.     }
  124.     /**
  125.      * @throws ORMException
  126.      * @throws OptimisticLockException
  127.      */
  128.     public function onFriendshipRequestedSendConversationMessage(FriendshipRequestedEvent $event): void
  129.     {
  130.         $sender $event->getFriendship()->getOwnerMember();
  131.         $recipient $event->getFriendship()->getTargetMember();
  132.         $type ConversationMessageType::FRIENDSHIP_REQUEST;
  133.         $source ConversationMessageSource::SYSTEM;
  134.         $text 'Ich habe Dir eine Freundschaftsanfrage geschickt!';
  135.         $this->messenger->sendDirectMessage($sender$recipient$text$type$source);
  136.     }
  137. }