<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends Controller
{
    /**
     * @Route("/login", name="login")
     * @Method({"GET", "POST"}) Post requests are intercepted by Symfony but must be allowed here
     *
     * @return Response
     */
    public function loginAction(AuthenticationUtils $authenticationUtils, UserInterface $user = null)
    {
        if ($user) {
            $this->addFlash('warning', 'You are already logged in.');

            return $this->redirectToRoute('homepage');
        }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('shared/login.html.twig', [
            'last_username' => $lastUsername,
            'error' => $error,
        ]);
    }
}
