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

ResetPasswordController::resetPassword()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 4
dl 0
loc 31
rs 9.4555
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\UserResetpasswordEvent;
7
use App\Event\User\UserValidatedEvent;
8
use App\FlashMessage\FlashMessageCategory;
9
use App\Form\ResetpasswordFormType;
10
use App\Security\UserValidator;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class ResetPasswordController extends AbstractController
18
{
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $dispatcher;
24
25
    public function __construct(EventDispatcherInterface $dispatcher)
26
    {
27
        $this->dispatcher = $dispatcher;
28
    }
29
30
    /**
31
     * @Route("/user/resetpassword/{token}", name="app_resetpassword", methods={"GET", "POST"}, requirements={
32
     *     "token": "[a-h0-9]*"
33
     * })
34
     */
35
    public function resetPassword(
36
        string $token,
37
        Request $request,
38
        AuthorizationCheckerInterface $authChecker,
39
        UserValidator $userValidator
40
    ) {
41
        //if we are authenticated, no reason to be here
42
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
43
            return $this->redirectToRoute('home');
44
        }
45
46
        //If we got here then we followed a reset link from email. We can verify mail
47
        if ($userValidator->doesResetpasswordTokenValidateEmail($token)) {
48
            $event = new UserValidatedEvent($user);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
49
            $this->dispatcher->dispatch(UserValidatedEvent::NAME, $event);
50
        }
51
52
        $form = $this->createForm(ResetpasswordFormType::class);
53
        $form->handleRequest($request);
54
55
        if ($form->isSubmitted() && $form->isValid()) {
56
            $event = new UserResetpasswordEvent($userValidator->retrieveUserFromToken($token), $form->get('plainPassword')->getData());
57
            $this->dispatcher->dispatch(UserResetpasswordEvent::NAME, $event);
58
59
            $this->addFlash(FlashMessageCategory::SUCCESS, "A reset password link has been sent");
60
            return $this->redirectToRoute('app_login');
61
        }
62
63
        return $this->render('validation/resetpassword.html.twig', [
64
            'resetpasswordForm' => $form->createView(),
65
            'userEmail' => $user->getEmail(),
66
        ]);
67
    }
68
}