-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d4ee55
commit 95122b8
Showing
17 changed files
with
385 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / PHPStan analysis
Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php GitHub Actions / PHPStan analysis
Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php GitHub Actions / PHPStan analysis
Check failure on line 11 in app/Model/Mailing/Commands/CreateMail.php GitHub Actions / PHPStan analysis
|
||
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 GitHub Actions / PHPStan analysis
|
||
{ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
app/Model/Mailing/Commands/Handlers/CreateTemplateMailHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.