Skip to content

Commit

Permalink
send mails from queue
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-stanek committed Oct 13, 2023
1 parent 95122b8 commit 62d98a7
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 225 deletions.
9 changes: 9 additions & 0 deletions app/ApiModule/Presenters/MailPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Model\Acl\Permission;
use App\Model\Acl\SrsResource;
use App\Model\Mailing\Commands\SendMails;
use App\Model\Settings\Commands\SetSettingStringValue;
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
Expand All @@ -26,6 +27,14 @@ class MailPresenter extends ApiBasePresenter
#[Inject]
public QueryBus $queryBus;

/**
* Odešle e-maily z fronty.
*/
public function actionSend(): void
{
$this->commandBus->handle(new SendMails());
}

/**
* Ověří e-mail semináře.
*
Expand Down
8 changes: 1 addition & 7 deletions app/Mailing/SrsMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class SrsMail extends AbstractMail implements IComposableMail
public function compose(Message $message, IMessageData|null $mailData = null): void
{
$message->setFrom($mailData->getFrom()->getEmail(), $mailData->getFrom()->getName());

foreach ($mailData->getRecipients() as $recipient) {
if (! empty($recipient->getEmail())) {
$message->addBcc($recipient->getEmail(), $recipient->getName());
}
}

$message->addTo($mailData->getTo()->getEmail(), $mailData->getTo()->getName());
$message->setSubject($mailData->getSubject());

$this->template->subject = $mailData->getSubject();
Expand Down
15 changes: 7 additions & 8 deletions app/Mailing/SrsMailData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
class SrsMailData implements IMessageData
{
/**
* @param Recipient $from Odesilatel mailu.
* @param Recipient[] $recipients Příjemci mailu.
* @param string $subject Předmět mailu.
* @param string $text Text mailu.
* @param Recipient $from Odesilatel mailu.
* @param Recipient $to Příjemce mailu.
* @param string $subject Předmět mailu.
* @param string $text Text mailu.
*/
public function __construct(
private readonly Recipient $from,
private readonly array $recipients,
private readonly Recipient $to,
private readonly string $subject,
private readonly string $text,
) {
Expand All @@ -31,10 +31,9 @@ public function getFrom(): Recipient
return $this->from;
}

/** @return Recipient[] */
public function getRecipients(): array
public function getTo(): Recipient
{
return $this->recipients;
return $this->to;
}

public function getSubject(): string
Expand Down
25 changes: 18 additions & 7 deletions app/Model/Mailing/Commands/Handlers/CreateMailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

namespace App\Model\Mailing\Commands\Handlers;

use App\Model\Acl\Repositories\RoleRepository;
use App\Model\Mailing\Commands\CreateMail;
use App\Model\Mailing\Mail;
use App\Model\Mailing\MailQueue;
use App\Model\Mailing\Recipient;
use App\Model\Mailing\Repositories\MailQueueRepository;
use App\Model\Mailing\Repositories\MailRepository;
use App\Model\Structure\Repositories\SubeventRepository;
use App\Model\User\Repositories\UserRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;

use function array_unique;

use const SORT_REGULAR;

class CreateMailHandler
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly MailRepository $mailRepository,
private readonly MailQueueRepository $mailQueueRepository,
private readonly UserRepository $userRepository,
private readonly RoleRepository $roleRepository,
private readonly SubeventRepository $subeventRepository,
) {
}

Expand All @@ -32,28 +41,30 @@ public function __invoke(CreateMail $command): void
if ($command->getRecipientUsers() !== null) {
$mail->setRecipientUsers($command->getRecipientUsers());
foreach ($command->getRecipientUsers() as $user) {
$recipients[] = $user->getEmail();
$recipients[] = Recipient::createFromUser($user);
}
}

if ($command->getRecipientRoles() !== null) {
$mail->setRecipientRoles($command->getRecipientRoles());
foreach ($command->getRecipientUsers() as $user) { //todo
$recipients[] = $user->getEmail();
$rolesIds = $this->roleRepository->findRolesIds($command->getRecipientRoles());
foreach ($this->userRepository->findAllApprovedInRoles($rolesIds) as $user) {
$recipients[] = Recipient::createFromUser($user);
}
}

if ($command->getRecipientSubevents() !== null) {
$mail->setRecipientSubevents($command->getRecipientSubevents());
foreach ($command->getRecipientUsers() as $user) { //todo
$recipients[] = $user->getEmail();
$subeventsIds = $this->subeventRepository->findSubeventsIds($command->getRecipientSubevents());
foreach ($this->userRepository->findAllWithSubevents($subeventsIds) as $user) {
$recipients[] = Recipient::createFromUser($user);
}
}

if ($command->getRecipientEmails() !== null) {
$mail->setRecipientEmails($command->getRecipientEmails()->toArray());
foreach ($command->getRecipientEmails() as $email) {
$recipients[] = $email;
$recipients[] = new Recipient($email);
}
}

Expand All @@ -64,7 +75,7 @@ public function __invoke(CreateMail $command): void

$this->mailRepository->save($mail);

foreach (array_unique($recipients) as $recipient) {
foreach (array_unique($recipients, SORT_REGULAR) as $recipient) {
$this->mailQueueRepository->save(new MailQueue($recipient, $mail, new DateTimeImmutable()));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Model\Mailing\Commands\CreateTemplateMail;
use App\Model\Mailing\Mail;
use App\Model\Mailing\MailQueue;
use App\Model\Mailing\Recipient;
use App\Model\Mailing\Repositories\MailQueueRepository;
use App\Model\Mailing\Repositories\MailRepository;
use App\Model\Mailing\Repositories\TemplateRepository;
Expand All @@ -18,6 +19,8 @@
use function str_replace;
use function strval;

use const SORT_REGULAR;

class CreateTemplateMailHandler
{
public function __construct(
Expand Down Expand Up @@ -55,14 +58,14 @@ public function __invoke(CreateTemplateMail $command): void
if ($command->getRecipientUsers() !== null) {
$mail->setRecipientUsers($command->getRecipientUsers());
foreach ($command->getRecipientUsers() as $user) {
$recipients[] = $user->getEmail();
$recipients[] = Recipient::createFromUser($user);
}
}

if ($command->getRecipientEmails() !== null) {
$mail->setRecipientEmails($command->getRecipientEmails()->toArray());
foreach ($command->getRecipientEmails() as $email) {
$recipients[] = $email;
$recipients[] = new Recipient($email);
}
}

Expand All @@ -73,7 +76,7 @@ public function __invoke(CreateTemplateMail $command): void

$this->mailRepository->save($mail);

foreach (array_unique($recipients) as $recipient) {
foreach (array_unique($recipients, SORT_REGULAR) as $recipient) {
$this->mailQueueRepository->save(new MailQueue($recipient, $mail, new DateTimeImmutable()));
}
});
Expand Down
51 changes: 51 additions & 0 deletions app/Model/Mailing/Commands/Handlers/SendMailsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands\Handlers;

use App\Mailing\SrsMail;
use App\Mailing\SrsMailData;
use App\Model\Mailing\Commands\SendMails;
use App\Model\Mailing\Recipient;
use App\Model\Mailing\Repositories\MailQueueRepository;
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Services\QueryBus;
use DateTimeImmutable;
use Ublaboo\Mailing\MailFactory;

class SendMailsHandler
{
public function __construct(
private readonly QueryBus $queryBus,
private readonly MailQueueRepository $mailQueueRepository,
private readonly MailFactory $mailFactory,
) {
}

public function __invoke(SendMails $command): void
{
$mailsToSend = $this->mailQueueRepository->findMailsToSend(50);

if ($mailsToSend->isEmpty()) {
return;
}

$from = new Recipient(
$this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_EMAIL)),
$this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)),
);

foreach ($mailsToSend as $mailToSend) {
$to = new Recipient($mailToSend->getRecipientEmail(), $mailToSend->getRecipientName());
$messageData = new SrsMailData($from, $to, $mailToSend->getMail()->getSubject(), $mailToSend->getMail()->getText());
$mail = $this->mailFactory->createByType(SrsMail::class, $messageData);
$mail->send();

$mailToSend->setSent(true);
$mailToSend->setSendDatetime(new DateTimeImmutable());
$this->mailQueueRepository->save($mailToSend);
}
}
}
12 changes: 12 additions & 0 deletions app/Model/Mailing/Commands/SendMails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands;

class SendMails
{
public function __construct()
{
}
}
19 changes: 14 additions & 5 deletions app/Model/Mailing/MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class MailQueue
private int|null $id = null;

#[ORM\Column(type: 'string')]
protected string $recipient;
protected string $recipientEmail;

#[ORM\Column(type: 'string', nullable: true)]
protected string|null $recipientName;

#[ORM\ManyToOne(targetEntity: Mail::class, cascade: ['persist'])]
protected Mail $mail;
Expand All @@ -40,9 +43,10 @@ class MailQueue
#[ORM\Column(type: 'datetime_immutable')]
protected DateTimeImmutable $sendDatetime;

public function __construct(string $recipient, Mail $mail, DateTimeImmutable $enqueueDatetime)
public function __construct(Recipient $recipient, Mail $mail, DateTimeImmutable $enqueueDatetime)
{
$this->recipient = $recipient;
$this->recipientEmail = $recipient->getEmail();
$this->recipientName = $recipient->getName();
$this->mail = $mail;
$this->enqueueDatetime = $enqueueDatetime;
}
Expand All @@ -52,9 +56,14 @@ public function getId(): int|null
return $this->id;
}

public function getRecipient(): string
public function getRecipientEmail(): string
{
return $this->recipientEmail;
}

public function getRecipientName(): string|null
{
return $this->recipient;
return $this->recipientName;
}

public function getMail(): Mail
Expand Down
15 changes: 15 additions & 0 deletions app/Model/Mailing/Repositories/MailQueueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use App\Model\Infrastructure\Repositories\AbstractRepository;
use App\Model\Mailing\MailQueue;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -18,6 +20,19 @@ public function __construct(EntityManagerInterface $em)
parent::__construct($em, MailQueue::class);
}

/** @return Collection<int, MailQueue> */
public function findMailsToSend(int $limit): Collection
{
$result = $this->createQueryBuilder('m')
->where('m.sent = false')
->orderBy('m.enqueueDatetime')
->setMaxResults($limit)
->getQuery()
->getResult();

return new ArrayCollection($result);
}

/**
* Uloží e-mail.
*/
Expand Down
32 changes: 0 additions & 32 deletions app/Services/IMailService.php

This file was deleted.

Loading

0 comments on commit 62d98a7

Please sign in to comment.