<?php
namespace App\Subscriber\UserGroups;
use App\Dictionary\ConversationMessageSource;
use App\Dictionary\ConversationMessageType;
use App\Dictionary\MemberMediaFolder;
use App\Event\UserGroups\GroupConfirmedEvent;
use App\Event\UserGroups\GroupRejectedEvent;
use App\Event\UserGroups\JoinGroupEvent;
use App\Service\Media\MemberMediaService;
use App\Service\MemberService;
use App\Service\Messenger\MessengerService;
use App\Service\UserGroups\GroupService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class UserGroupRequestSubscriber implements EventSubscriberInterface
{
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var MessengerService
*/
protected $messenger;
/**
* @var MemberService
*/
protected $memberService;
/**
* @var GroupService
*/
protected $groupService;
/**
* @var MemberMediaService
*/
protected $memberMediaService;
/**
* UserGroupRequestSubscriber constructor.
*/
public function __construct(EventDispatcherInterface $dispatcher, GroupService $groupService,
MessengerService $messengerService, MemberService $memberService,
MemberMediaService $memberMediaService)
{
$this->dispatcher = $dispatcher;
$this->groupService = $groupService;
$this->memberMediaService = $memberMediaService;
$this->setMessenger($messengerService);
$this->setMemberService($memberService);
}
public static function getSubscribedEvents(): array
{
return [
GroupConfirmedEvent::class => [
['onGroupRequestConfirmedActivateGroup', 0],
['onGroupRequestConfirmedSendDirectMessage', 1],
['onGroupRequestConfirmedJoinGroup', 0],
],
GroupRejectedEvent::class => [
['onGroupRequestRejected', 0],
],
];
}
public function setMemberService(MemberService $service): self
{
$this->memberService = $service;
return $this;
}
public function getMemberService(): MemberService
{
return $this->memberService;
}
public function setMessenger(MessengerService $service): self
{
$this->messenger = $service;
return $this;
}
public function getMessenger(): MessengerService
{
return $this->messenger;
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onGroupRequestRejected(GroupRejectedEvent $event)
{
$group = $event->getGroup();
$founder = $group->getFounder();
// Send messenger message (from support)
$supportMember = $this->getMemberService()->getSupportMember();
$type = ConversationMessageType::TEXT;
$source = ConversationMessageSource::SYSTEM;
$this->getMessenger()->sendDirectMessage($supportMember, $founder, $this->getRejectedMessage($event), $type, $source);
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onGroupRequestConfirmedActivateGroup(GroupConfirmedEvent $event)
{
$group = $event->getGroup();
if ($group->getPhoto()) {
$this->memberMediaService->moveFromTempToFolder($group->getPhoto(), MemberMediaFolder::GROUP);
}
$group->setIsActive(true);
if (!$group->getSlug()) {
$group->setSlug($group->getName());
}
$this->groupService->storeEntity($group);
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onGroupRequestConfirmedSendDirectMessage(GroupConfirmedEvent $event)
{
$founder = $event->getGroup()->getFounder();
// Send messenger message (from support)
$supportMember = $this->getMemberService()->getSupportMember();
$type = ConversationMessageType::TEXT;
$source = ConversationMessageSource::SYSTEM;
try {
$this->getMessenger()->sendDirectMessage($supportMember, $founder, $this->getConfirmedMessage($event), $type, $source);
} catch (\InvalidArgumentException $e) {
}
}
public function onGroupRequestConfirmedJoinGroup(GroupConfirmedEvent $event)
{
$group = $event->getGroup();
$joinGroupEvent = new JoinGroupEvent($group, $group->getFounder(), true);
$this->dispatcher->dispatch($joinGroupEvent);
}
protected function getConfirmedMessage(GroupConfirmedEvent $event): string
{
$group = $event->getGroup();
$founder = $group->getFounder();
$msgFormat = 'Hallo %s, deine Gruppe "%s" wurde akzeptiert und kann nun von dir genutzt werden. Viel Spaß!';
return sprintf($msgFormat, $founder->getUsername(), $group->getName());
}
protected function getRejectedMessage(GroupRejectedEvent $event): string
{
$group = $event->getGroup();
$founder = $group->getFounder();
$msgFormat = 'Hallo %s, deine Gruppe "%s" wurde leider abgelehnt. Grund: %s';
return sprintf($msgFormat, $founder->getUsername(), $group->getName(), $event->getReason());
}
}