Version 0.5.2
[userbundle] / Handler / AuthenticationFailureHandler.php
index c5d0ec5fd43f7adbf827dee72a2cdc41c4a4ab4e..14d4dec4a28494cbdc7b97c39bc884be49180610 100644 (file)
 namespace Rapsys\UserBundle\Handler;
 
 use Doctrine\Persistence\ManagerRegistry;
+
 use Psr\Log\LoggerInterface;
+
+use Rapsys\PackBundle\Util\SluggerUtil;
+
+use Rapsys\UserBundle\Exception\UnactivatedException;
+use Rapsys\UserBundle\RapsysUserBundle;
+
 use Symfony\Bridge\Twig\Mime\TemplatedEmail;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -35,14 +42,15 @@ use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureH
 use Symfony\Component\Security\Http\HttpUtils;
 use Symfony\Contracts\Translation\TranslatorInterface;
 
-use Rapsys\PackBundle\Util\SluggerUtil;
-use Rapsys\UserBundle\Exception\UnactivatedException;
-use Rapsys\UserBundle\RapsysUserBundle;
-
 /**
  * {@inheritdoc}
  */
 class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
+       /**
+        * Alias string
+        */
+       protected string $alias;
+
        /**
         * Config array
         */
@@ -55,6 +63,8 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
        ];
 
        /**
+        * {@inheritdoc}
+        *
         * @xxx Second argument will be replaced by security.firewalls.main.logout.target
         * @see vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php +360
         *
@@ -69,15 +79,13 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
         * @param SluggerUtil $slugger The slugger instance
         * @param RequestStack $stack The stack instance
         * @param TranslatorInterface $translator The translator instance
-        *
-        * {@inheritdoc}
         */
        public function __construct(protected HttpKernelInterface $httpKernel, protected HttpUtils $httpUtils, protected array $options, protected ?LoggerInterface $logger, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected MailerInterface $mailer, protected RouterInterface $router, protected SluggerUtil $slugger, protected RequestStack $stack, protected TranslatorInterface $translator) {
                //Call parent constructor
                parent::__construct($httpKernel, $httpUtils, $options, $logger);
 
                //Set config
-               $this->config = $container->getParameter(self::getAlias());
+               $this->config = $container->getParameter($this->alias = RapsysUserBundle::getAlias());
        }
 
        /**
@@ -100,9 +108,9 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
        }
 
        /**
-        * This is called when an interactive authentication attempt fails
-        *
         * {@inheritdoc}
+        *
+        * This is called when an interactive authentication attempt fails
         */
        public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response {
                //With bad credential exception
@@ -184,7 +192,7 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
                        //With not enabled user
                        } elseif ($parent instanceof DisabledException) {
                                //Add error message account is not enabled
-                               $this->addFlash('error', $this->translator->trans('Account not enabled'));
+                               $this->addFlash('error', $this->translator->trans('Account not enabled', [], $this->alias));
 
                                //Redirect on the same route with sent=1 to cleanup form
                                return new RedirectResponse($this->router->generate($request->get('_route'), $request->get('_route_params')), 302);
@@ -243,7 +251,7 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
                                        } while(next($keys));
 
                                        //Set translation
-                                       $current = $this->translator->trans($current);
+                                       $current = $this->translator->trans($current, [], $this->alias);
 
                                        //Remove reference
                                        unset($current);
@@ -253,7 +261,8 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
                                $context['subject'] = $subject = ucfirst(
                                        $this->translator->trans(
                                                $this->config['register']['mail']['subject'],
-                                               $this->slugger->flatten($context, null, '.', '%', '%')
+                                               $this->slugger->flatten($context, null, '.', '%', '%'),
+                                               $this->alias
                                        )
                                );
 
@@ -282,14 +291,14 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
                                //Catch obvious transport exception
                                } catch(TransportExceptionInterface $e) {
                                        //Add error message mail unreachable
-                                       $this->addFlash('error', $this->translator->trans('Unable to reach account'));
+                                       $this->addFlash('error', $this->translator->trans('Unable to reach account', [], $this->alias));
                                }
 
                                //Add notice
-                               $this->addFlash('notice', $this->translator->trans('Your verification mail has been sent, to activate your account follow the confirmation link inside'));
+                               $this->addFlash('notice', $this->translator->trans('Your verification mail has been sent, to activate your account follow the confirmation link inside', [], $this->alias));
 
                                //Add junk warning
-                               $this->addFlash('warning', $this->translator->trans('If you did not receive a verification mail, check your Spam or Junk mail folder'));
+                               $this->addFlash('warning', $this->translator->trans('If you did not receive a verification mail, check your Spam or Junk mail folder', [], $this->alias));
 
                                //Redirect on the same route with sent=1 to cleanup form
                                return new RedirectResponse($this->router->generate($request->get('_route'), $request->get('_route_params')), 302);
@@ -299,11 +308,4 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler {
                //Call parent function
                return parent::onAuthenticationFailure($request, $exception);
        }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function getAlias(): string {
-               return RapsysUserBundle::getAlias();
-       }
 }