src/Entrepreneurs/Bundle/AppBundle/Controller/ApiController.php line 17

Open in your IDE?
  1. <?php
  2. namespace Entrepreneurs\Bundle\AppBundle\Controller;
  3. use Entrepreneurs\Bundle\AppBundle\Propel\SentEmailMailjetTrack;
  4. use Entrepreneurs\Bundle\AppBundle\Propel\SentEmailMailjetTrackQuery;
  5. use Entrepreneurs\Bundle\AppBundle\Propel\SentEmailQuery;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class ApiController extends AbstractController
  12. {
  13.     #[Route('/api/mail/webhook/{hash}'name'api_mails_webhook')]
  14.     public function webhook(Request $requeststring $hashstring $webhookHash): Response
  15.     {
  16.         if ($hash !== $webhookHash) {
  17.             return new JsonResponse(['error' => 'Accès non autorisĂ©'], Response::HTTP_FORBIDDEN);
  18.         }
  19.         $content $request->getContent();
  20.         $event json_decode($contenttrue);
  21.         if ($event) {
  22.             $messageId $event['CustomID'] ?? null;
  23.             $eventType $event['event'] ?? null;
  24.             if (in_array($eventType, ['sent''open''click''bounce'])) {
  25.                 $sentMail SentEmailQuery::create()->filterByMessageId($messageId)->findOne();
  26.                 if ($sentMail) {
  27.                     $sentMailJetTrack SentEmailMailjetTrackQuery::create()
  28.                         ->select('id')
  29.                         ->filterBySentEmailId($sentMail->getId())
  30.                         ->filterByMailjetEventType($eventType)
  31.                         ->findOne()
  32.                     ;
  33.                     $date = new \DateTime();
  34.                     if (!$sentMailJetTrack) {
  35.                         $sentMailJetTrack = new SentEmailMailjetTrack();
  36.                         $sentMailJetTrack->setSentEmailId($sentMail->getId());
  37.                         $sentMailJetTrack->setMailjetEventType($eventType);
  38.                         $sentMailJetTrack->setDate($date);
  39.                         $sentMailJetTrack->setRecipient($event['email']);
  40.                         $sentMailJetTrack->setTargetLinkUrl($event['url'] ?? null);
  41.                         $sentMailJetTrack->setMessageId($messageId);
  42.                         $sentMailJetTrack->setFailReason($event['error'] ?? null);
  43.                         $sentMailJetTrack->save();
  44.                         $sentMail->setLastStatus($sentMail->describeEvent($eventType$date));
  45.                         $sentMail->save();
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.         return new JsonResponse(['status' => 'success']);
  51.     }
  52. }