Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

ValidateController::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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