Skip to content

Commit

Permalink
mailservice call replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-stanek committed Oct 13, 2023
1 parent 62d98a7 commit 756fbcb
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,15 +106,15 @@ 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,
[
TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)),
TemplateVariable::EMAIL_VERIFICATION_LINK => $link,
],
);
));
}

$contactFormRecipients = array_map(
Expand Down
5 changes: 3 additions & 2 deletions app/AdminModule/Forms/EditUserSeminarFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
]);
]));
}
});
}
Expand Down
25 changes: 6 additions & 19 deletions app/AdminModule/MailingModule/Forms/SendFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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));
}
}
7 changes: 1 addition & 6 deletions app/AdminModule/MailingModule/Presenters/SendPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};

Expand Down
5 changes: 3 additions & 2 deletions app/ApiModule/Presenters/MaturityPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]);
]));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Model/Mailing/Commands/CreateTemplateMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CreateTemplateMail
{
public function __construct(

Check failure on line 12 in app/Model/Mailing/Commands/CreateTemplateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateTemplateMail::__construct() has parameter $parameters with no value type specified in iterable type array.

Check failure on line 12 in app/Model/Mailing/Commands/CreateTemplateMail.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Method App\Model\Mailing\Commands\CreateTemplateMail::__construct() has parameter $recipientEmails with generic interface Doctrine\Common\Collections\Collection but does not specify its types: TKey, T
private readonly Collection|null $recipientUsers,
private readonly array|null $recipientEmails,
private readonly Collection|null $recipientEmails,
private readonly string $template,
private readonly array $parameters,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
],
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
]);
]));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/Presenters/AuthPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
Expand Down
35 changes: 18 additions & 17 deletions app/Services/ApplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -133,7 +134,7 @@ public function register(
new SettingDateValueAsTextQuery(Settings::EDIT_REGISTRATION_TO),
);

$this->mailService->sendMailFromTemplate(
$this->commandBus->handle(new CreateTemplateMail(
new ArrayCollection(
[$user],
),
Expand All @@ -149,7 +150,7 @@ public function register(
new SettingStringValueQuery(Settings::ACCOUNT_NUMBER),
),
],
);
));
}

/**
Expand Down Expand Up @@ -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],
),
Expand All @@ -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()),
],
);
));
}

/**
Expand Down Expand Up @@ -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],
),
Expand All @@ -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],
),
Expand All @@ -326,7 +327,7 @@ public function cancelRegistration(User $user, string $state, User|null $created
),
),
],
);
));
}
}

Expand All @@ -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],
),
Expand All @@ -360,7 +361,7 @@ public function addSubeventsApplication(User $user, Collection $subevents, User
),
TemplateVariable::USERS_SUBEVENTS => $user->getSubeventsText(),
],
);
));
}

/**
Expand Down Expand Up @@ -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()],
),
Expand All @@ -418,7 +419,7 @@ public function updateSubeventsApplication(SubeventsApplication $application, Co
),
TemplateVariable::USERS_SUBEVENTS => $application->getUser()->getSubeventsText(),
],
);
));
}

/**
Expand Down Expand Up @@ -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()],
),
Expand All @@ -473,7 +474,7 @@ public function cancelSubeventsApplication(SubeventsApplication $application, st
),
TemplateVariable::USERS_SUBEVENTS => $application->getUser()->getSubeventsText(),
],
);
));
}

/**
Expand Down Expand Up @@ -517,7 +518,7 @@ public function updateApplicationPayment(
});

if ($paymentDate !== null && $oldPaymentDate === null) {
$this->mailService->sendMailFromTemplate(
$this->commandBus->handle(new CreateTemplateMail(
new ArrayCollection(
[
$application->getUser(),
Expand All @@ -531,7 +532,7 @@ public function updateApplicationPayment(
),
TemplateVariable::APPLICATION_SUBEVENTS => $application->getSubeventsText(),
],
);
));
}
}

Expand Down
Loading

0 comments on commit 756fbcb

Please sign in to comment.