OrganizationDeletionHandler.php 2.3 KB

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