<?php
namespace App\Controller;
use App\Service\Mailgun;
use Http\Discovery\HttpClientDiscovery;
use Mailgun\Hydrator\ArrayHydrator;
use Mailgun\RequestBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Class SecurityController
* @package App\Controller
*/
class SecurityController extends AbstractController
{
/**
* @Route("/inloggen", name="login")
* @param AuthenticationUtils $authenticationUtils
* @return \Symfony\Component\HttpFoundation\Response
*/
public function login(AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
/**
* @Route("/gebruikers", name="gebruikers")
*/
public function users()
{
return $this->render('app/index.html.twig', [
'title' => 'Gebruikers'
]);
}
/**
* @Route("/mailgun/webhook", methods={"POST"}, name="mailgun_webhook")
*/
public function mailgunWebhook(Request $request, Mailgun $mailer)
{
if (!$data = json_decode($request->getContent(), true)) {
return new JsonResponse([
'status' => 'error',
'message' => 'No data',
]);
}
$c = HttpClientDiscovery::find();
$r = new RequestBuilder();
$h = new ArrayHydrator();
$w = new \Mailgun\Api\Webhook($c, $r, $h, $_ENV['MAILGUN_WEBHOOK_SIGNING_KEY']);
$sig = $data['signature'];
if ($w->verifyWebhookSignature($sig['timestamp'], $sig['token'], $sig['signature'])) {
$event = $data['event-data'];
$mailer->sendWebhookEmail($event);
return new JsonResponse([
'status' => 'success',
]);
}
return new JsonResponse([
'status' => 'error',
'message' => 'Invalid signature',
'data' => $data
]);
}
}