<?php
namespace App\Subscriber\Content;
use App\Dictionary\ContentStatus;
use App\Dictionary\ConversationMessageSource;
use App\Dictionary\ConversationMessageType;
use App\Event\Content\ContentCheckedEvent;
use App\Lib\Content\ContentServiceAwareInterface;
use App\Service\Content\ContentService;
use App\Service\MemberService;
use App\Service\Messenger\MessengerService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentCheckSubscriber implements EventSubscriberInterface, ContentServiceAwareInterface
{
protected ContentService $service;
protected MessengerService $messenger;
protected MemberService $memberService;
/**
* ContentCheckSubscriber constructor.
*/
public function __construct(ContentService $service, MessengerService $messenger, MemberService $memberService)
{
$this->setContentService($service);
$this->setMessenger($messenger);
$this->setMemberService($memberService);
}
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;
}
public function setContentService(ContentService $service): ContentServiceAwareInterface
{
$this->service = $service;
return $this;
}
public function getContentService(): ContentService
{
return $this->service;
}
public static function getSubscribedEvents(): array
{
return [
ContentCheckedEvent::class => [
['onCheckedAndRejected', 0],
['onCheckedAndAcceptedSendMessage', 1],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onCheckedAndRejected(ContentCheckedEvent $event)
{
if ($event->isAccepted()) {
return;
}
$content = $event->getContent();
$author = $content->getMember();
// change to none not new as of gitlab #486
// when its set to new, we can only edit the preview images. but most likely, the title, categories,
// actors need to be changed. so one step back even more.
$this->service->changeContentStatus($content, ContentStatus::NONE);
// Send messenger message to author (from support)
$supportMember = $this->getMemberService()->getSupportMember();
$type = ConversationMessageType::TEXT;
$source = ConversationMessageSource::SYSTEM;
$this->getMessenger()->sendDirectMessage($supportMember, $author, $event->getRejectMessage($content), $type, $source);
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onCheckedAndAcceptedSendMessage(ContentCheckedEvent $event)
{
if (!$event->isAccepted()) {
return;
}
$content = $event->getContent();
$author = $content->getMember();
// Send messenger message to author (from support)
$supportMember = $this->getMemberService()->getSupportMember();
$type = ConversationMessageType::TEXT;
$source = ConversationMessageSource::SYSTEM;
$this->getMessenger()->sendDirectMessage($supportMember, $author, $event->getAcceptMessage($content), $type, $source);
}
}