|
|
@@ -6,21 +6,48 @@ namespace App\Message\Handler;
|
|
|
|
|
|
use App\Message\Command\OrganizationCreationCommand;
|
|
|
use App\Service\Organization\OrganizationFactory;
|
|
|
+use Symfony\Component\Mailer\MailerInterface;
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
|
|
+use Symfony\Component\Mime\Address;
|
|
|
+use Symfony\Component\Mime\Email as SymfonyEmail;
|
|
|
|
|
|
#[AsMessageHandler(priority: 1)]
|
|
|
class OrganizationCreationHandler
|
|
|
{
|
|
|
public function __construct(
|
|
|
- private readonly OrganizationFactory $organizationFactory
|
|
|
+ private readonly OrganizationFactory $organizationFactory,
|
|
|
+ private readonly MailerInterface $symfonyMailer,
|
|
|
) {}
|
|
|
|
|
|
public function __invoke(OrganizationCreationCommand $organizationCreationCommand): void
|
|
|
{
|
|
|
$organizationCreationRequest = $organizationCreationCommand->getOrganizationCreationRequest();
|
|
|
+ $error = null;
|
|
|
|
|
|
- $organization = $this->organizationFactory->create($organizationCreationRequest);
|
|
|
+ try {
|
|
|
+ $organization = $this->organizationFactory->create($organizationCreationRequest);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $error = $e->getMessage();
|
|
|
+ }
|
|
|
|
|
|
- // TODO: send confirmation email
|
|
|
+ if ($organizationCreationRequest->getSendConfirmationEmailAt() !== null) {
|
|
|
+
|
|
|
+ if ($error !== null) {
|
|
|
+ $subject = 'Organization creation : an error occured';
|
|
|
+ $message = 'An error occured while creating the new organization : \n' . $error;
|
|
|
+ } else {
|
|
|
+ $subject = 'New organization created';
|
|
|
+ $message = 'The organization "' . $organization->getName() . '" has been created successfully.';
|
|
|
+ }
|
|
|
+
|
|
|
+ $symfonyMail = (new SymfonyEmail())
|
|
|
+ ->from('mail.report@opentalent.fr')
|
|
|
+ ->replyTo('mail.report@opentalent.fr')
|
|
|
+ ->returnPath(Address::create('mail.report@opentalent.fr'))
|
|
|
+ ->to($organizationCreationRequest->getSendConfirmationEmailAt())
|
|
|
+ ->subject($subject)
|
|
|
+ ->text($message);
|
|
|
+ $this->symfonyMailer->send($symfonyMail);
|
|
|
+ }
|
|
|
}
|
|
|
}
|