diff --git a/app/AdminModule/ConfigurationModule/Forms/MailingFormFactory.php b/app/AdminModule/ConfigurationModule/Forms/MailingFormFactory.php index bb272b0f3..1dd2dfddb 100644 --- a/app/AdminModule/ConfigurationModule/Forms/MailingFormFactory.php +++ b/app/AdminModule/ConfigurationModule/Forms/MailingFormFactory.php @@ -5,6 +5,7 @@ namespace App\AdminModule\ConfigurationModule\Forms; use App\AdminModule\Forms\BaseFormFactory; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Commands\SetSettingArrayValue; @@ -105,7 +106,7 @@ public function processForm(Form $form, stdClass $values): void $link = $this->linkGenerator->link('Api:Mail:verify', ['code' => $verificationCode]); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( null, new ArrayCollection([$values->seminarEmail]), Template::EMAIL_VERIFICATION, @@ -113,7 +114,7 @@ public function processForm(Form $form, stdClass $values): void TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::EMAIL_VERIFICATION_LINK => $link, ], - ); + )); } $contactFormRecipients = array_map( diff --git a/app/AdminModule/Forms/EditUserSeminarFormFactory.php b/app/AdminModule/Forms/EditUserSeminarFormFactory.php index 49e4c9941..faaa54097 100644 --- a/app/AdminModule/Forms/EditUserSeminarFormFactory.php +++ b/app/AdminModule/Forms/EditUserSeminarFormFactory.php @@ -22,6 +22,7 @@ use App\Model\CustomInput\CustomTextValue; use App\Model\CustomInput\Repositories\CustomInputRepository; use App\Model\CustomInput\Repositories\CustomInputValueRepository; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Queries\SettingStringValueQuery; @@ -336,10 +337,10 @@ public function processForm(Form $form, stdClass $values): void if ($customInputValueChanged) { assert($this->user instanceof User); - $this->mailService->sendMailFromTemplate(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::USER => $this->user->getDisplayName(), - ]); + ])); } }); } diff --git a/app/AdminModule/MailingModule/Forms/SendFormFactory.php b/app/AdminModule/MailingModule/Forms/SendFormFactory.php index 6b40939cd..761dfc3a6 100644 --- a/app/AdminModule/MailingModule/Forms/SendFormFactory.php +++ b/app/AdminModule/MailingModule/Forms/SendFormFactory.php @@ -7,6 +7,7 @@ use App\AdminModule\Forms\BaseFormFactory; use App\Model\Acl\Repositories\RoleRepository; use App\Model\Acl\Role; +use App\Model\Mailing\Commands\CreateMail; use App\Model\Structure\Repositories\SubeventRepository; use App\Model\User\Repositories\UserRepository; use App\Services\AclService; @@ -15,11 +16,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Nette; use Nette\Application\UI\Form; -use Nette\Mail\SendException; use stdClass; use Throwable; -use Tracy\Debugger; -use Tracy\ILogger; /** * Formulář pro vytvoření e-mailu. @@ -28,11 +26,6 @@ class SendFormFactory { use Nette\SmartObject; - /** - * Stav odeslání e-mailu. - */ - public bool $mailSuccess; - public function __construct( private readonly BaseFormFactory $baseFormFactory, private readonly CommandBus $commandBus, @@ -111,20 +104,14 @@ public function create(): Form */ public function processForm(Form $form, stdClass $values): void { - try { - $recipientsRoles = $this->roleRepository->findRolesByIds($values->recipientRoles); + $recipientsRoles = $this->roleRepository->findRolesByIds($values->recipientRoles); $recipientsSubevents = $this->subeventRepository->findSubeventsByIds($values->recipientSubevents); $recipientsUsers = $this->userRepository->findUsersByIds($values->recipientUsers); $recipientsEmails = new ArrayCollection(); - if (! empty($values->copy)) { - $recipientsEmails->add($values->copy); - } - - $this->mailService->sendMail($recipientsRoles, $recipientsSubevents, $recipientsUsers, $recipientsEmails, $values->subject, $values->text); - $this->mailSuccess = true; - } catch (SendException $ex) { - Debugger::log($ex, ILogger::WARNING); - $this->mailSuccess = false; + if (! empty($values->copy)) { + $recipientsEmails->add($values->copy); } + + $this->commandBus->handle(new CreateMail($recipientsRoles, $recipientsSubevents, $recipientsUsers, $recipientsEmails, $values->subject, $values->text)); } } diff --git a/app/AdminModule/MailingModule/Presenters/SendPresenter.php b/app/AdminModule/MailingModule/Presenters/SendPresenter.php index 8d0dc0d27..e77c75bea 100644 --- a/app/AdminModule/MailingModule/Presenters/SendPresenter.php +++ b/app/AdminModule/MailingModule/Presenters/SendPresenter.php @@ -22,12 +22,7 @@ protected function createComponentSendForm(): Form $form = $this->sendFormFactory->create(); $form->onSuccess[] = function (Form $form, stdClass $values): void { - if ($this->sendFormFactory->mailSuccess) { - $this->flashMessage('admin.mailing.send.sent', 'success'); - } else { - $this->flashMessage('admin.mailing.send.error', 'danger'); - } - + $this->flashMessage('admin.mailing.send.sent', 'success'); $this->redirect('this'); }; diff --git a/app/ApiModule/Presenters/MaturityPresenter.php b/app/ApiModule/Presenters/MaturityPresenter.php index 0c9c23065..eca698097 100644 --- a/app/ApiModule/Presenters/MaturityPresenter.php +++ b/app/ApiModule/Presenters/MaturityPresenter.php @@ -7,6 +7,7 @@ use App\Model\Acl\Repositories\RoleRepository; use App\Model\Acl\Role; use App\Model\Enums\ApplicationState; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Queries\SettingIntValueQuery; @@ -120,10 +121,10 @@ public function actionSendReminders(): void $maturityDate = $application->getMaturityDate(); if ($maturityReminderDate == $maturityDate) { - $this->mailService->sendMailFromTemplate(new ArrayCollection([$application->getUser()]), null, Template::MATURITY_REMINDER, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$application->getUser()]), null, Template::MATURITY_REMINDER, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::APPLICATION_MATURITY => $maturityDate->format(Helpers::DATE_FORMAT), - ]); + ])); } } } diff --git a/app/Model/Mailing/Commands/CreateTemplateMail.php b/app/Model/Mailing/Commands/CreateTemplateMail.php index e7ce74cf9..e4f186b41 100644 --- a/app/Model/Mailing/Commands/CreateTemplateMail.php +++ b/app/Model/Mailing/Commands/CreateTemplateMail.php @@ -11,7 +11,7 @@ class CreateTemplateMail { public function __construct( private readonly Collection|null $recipientUsers, - private readonly array|null $recipientEmails, + private readonly Collection|null $recipientEmails, private readonly string $template, private readonly array $parameters, ) { diff --git a/app/Model/User/Events/Subscribers/ProgramRegisteredEventListener.php b/app/Model/User/Events/Subscribers/ProgramRegisteredEventListener.php index 435c8cc6e..f87a3e56d 100644 --- a/app/Model/User/Events/Subscribers/ProgramRegisteredEventListener.php +++ b/app/Model/User/Events/Subscribers/ProgramRegisteredEventListener.php @@ -4,6 +4,7 @@ namespace App\Model\User\Events\Subscribers; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Queries\SettingStringValueQuery; @@ -23,10 +24,15 @@ public function __construct(private readonly CommandBus $commandBus, private rea public function __invoke(ProgramRegisteredEvent $event): void { if (! $event->isAlternate() && $event->isNotifyUser()) { - $this->mailService->sendMailFromTemplate(new ArrayCollection([$event->getUser()]), null, Template::PROGRAM_REGISTERED, [ - TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), - TemplateVariable::PROGRAM_NAME => $event->getProgram()->getBlock()->getName(), - ]); + $this->commandBus->handle(new CreateTemplateMail( + new ArrayCollection([$event->getUser()]), + null, + Template::PROGRAM_REGISTERED, + [ + TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), + TemplateVariable::PROGRAM_NAME => $event->getProgram()->getBlock()->getName(), + ], + )); } } } diff --git a/app/Model/User/Events/Subscribers/ProgramUnregisteredEventListener.php b/app/Model/User/Events/Subscribers/ProgramUnregisteredEventListener.php index 5142c95d1..06140e139 100644 --- a/app/Model/User/Events/Subscribers/ProgramUnregisteredEventListener.php +++ b/app/Model/User/Events/Subscribers/ProgramUnregisteredEventListener.php @@ -4,6 +4,7 @@ namespace App\Model\User\Events\Subscribers; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Queries\SettingStringValueQuery; @@ -35,10 +36,10 @@ public function __invoke(ProgramUnregisteredEvent $event): void } if ($event->isNotifyUser()) { - $this->mailService->sendMailFromTemplate(new ArrayCollection([$event->getUser()]), null, Template::PROGRAM_UNREGISTERED, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$event->getUser()]), null, Template::PROGRAM_UNREGISTERED, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::PROGRAM_NAME => $event->getProgram()->getBlock()->getName(), - ]); + ])); } } } diff --git a/app/Presenters/AuthPresenter.php b/app/Presenters/AuthPresenter.php index a7fac93bf..449c587bc 100644 --- a/app/Presenters/AuthPresenter.php +++ b/app/Presenters/AuthPresenter.php @@ -4,6 +4,7 @@ namespace App\Presenters; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Exceptions\SettingsItemNotFoundException; @@ -68,9 +69,9 @@ public function actionLogin(string $backlink = ''): void $user = $this->userRepository->findById($this->user->id); assert($user instanceof User); - $this->mailService->sendMailFromTemplate(new ArrayCollection([$user]), null, Template::SIGN_IN, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$user]), null, Template::SIGN_IN, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), - ]); + ])); } $this->redirectAfterLogin($this->getParameter('ReturnUrl')); diff --git a/app/Services/ApplicationService.php b/app/Services/ApplicationService.php index 2e295897b..e38ab730e 100644 --- a/app/Services/ApplicationService.php +++ b/app/Services/ApplicationService.php @@ -19,6 +19,7 @@ use App\Model\Enums\MaturityType; use App\Model\Enums\PaymentState; use App\Model\Enums\PaymentType; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Payment\Payment; @@ -63,6 +64,7 @@ class ApplicationService use Nette\SmartObject; public function __construct( + private readonly CommandBus $commandBus, private readonly QueryBus $queryBus, private readonly EntityManagerInterface $em, private readonly ApplicationRepository $applicationRepository, @@ -72,7 +74,6 @@ public function __construct( private readonly SubeventRepository $subeventRepository, private readonly DiscountService $discountService, private readonly VariableSymbolRepository $variableSymbolRepository, - private readonly MailService $mailService, private readonly UserService $userService, private readonly Translator $translator, private readonly PaymentRepository $paymentRepository, @@ -133,7 +134,7 @@ public function register( new SettingDateValueAsTextQuery(Settings::EDIT_REGISTRATION_TO), ); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$user], ), @@ -149,7 +150,7 @@ public function register( new SettingStringValueQuery(Settings::ACCOUNT_NUMBER), ), ], - ); + )); } /** @@ -238,7 +239,7 @@ public function updateRoles(User $user, Collection $roles, User|null $createdBy, $this->updateUserPaymentInfo($user); }); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$user], ), @@ -248,7 +249,7 @@ public function updateRoles(User $user, Collection $roles, User|null $createdBy, TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::USERS_ROLES => implode(', ', $roles->map(static fn (Role $role) => $role->getName())->toArray()), ], - ); + )); } /** @@ -300,7 +301,7 @@ public function cancelRegistration(User $user, string $state, User|null $created }); if ($state === ApplicationState::CANCELED) { - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$user], ), @@ -311,9 +312,9 @@ public function cancelRegistration(User $user, string $state, User|null $created new SettingStringValueQuery(Settings::SEMINAR_NAME), ), ], - ); + )); } elseif ($state === ApplicationState::CANCELED_NOT_PAID) { - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$user], ), @@ -326,7 +327,7 @@ public function cancelRegistration(User $user, string $state, User|null $created ), ), ], - ); + )); } } @@ -348,7 +349,7 @@ public function addSubeventsApplication(User $user, Collection $subevents, User $this->updateUserPaymentInfo($user); }); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$user], ), @@ -360,7 +361,7 @@ public function addSubeventsApplication(User $user, Collection $subevents, User ), TemplateVariable::USERS_SUBEVENTS => $user->getSubeventsText(), ], - ); + )); } /** @@ -406,7 +407,7 @@ public function updateSubeventsApplication(SubeventsApplication $application, Co $this->decrementSubeventsOccupancy($application->getSubevents()); }); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$application->getUser()], ), @@ -418,7 +419,7 @@ public function updateSubeventsApplication(SubeventsApplication $application, Co ), TemplateVariable::USERS_SUBEVENTS => $application->getUser()->getSubeventsText(), ], - ); + )); } /** @@ -461,7 +462,7 @@ public function cancelSubeventsApplication(SubeventsApplication $application, st $this->decrementSubeventsOccupancy($application->getSubevents()); }); - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [$application->getUser()], ), @@ -473,7 +474,7 @@ public function cancelSubeventsApplication(SubeventsApplication $application, st ), TemplateVariable::USERS_SUBEVENTS => $application->getUser()->getSubeventsText(), ], - ); + )); } /** @@ -517,7 +518,7 @@ public function updateApplicationPayment( }); if ($paymentDate !== null && $oldPaymentDate === null) { - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( new ArrayCollection( [ $application->getUser(), @@ -531,7 +532,7 @@ public function updateApplicationPayment( ), TemplateVariable::APPLICATION_SUBEVENTS => $application->getSubeventsText(), ], - ); + )); } } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 057666b2c..4cd37bd45 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -5,6 +5,7 @@ namespace App\Services; use App\Model\Enums\PaymentType; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Queries\SettingStringValueQuery; @@ -25,11 +26,11 @@ class UserService use Nette\SmartObject; public function __construct( + private readonly CommandBus $commandBus, private readonly QueryBus $queryBus, private readonly EventBus $eventBus, private readonly Translator $translator, private readonly UserRepository $userRepository, - private readonly MailService $mailService, private readonly EntityManagerInterface $em, ) { } @@ -92,9 +93,9 @@ public function setApproved(User $user, bool $approved): void $this->eventBus->handle(new UserUpdatedEvent($user, $approvedOld)); if ($approved) { - $this->mailService->sendMailFromTemplate(new ArrayCollection([$user]), null, Template::REGISTRATION_APPROVED, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$user]), null, Template::REGISTRATION_APPROVED, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), - ]); + ])); } }); } diff --git a/app/WebModule/Forms/AdditionalInformationFormFactory.php b/app/WebModule/Forms/AdditionalInformationFormFactory.php index f0a8e2420..b5448068c 100644 --- a/app/WebModule/Forms/AdditionalInformationFormFactory.php +++ b/app/WebModule/Forms/AdditionalInformationFormFactory.php @@ -20,6 +20,7 @@ use App\Model\CustomInput\CustomTextValue; use App\Model\CustomInput\Repositories\CustomInputRepository; use App\Model\CustomInput\Repositories\CustomInputValueRepository; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Exceptions\SettingsItemNotFoundException; @@ -279,10 +280,10 @@ public function processForm(Form $form, stdClass $values): void if ($customInputValueChanged) { assert($this->user instanceof User); - $this->mailService->sendMailFromTemplate(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [ + $this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [ TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)), TemplateVariable::USER => $this->user->getDisplayName(), - ]); + ])); } }); } diff --git a/app/WebModule/Forms/ContactForm.php b/app/WebModule/Forms/ContactForm.php index 09ee4654b..cffa1da1b 100644 --- a/app/WebModule/Forms/ContactForm.php +++ b/app/WebModule/Forms/ContactForm.php @@ -4,6 +4,7 @@ namespace App\WebModule\Forms; +use App\Model\Mailing\Commands\CreateTemplateMail; use App\Model\Mailing\Template; use App\Model\Mailing\TemplateVariable; use App\Model\Settings\Exceptions\SettingsItemNotFoundException; @@ -136,7 +137,7 @@ public function processForm(Form $form, stdClass $values): void $recipientsEmails->add($recipient); } - $this->mailService->sendMailFromTemplate( + $this->commandBus->handle(new CreateTemplateMail( $recipientsUsers, $recipientsEmails, Template::CONTACT_FORM, @@ -146,7 +147,7 @@ public function processForm(Form $form, stdClass $values): void TemplateVariable::SENDER_EMAIL => $senderEmail, TemplateVariable::MESSAGE => str_replace(["\n", "\r"], '', nl2br($values->message, false)), ], - ); + )); $this->onSave(); } diff --git a/tests/integration/Services/stubs/MailServiceStub.php b/tests/integration/Services/stubs/MailServiceStub.php deleted file mode 100644 index 4abeccd92..000000000 --- a/tests/integration/Services/stubs/MailServiceStub.php +++ /dev/null @@ -1,19 +0,0 @@ -