src/Subscriber/UserGroups/UserGroupRequestSubscriber.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\UserGroups;
  3. use App\Dictionary\ConversationMessageSource;
  4. use App\Dictionary\ConversationMessageType;
  5. use App\Dictionary\MemberMediaFolder;
  6. use App\Event\UserGroups\GroupConfirmedEvent;
  7. use App\Event\UserGroups\GroupRejectedEvent;
  8. use App\Event\UserGroups\JoinGroupEvent;
  9. use App\Service\Media\MemberMediaService;
  10. use App\Service\MemberService;
  11. use App\Service\Messenger\MessengerService;
  12. use App\Service\UserGroups\GroupService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. class UserGroupRequestSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EventDispatcherInterface
  19.      */
  20.     protected $dispatcher;
  21.     /**
  22.      * @var MessengerService
  23.      */
  24.     protected $messenger;
  25.     /**
  26.      * @var MemberService
  27.      */
  28.     protected $memberService;
  29.     /**
  30.      * @var GroupService
  31.      */
  32.     protected $groupService;
  33.     /**
  34.      * @var MemberMediaService
  35.      */
  36.     protected $memberMediaService;
  37.     /**
  38.      * UserGroupRequestSubscriber constructor.
  39.      */
  40.     public function __construct(EventDispatcherInterface $dispatcherGroupService $groupService,
  41.         MessengerService $messengerServiceMemberService $memberService,
  42.         MemberMediaService $memberMediaService)
  43.     {
  44.         $this->dispatcher $dispatcher;
  45.         $this->groupService $groupService;
  46.         $this->memberMediaService $memberMediaService;
  47.         $this->setMessenger($messengerService);
  48.         $this->setMemberService($memberService);
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             GroupConfirmedEvent::class => [
  54.                 ['onGroupRequestConfirmedActivateGroup'0],
  55.                 ['onGroupRequestConfirmedSendDirectMessage'1],
  56.                 ['onGroupRequestConfirmedJoinGroup'0],
  57.             ],
  58.             GroupRejectedEvent::class => [
  59.                 ['onGroupRequestRejected'0],
  60.             ],
  61.         ];
  62.     }
  63.     public function setMemberService(MemberService $service): self
  64.     {
  65.         $this->memberService $service;
  66.         return $this;
  67.     }
  68.     public function getMemberService(): MemberService
  69.     {
  70.         return $this->memberService;
  71.     }
  72.     public function setMessenger(MessengerService $service): self
  73.     {
  74.         $this->messenger $service;
  75.         return $this;
  76.     }
  77.     public function getMessenger(): MessengerService
  78.     {
  79.         return $this->messenger;
  80.     }
  81.     /**
  82.      * @throws \Doctrine\ORM\ORMException
  83.      * @throws \Doctrine\ORM\OptimisticLockException
  84.      */
  85.     public function onGroupRequestRejected(GroupRejectedEvent $event)
  86.     {
  87.         $group $event->getGroup();
  88.         $founder $group->getFounder();
  89.         // Send messenger message (from support)
  90.         $supportMember $this->getMemberService()->getSupportMember();
  91.         $type ConversationMessageType::TEXT;
  92.         $source ConversationMessageSource::SYSTEM;
  93.         $this->getMessenger()->sendDirectMessage($supportMember$founder$this->getRejectedMessage($event), $type$source);
  94.     }
  95.     /**
  96.      * @throws \Doctrine\ORM\ORMException
  97.      * @throws \Doctrine\ORM\OptimisticLockException
  98.      */
  99.     public function onGroupRequestConfirmedActivateGroup(GroupConfirmedEvent $event)
  100.     {
  101.         $group $event->getGroup();
  102.         if ($group->getPhoto()) {
  103.             $this->memberMediaService->moveFromTempToFolder($group->getPhoto(), MemberMediaFolder::GROUP);
  104.         }
  105.         $group->setIsActive(true);
  106.         if (!$group->getSlug()) {
  107.             $group->setSlug($group->getName());
  108.         }
  109.         $this->groupService->storeEntity($group);
  110.     }
  111.     /**
  112.      * @throws \Doctrine\ORM\ORMException
  113.      * @throws \Doctrine\ORM\OptimisticLockException
  114.      */
  115.     public function onGroupRequestConfirmedSendDirectMessage(GroupConfirmedEvent $event)
  116.     {
  117.         $founder $event->getGroup()->getFounder();
  118.         // Send messenger message (from support)
  119.         $supportMember $this->getMemberService()->getSupportMember();
  120.         $type ConversationMessageType::TEXT;
  121.         $source ConversationMessageSource::SYSTEM;
  122.         try {
  123.             $this->getMessenger()->sendDirectMessage($supportMember$founder$this->getConfirmedMessage($event), $type$source);
  124.         } catch (\InvalidArgumentException $e) {
  125.         }
  126.     }
  127.     public function onGroupRequestConfirmedJoinGroup(GroupConfirmedEvent $event)
  128.     {
  129.         $group $event->getGroup();
  130.         $joinGroupEvent = new JoinGroupEvent($group$group->getFounder(), true);
  131.         $this->dispatcher->dispatch($joinGroupEvent);
  132.     }
  133.     protected function getConfirmedMessage(GroupConfirmedEvent $event): string
  134.     {
  135.         $group $event->getGroup();
  136.         $founder $group->getFounder();
  137.         $msgFormat 'Hallo %s, deine Gruppe "%s" wurde akzeptiert und kann nun von dir genutzt werden. Viel Spaß!';
  138.         return sprintf($msgFormat$founder->getUsername(), $group->getName());
  139.     }
  140.     protected function getRejectedMessage(GroupRejectedEvent $event): string
  141.     {
  142.         $group $event->getGroup();
  143.         $founder $group->getFounder();
  144.         $msgFormat 'Hallo %s, deine Gruppe "%s" wurde leider abgelehnt. Grund: %s';
  145.         return sprintf($msgFormat$founder->getUsername(), $group->getName(), $event->getReason());
  146.     }
  147. }