src/Controller/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\Mailgun;
  4. use Http\Discovery\HttpClientDiscovery;
  5. use Mailgun\Hydrator\ArrayHydrator;
  6. use Mailgun\RequestBuilder;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. /**
  13.  * Class SecurityController
  14.  * @package App\Controller
  15.  */
  16. class SecurityController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/inloggen", name="login")
  20.      * @param AuthenticationUtils $authenticationUtils
  21.      * @return \Symfony\Component\HttpFoundation\Response
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtils)
  24.     {
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('security/login.html.twig', array(
  30.             'last_username' => $lastUsername,
  31.             'error'         => $error,
  32.         ));
  33.     }
  34.     /**
  35.      * @Route("/gebruikers", name="gebruikers")
  36.      */
  37.     public function users()
  38.     {
  39.         return $this->render('app/index.html.twig', [
  40.             'title' => 'Gebruikers'
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/mailgun/webhook", methods={"POST"}, name="mailgun_webhook")
  45.      */
  46.     public function mailgunWebhook(Request $requestMailgun $mailer)
  47.     {
  48.         if (!$data json_decode($request->getContent(), true)) {
  49.             return new JsonResponse([
  50.                 'status' => 'error',
  51.                 'message' => 'No data',
  52.             ]);
  53.         }
  54.         $c HttpClientDiscovery::find();
  55.         $r = new RequestBuilder();
  56.         $h = new ArrayHydrator();
  57.         $w = new \Mailgun\Api\Webhook($c$r$h$_ENV['MAILGUN_WEBHOOK_SIGNING_KEY']);
  58.         $sig $data['signature'];
  59.         
  60.         if ($w->verifyWebhookSignature($sig['timestamp'], $sig['token'], $sig['signature'])) {
  61.             $event $data['event-data'];
  62.             $mailer->sendWebhookEmail($event);
  63.             return new JsonResponse([
  64.                 'status' => 'success',
  65.             ]);
  66.         }
  67.         return new JsonResponse([
  68.             'status' => 'error',
  69.             'message' => 'Invalid signature',
  70.             'data' => $data
  71.         ]);
  72.     }
  73. }