<?php
namespace App\Action\PressSite\Account;
use App\Action\PressSite\DomainAwareAction;
use App\Contract\PressSite\Language\Switcher\GenericActionInterface;
use App\Entity\PressSite\AccountManager;
use App\Service\PressSite\DomainAwareManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;
/**
* Class SignInAction.
*/
class SignInAction extends DomainAwareAction implements GenericActionInterface
{
/**
* The token manager.
*
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
*/
private $tokenStorage;
/**
* The routing manager.
*
* @var \Symfony\Component\Routing\RouterInterface
*/
private $router;
public function __construct(
Environment $twig,
DomainAwareManager $domainAwareManager,
TokenStorageInterface $tokenStorage,
RouterInterface $router
) {
parent::__construct($twig, $domainAwareManager);
$this->tokenStorage = $tokenStorage;
$this->router = $router;
}
/**
* Responsible for rendering the authentication page.
*
* The homepage will actually serve as a login page as well,
* because in order for a member to access the rest of the pages,
* they have to login first.
*
* @param \Symfony\Component\Security\Http\Authentication\AuthenticationUtils $authenticationUtils
* The authentication helper
*
* @return \Symfony\Component\HttpFoundation\Response
* The response object to return
*/
public function __invoke(AuthenticationUtils $authenticationUtils): Response
{
$token = $this->tokenStorage->getToken();
if ($token && ($member = $token->getUser()) && $member instanceof AccountManager) {
return new RedirectResponse(
$this->router->generate($this->getDefaultRedirectRoute(), [
'_locale' => $this->getDomainManager()->getCurrentByHostnameAndLocale()->getLanguageCode(),
])
);
}
return $this->render('press_site/actions/account/sign_in.html.twig', [
'last_user' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
]);
}
}