ValidateController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 27 3
1
<?php
2
3
namespace App\Controller\User;
4
5
use App\Event\User\UserValidatedEvent;
6
use App\Security\UserAutoLogon;
7
use App\FlashMessage\FlashMessageCategory;
8
use App\Security\UserValidator;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13
14
class ValidateController extends AbstractController
15
{
16
17
    /**
18
     * @var EventDispatcherInterface
19
     */
20
    private $dispatcher;
21
22
    public function __construct(EventDispatcherInterface $dispatcher)
23
    {
24
        $this->dispatcher = $dispatcher;
25
    }
26
27
    /**
28
     * @Route("/user/validate/{token}", name="app_validate", methods={"GET"}, requirements={
29
     *     "token": "[a-h0-9]*"
30
     * })
31
     */
32
    public function validate(
33
        string $token,
34
        AuthorizationCheckerInterface $authChecker,
35
        UserAutoLogon $autoLogon,
36
        UserValidator $userValidator
37
    ) {
38
        //if we are authenticated, no reason to be here
39
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
40
            return $this->redirectToRoute('home');
41
        }
42
43
        if ($userValidator->isUserTokenValid($token)) {
44
45
            $user = $userValidator->retrieveUserFromToken($token);
46
            $event = new UserValidatedEvent($user);
47
            $this->dispatcher->dispatch(UserValidatedEvent::NAME, $event);
48
49
            //autologon
50
            $autoLogon->autoLogon($user);
51
52
            return $this->redirectToRoute('home');
53
        }
54
55
        //Error, redirect to the forgot password
56
        $this->addFlash(FlashMessageCategory::ERROR,
57
            'Your verification link is no longer valid, please use this form to resend a link');
58
        return $this->redirectToRoute('app_forgotpassword');
59
    }
60
61
}
62