OrganizationDeletionHandler.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Message\Handler;
  4. use App\Message\Message\OrganizationDeletion;
  5. use App\Service\Organization\OrganizationFactory;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use Symfony\Component\Messenger\Attribute\AsMessageHandler;
  8. use Symfony\Component\Mime\Address;
  9. use Symfony\Component\Mime\Email as SymfonyEmail;
  10. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  11. #[AsMessageHandler(priority: 1)]
  12. class OrganizationDeletionHandler
  13. {
  14. public function __construct(
  15. private readonly OrganizationFactory $organizationFactory,
  16. private readonly MailerInterface $symfonyMailer,
  17. private readonly string $opentalentMailReport,
  18. ) {
  19. }
  20. /**
  21. * @throws \Throwable
  22. * @throws TransportExceptionInterface
  23. * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  24. */
  25. public function __invoke(OrganizationDeletion $organizationDeletionCommand): void
  26. {
  27. $organizationCreationRequest = $organizationDeletionCommand->getOrganizationDeletionRequest();
  28. $mail = ['subject' => '', 'content' => ''];
  29. try {
  30. $this->organizationFactory->delete($organizationCreationRequest);
  31. $mail['subject'] = 'Organization deleted';
  32. $mail['content'] = 'The organization n° '.$organizationCreationRequest->getOrganizationId().' has been deleted successfully.';
  33. } catch (\Exception $e) {
  34. $mail['subject'] = 'Organization deletion : an error occured';
  35. $mail['content'] = 'An error occured while deleting the new organization : \n'.$e->getMessage();
  36. throw $e;
  37. } finally {
  38. if ($organizationCreationRequest->getSendConfirmationEmailAt() !== null) {
  39. $symfonyMail = (new SymfonyEmail())
  40. ->from($this->opentalentMailReport)
  41. ->replyTo($this->opentalentMailReport)
  42. ->returnPath(Address::create($this->opentalentMailReport))
  43. ->to($organizationCreationRequest->getSendConfirmationEmailAt())
  44. ->subject($mail['subject'])
  45. ->text($mail['content']);
  46. $this->symfonyMailer->send($symfonyMail);
  47. }
  48. }
  49. }
  50. }