src/Controller/Email/EmailWebhooksController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Email;
  3. use App\Message\EmailWebhookQueued;
  4. use App\Model\Email\Webhook\Webhook;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\IpUtils;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. /**
  16.  * @see https://developers.brevo.com/docs/how-to-use-webhooks
  17.  * @see https://help.brevo.com/hc/en-us/articles/27824932835474-New-webhooks-Create-outbound-webhooks-to-send-real-time-data-from-Brevo-to-an-external-app
  18.  * @see https://developers.brevo.com/docs/transactional-webhooks
  19.  * @see https://developers.brevo.com/docs/send-a-transactional-email#tracking-your-transactional-activity-through-webhooks
  20.  * @see https://developers.brevo.com/reference/sendtransacemail
  21.  *
  22.  * @Route("/email/webhooks")
  23.  */
  24. class EmailWebhooksController extends AbstractController
  25. {
  26.     /**
  27.      * @Route("/", name="email_webhooks", methods={"GET", "POST"})
  28.      */
  29.     public function webhooks(
  30.         Request $request,
  31.         SerializerInterface $serializer,
  32.         MessageBusInterface $bus,
  33.         LoggerInterface $logger): Response
  34.     {
  35.         // Brevo webhooks IP ranges (@see https://help.brevo.com/hc/en-us/articles/15127404548498-Brevo-IP-ranges-List-of-publicly-exposed-services
  36.         $limitIps = ['1.179.112.0/20''172.246.240.0/20'];
  37.         if (!IpUtils::checkIp($request->getClientIp(), $limitIps)) {
  38.             throw new AccessDeniedHttpException('Acceso restringido por IP.');
  39.         }
  40.         if ($request->isMethod('POST')) {
  41.             if (str_contains($request->getContentType(), 'json')) {
  42.                 $body $request->getContent();
  43.                 if ($body) {
  44.                     $logger->info('EmailWebhooksController $body = ' .$body);
  45.                     $context = [
  46.                         AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
  47.                         AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true
  48.                     ];
  49.                     $webhook $serializer->deserialize($bodyWebhook::class, 'json'$context); // to deserialize a list of objects, you have to append [] to the type parameter
  50.                     $logger->info('EmailWebhooksController $webhook = ' .$serializer->serialize($webhook'json'));
  51.                     $bus->dispatch(new EmailWebhookQueued($webhook)); // async worker will process the queued webhook
  52.                     return new Response(''Response::HTTP_OK);
  53.                 }
  54.             }
  55.         }
  56.         //$logger->info('EmailWebhooksController BAD REQUEST');
  57.         return new Response(''Response::HTTP_BAD_REQUEST);
  58.     }
  59. }