Skip to content

Commit

Permalink
create mail commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-stanek committed Oct 11, 2023
1 parent 5d4ee55 commit 95122b8
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Services\CommandBus;
use App\Services\IMailService;
use App\Services\QueryBus;
use App\Utils\Validators;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -48,7 +47,6 @@ public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly CommandBus $commandBus,
private readonly QueryBus $queryBus,
private readonly IMailService $mailService,
private readonly LinkGenerator $linkGenerator,
private readonly Validators $validators,
) {
Expand Down
4 changes: 2 additions & 2 deletions app/AdminModule/Forms/EditUserSeminarFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
use App\Model\User\User;
use App\Services\AclService;
use App\Services\ApplicationService;
use App\Services\CommandBus;
use App\Services\FilesService;
use App\Services\IMailService;
use App\Services\QueryBus;
use App\Services\UserService;
use App\Utils\Helpers;
Expand Down Expand Up @@ -71,6 +71,7 @@ class EditUserSeminarFormFactory

public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly CommandBus $commandBus,
private readonly QueryBus $queryBus,
private readonly EntityManagerInterface $em,
private readonly UserRepository $userRepository,
Expand All @@ -80,7 +81,6 @@ public function __construct(
private readonly ApplicationService $applicationService,
private readonly Validators $validators,
private readonly FilesService $filesService,
private readonly IMailService $mailService,
private readonly AclService $aclService,
private readonly UserService $userService,
) {
Expand Down
4 changes: 2 additions & 2 deletions app/AdminModule/MailingModule/Forms/SendFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use App\Model\Structure\Repositories\SubeventRepository;
use App\Model\User\Repositories\UserRepository;
use App\Services\AclService;
use App\Services\IMailService;
use App\Services\CommandBus;
use App\Services\SubeventService;
use Doctrine\Common\Collections\ArrayCollection;
use Nette;
Expand All @@ -35,7 +35,7 @@ class SendFormFactory

public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly IMailService $mailService,
private readonly CommandBus $commandBus,

Check failure on line 38 in app/AdminModule/MailingModule/Forms/SendFormFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Property App\AdminModule\MailingModule\Forms\SendFormFactory::$commandBus is never read, only written.
private readonly RoleRepository $roleRepository,
private readonly UserRepository $userRepository,
private readonly SubeventRepository $subeventRepository,
Expand Down
8 changes: 4 additions & 4 deletions app/ApiModule/Presenters/MaturityPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use App\Model\Settings\Settings;
use App\Model\User\Repositories\UserRepository;
use App\Services\ApplicationService;
use App\Services\IMailService;
use App\Services\CommandBus;
use App\Services\QueryBus;
use App\Utils\Helpers;
use DateTimeImmutable;
Expand All @@ -29,6 +29,9 @@
*/
class MaturityPresenter extends ApiBasePresenter
{
#[Inject]
public CommandBus $commandBus;

#[Inject]
public QueryBus $queryBus;

Expand All @@ -41,9 +44,6 @@ class MaturityPresenter extends ApiBasePresenter
#[Inject]
public RoleRepository $roleRepository;

#[Inject]
public IMailService $mailService;

#[Inject]
public ApplicationService $applicationService;

Expand Down
56 changes: 56 additions & 0 deletions app/Model/Mailing/Commands/CreateMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands;

use Doctrine\Common\Collections\Collection;

class CreateMail
{
public function __construct(

Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateMail::__construct() has parameter $recipientEmails with generic interface Doctrine\Common\Collections\Collection but does not specify its types: TKey, T

Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateMail::__construct() has parameter $recipientRoles with generic interface Doctrine\Common\Collections\Collection but does not specify its types: TKey, T

Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateMail::__construct() has parameter $recipientSubevents with generic interface Doctrine\Common\Collections\Collection but does not specify its types: TKey, T

Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateMail::__construct() has parameter $recipientUsers with generic interface Doctrine\Common\Collections\Collection but does not specify its types: TKey, T
private readonly Collection|null $recipientRoles,
private readonly Collection|null $recipientSubevents,
private readonly Collection|null $recipientUsers,
private readonly Collection|null $recipientEmails,
private readonly string $subject,
private readonly string $text,
private readonly bool $automatic = false,
) {
}

public function getRecipientRoles(): Collection|null

Check failure on line 22 in app/Model/Mailing/Commands/CreateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateMail::getRecipientRoles() return type with generic interface Doctrine\Common\Collections\Collection does not specify its types: TKey, T
{
return $this->recipientRoles;
}

public function getRecipientSubevents(): Collection|null
{
return $this->recipientSubevents;
}

public function getRecipientUsers(): Collection|null
{
return $this->recipientUsers;
}

public function getRecipientEmails(): Collection|null
{
return $this->recipientEmails;
}

public function getSubject(): string
{
return $this->subject;
}

public function getText(): string
{
return $this->text;
}

public function isAutomatic(): bool
{
return $this->automatic;
}
}
41 changes: 41 additions & 0 deletions app/Model/Mailing/Commands/CreateTemplateMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands;

use App\Model\User\User;
use Doctrine\Common\Collections\Collection;

class CreateTemplateMail
{
public function __construct(
private readonly Collection|null $recipientUsers,
private readonly array|null $recipientEmails,
private readonly string $template,
private readonly array $parameters,
) {
}

/** @return User[]|null */
public function getRecipientUsers(): Collection|null
{
return $this->recipientUsers;
}

/** @return string[]|null */
public function getRecipientEmails(): Collection|null
{
return $this->recipientEmails;
}

public function getTemplate(): string
{
return $this->template;
}

public function getParameters(): array
{
return $this->parameters;
}
}
72 changes: 72 additions & 0 deletions app/Model/Mailing/Commands/Handlers/CreateMailHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands\Handlers;

use App\Model\Mailing\Commands\CreateMail;
use App\Model\Mailing\Mail;
use App\Model\Mailing\MailQueue;
use App\Model\Mailing\Repositories\MailQueueRepository;
use App\Model\Mailing\Repositories\MailRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;

use function array_unique;

class CreateMailHandler
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly MailRepository $mailRepository,
private readonly MailQueueRepository $mailQueueRepository,
) {
}

public function __invoke(CreateMail $command): void
{
$this->em->wrapInTransaction(function () use ($command): void {
$mail = new Mail();
$recipients = [];

if ($command->getRecipientUsers() !== null) {
$mail->setRecipientUsers($command->getRecipientUsers());
foreach ($command->getRecipientUsers() as $user) {
$recipients[] = $user->getEmail();
}
}

if ($command->getRecipientRoles() !== null) {
$mail->setRecipientRoles($command->getRecipientRoles());
foreach ($command->getRecipientUsers() as $user) { //todo
$recipients[] = $user->getEmail();
}
}

if ($command->getRecipientSubevents() !== null) {
$mail->setRecipientSubevents($command->getRecipientSubevents());
foreach ($command->getRecipientUsers() as $user) { //todo
$recipients[] = $user->getEmail();
}
}

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

$mail->setSubject($command->getSubject());
$mail->setText($command->getText());
$mail->setDatetime(new DateTimeImmutable());
$mail->setAutomatic($command->isAutomatic());

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

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

declare(strict_types=1);

namespace App\Model\Mailing\Commands\Handlers;

use App\Model\Mailing\Commands\CreateTemplateMail;
use App\Model\Mailing\Mail;
use App\Model\Mailing\MailQueue;
use App\Model\Mailing\Repositories\MailQueueRepository;
use App\Model\Mailing\Repositories\MailRepository;
use App\Model\Mailing\Repositories\TemplateRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Translation\Translator;

use function array_unique;
use function str_replace;
use function strval;

class CreateTemplateMailHandler
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly MailRepository $mailRepository,
private readonly MailQueueRepository $mailQueueRepository,
private readonly TemplateRepository $templateRepository,
private readonly Translator $translator,
) {
}

public function __invoke(CreateTemplateMail $command): void
{
$this->em->wrapInTransaction(function () use ($command): void {
$template = $this->templateRepository->findByType($command->getTemplate());

if (! $template->isActive()) {
return;
}

$subject = $template->getSubject();
$text = $template->getText();

foreach ($template->getVariables() as $variable) {
$variableName = '%' . $this->translator->translate('common.mailing.variable_name.' . $variable->getName()) . '%';
$value = $command->getParameters()[$variable->getName()];

$subject = str_replace($variableName, strval($value), $subject);
$text = str_replace($variableName, strval($value), $text);
}

$mail = new Mail();
$recipients = [];

if ($command->getRecipientUsers() !== null) {
$mail->setRecipientUsers($command->getRecipientUsers());
foreach ($command->getRecipientUsers() as $user) {
$recipients[] = $user->getEmail();
}
}

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

$mail->setSubject($subject);
$mail->setText($text);
$mail->setDatetime(new DateTimeImmutable());
$mail->setAutomatic(true);

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

foreach (array_unique($recipients) as $recipient) {
$this->mailQueueRepository->save(new MailQueue($recipient, $mail, new DateTimeImmutable()));
}
});
}
}
Loading

0 comments on commit 95122b8

Please sign in to comment.