|
|
@@ -6,52 +6,64 @@ namespace App\Message\Handler;
|
|
|
|
|
|
use App\Message\Message\OrganizationDeletion;
|
|
|
use App\Service\Organization\OrganizationFactory;
|
|
|
+use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
|
|
use Symfony\Component\Mime\Address;
|
|
|
use Symfony\Component\Mime\Email as SymfonyEmail;
|
|
|
-use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
|
|
|
#[AsMessageHandler(priority: 1)]
|
|
|
-class OrganizationDeletionHandler
|
|
|
+readonly class OrganizationDeletionHandler
|
|
|
{
|
|
|
public function __construct(
|
|
|
- private readonly OrganizationFactory $organizationFactory,
|
|
|
- private readonly MailerInterface $symfonyMailer,
|
|
|
- private readonly string $opentalentMailReport,
|
|
|
+ private OrganizationFactory $organizationFactory,
|
|
|
+ private MailerInterface $symfonyMailer,
|
|
|
+ private string $opentalentMailReport,
|
|
|
) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @throws \Throwable
|
|
|
* @throws TransportExceptionInterface
|
|
|
- * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
|
|
*/
|
|
|
public function __invoke(OrganizationDeletion $organizationDeletionCommand): void
|
|
|
{
|
|
|
$organizationCreationRequest = $organizationDeletionCommand->getOrganizationDeletionRequest();
|
|
|
- $mail = ['subject' => '', 'content' => ''];
|
|
|
|
|
|
try {
|
|
|
$this->organizationFactory->delete($organizationCreationRequest);
|
|
|
|
|
|
- $mail['subject'] = 'Organization deleted';
|
|
|
- $mail['content'] = 'The organization n° '.$organizationCreationRequest->getOrganizationId().' has been deleted successfully.';
|
|
|
+ $this->sendMail(
|
|
|
+ $organizationCreationRequest->getSendConfirmationEmailAt() ?? $this->opentalentMailReport,
|
|
|
+ 'Organization deleted',
|
|
|
+ 'The organization n° '.$organizationCreationRequest->getOrganizationId().' has been deleted successfully.',
|
|
|
+ );
|
|
|
} catch (\Exception $e) {
|
|
|
- $mail['subject'] = 'Organization deletion : an error occured';
|
|
|
- $mail['content'] = 'An error occured while deleting the new organization : \n'.$e->getMessage();
|
|
|
+ $this->sendMail(
|
|
|
+ $organizationCreationRequest->getSendConfirmationEmailAt() ?? $this->opentalentMailReport,
|
|
|
+ 'Organization deletion : an error occurred',
|
|
|
+ 'An error occurred while deleting the new organization : \n'.$e->getMessage()
|
|
|
+ );
|
|
|
throw $e;
|
|
|
- } finally {
|
|
|
- if ($organizationCreationRequest->getSendConfirmationEmailAt() !== null) {
|
|
|
- $symfonyMail = (new SymfonyEmail())
|
|
|
- ->from($this->opentalentMailReport)
|
|
|
- ->replyTo($this->opentalentMailReport)
|
|
|
- ->returnPath(Address::create($this->opentalentMailReport))
|
|
|
- ->to($organizationCreationRequest->getSendConfirmationEmailAt())
|
|
|
- ->subject($mail['subject'])
|
|
|
- ->text($mail['content']);
|
|
|
- $this->symfonyMailer->send($symfonyMail);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws TransportExceptionInterface
|
|
|
+ */
|
|
|
+ private function sendMail(
|
|
|
+ string $to,
|
|
|
+ string $subject,
|
|
|
+ string $content,
|
|
|
+ ): void
|
|
|
+ {
|
|
|
+ $symfonyMail = (new SymfonyEmail())
|
|
|
+ ->from($this->opentalentMailReport)
|
|
|
+ ->replyTo($this->opentalentMailReport)
|
|
|
+ ->returnPath(Address::create($this->opentalentMailReport))
|
|
|
+ ->to($to)
|
|
|
+ ->subject($subject)
|
|
|
+ ->text($content);
|
|
|
+ $this->symfonyMailer->send($symfonyMail);
|
|
|
+ }
|
|
|
}
|