| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- declare(strict_types=1);
- namespace App\State\Processor;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\Metadata\Post;
- use ApiPlatform\State\ProcessorInterface;
- use App\ApiResource\ContactRequest;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
- use Symfony\Component\Mailer\MailerInterface;
- use Symfony\Component\Mime\Email;
- class ContactRequestProcessor implements ProcessorInterface
- {
- public function __construct(
- private readonly MailerInterface $symfonyMailer,
- private readonly string $fromEmail,
- private readonly string $contactEmail,
- )
- {}
- /**
- * @throws TransportExceptionInterface
- */
- public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ContactRequest
- {
- if (!$operation instanceof Post) {
- throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- /** @var ContactRequest $contactRequest */
- $contactRequest = $data;
- $symfonyMail = (new Email())
- ->to($this->contactEmail)
- ->from($this->fromEmail)
- ->subject('Contact from cv.ogene.fr')
- ->text(
- 'From : ' . $contactRequest->getEmail() . "\n" .
- 'Name: ' . $contactRequest->getName() ?? '-' . "\n\n" .
- $contactRequest->getMessage()
- );
- $this->symfonyMailer->send($symfonyMail);
- return $contactRequest;
- }
- }
|