Passed
Push — develop ( d944ff...2efefd )
by Stone
07:19 queued 10s
created

ValidateController::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 4
dl 0
loc 26
rs 9.9332
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
            $event = new UserValidatedEvent($user);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
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