Completed
Push — master ( febf2b...b3372e )
by Valery
13:12 queued 13:12
created

ResetPasswordController::passwordReset()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Auth;
6
7
use App\Controller\BaseController;
8
use App\Entity\User;
9
use App\Form\Type\PasswordType;
10
use App\Form\Type\UserEmailType;
11
use App\Repository\ResettingRepository;
12
use App\Service\Auth\ResettingService;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
final class ResetPasswordController extends BaseController
19
{
20
    /**
21
     * @Route("/password/reset", methods={"GET|POST"}, name="password_reset")
22
     */
23
    public function passwordReset(ResettingService $service, Request $request): Response
24
    {
25
        $form = $this->createForm(UserEmailType::class, []);
26
        $form->handleRequest($request);
27
28
        if ($form->isSubmitted() && $form->isValid()) {
29
            $service->sendResetPasswordLink($request);
30
        }
31
32
        return $this->render('auth/passwords/password_reset.html.twig', [
33
            'site' => $this->site($request),
34
            'form' => $form->createView(),
35
        ]);
36
    }
37
38
    /**
39
     * @Route("/password/reset/{token}", methods={"GET|POST"}, name="password_reset_confirm")
40
     */
41
    public function passwordResetConfirm(ResettingRepository $repository, Request $request, string $token): Response
42
    {
43
        /** @var User $user */
44
        $user = $repository->findOneBy(['confirmation_token' => $token]);
45
46
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type App\Entity\User, thus it always evaluated to true.
Loading history...
47
            // Token not found.
48
            return new RedirectResponse($this->generateUrl('security_login'));
49
        } elseif (!$user->isPasswordRequestNonExpired($user::TOKEN_TTL)) {
50
            // Token has expired.
51
            $this->addFlash('danger', 'message.token_expired');
52
53
            return new RedirectResponse($this->generateUrl('password_reset'));
54
        }
55
56
        $form = $this->createForm(PasswordType::class, []);
57
        $form->handleRequest($request);
58
59
        if ($form->isSubmitted() && $form->isValid()) {
60
            $repository->setPassword($user, $form->getNormData()['password']);
61
            $this->addFlash('success', 'message.password_has_been_reset');
62
63
            return $this->redirectToRoute('security_login');
64
        }
65
66
        return $this->render('auth/passwords/password_change.html.twig', [
67
            'site' => $this->site($request),
68
            'form' => $form->createView(),
69
        ]);
70
    }
71
}
72