<?php
namespace App\Subscriber\UserGroups;
use App\Dictionary\MailPriority;
use App\Event\UserGroups\UserGroupPostCreatedEvent;
use App\Event\UserGroups\UserGroupPostNotificationTransmitEvent;
use App\Service\Mail\MailService;
use App\Service\User\AccountService;
use App\Service\UserGroups\PostNotificationService;
use App\Subscriber\AbstractMailSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class UserGroupPostNotificationSubscriber extends AbstractMailSubscriber implements EventSubscriberInterface
{
/**
* @var PostNotificationService
*/
protected $service;
/**
* UserGroupPostNotificationSubscriber constructor.
*/
public function __construct(PostNotificationService $notificationService, AccountService $accountService,
MailService $mailService, Environment $twig)
{
parent::__construct($accountService, $mailService, $twig);
$this->service = $notificationService;
}
public static function getSubscribedEvents(): array
{
return [
UserGroupPostCreatedEvent::class => [
['onGroupPostSendNotification', 0],
],
UserGroupPostNotificationTransmitEvent::class => [
['onGroupPostNotificationSendEmail', 0],
],
];
}
public function onGroupPostSendNotification(UserGroupPostCreatedEvent $event)
{
$this->service->enqueuePostNotification($event->getEntity());
}
public function onGroupPostNotificationSendEmail(UserGroupPostNotificationTransmitEvent $event): bool
{
if (!$event->getSendEmail()) {
return false;
}
$post = $event->getEntity();
$recipient = $event->getRecipient();
$account = $recipient->getAccount();
$group = $post->getThread()->getUserGroup();
$mail = new \Swift_Message('Beitrag in Gruppe '.$group->getName());
$mail->addTo($account->getEmail());
$mail->setFrom('server@frivol.com', 'Frivol.com');
$mail->setBody($this->getTwigEnvironment()->render('emails/groups-new-post.html.twig', [
'member' => $recipient,
'author' => $post->getAuthor(),
'group' => $group,
'thread' => $post->getThread(),
'login' => $this->getAccountService()->getAutologinParameter($account),
'locale' => 'de',
]), 'text/html');
return $this->getMailService()->queueMessage($mail, MailPriority::GROUP_NEW_POST);
}
}