ContactRequestProcessor.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Processor;
  4. use ApiPlatform\Metadata\Operation;
  5. use ApiPlatform\Metadata\Post;
  6. use ApiPlatform\State\ProcessorInterface;
  7. use App\ApiResource\ContactRequest;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Email;
  12. class ContactRequestProcessor implements ProcessorInterface
  13. {
  14. public function __construct(
  15. private readonly MailerInterface $symfonyMailer,
  16. private readonly string $fromEmail,
  17. private readonly string $contactEmail,
  18. )
  19. {}
  20. /**
  21. * @throws TransportExceptionInterface
  22. */
  23. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ContactRequest
  24. {
  25. if (!$operation instanceof Post) {
  26. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  27. }
  28. /** @var ContactRequest $contactRequest */
  29. $contactRequest = $data;
  30. $symfonyMail = (new Email())
  31. ->to($this->contactEmail)
  32. ->from($this->fromEmail)
  33. ->subject('Contact from cv.ogene.fr')
  34. ->text(
  35. 'From : ' . $contactRequest->getEmail() . "\n" .
  36. 'Name: ' . $contactRequest->getName() ?? '-' . "\n\n" .
  37. $contactRequest->getMessage()
  38. );
  39. $this->symfonyMailer->send($symfonyMail);
  40. return $contactRequest;
  41. }
  42. }