Issues (62)

src/Controller/Security/ResettingController.php (1 issue)

Severity
1
<?php
2
namespace App\Controller\Security;
3
4
use App\Command\ResetPasswordCommand;
5
use App\Command\ResetPasswordConfirmationCommand;
6
use App\Form\Type\NewPasswordType;
7
use App\Form\Type\PasswordRequestType;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\Routing\Annotation\Route;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Messenger\MessageBusInterface;
13
use App\Repository\UserRepository;
14
use Symfony\Component\Form\FormError;
15
16
class ResettingController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

16
class ResettingController extends /** @scrutinizer ignore-deprecated */ Controller
Loading history...
17
{
18
    /**
19
     * @var MessageBusInterface
20
     */
21
    private $commandBus;
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
    /**
27
     * @var UserRepository
28
     */
29
    private $repository;
30
31
    /**
32
     * @param MessageBusInterface $commandBus
33
     * @param LoggerInterface $logger
34
     * @param UserRepository $repository
35
     */
36
    public function __construct(
37
        MessageBusInterface $commandBus,
38
        LoggerInterface $logger,
39
        UserRepository $repository
40
    )
41
    {
42
        $this->commandBus = $commandBus;
43
        $this->logger = $logger;
44
        $this->repository = $repository;
45
    }
46
47
    /**
48
     * @Route("/reset_password", name="reset_password", methods={"GET", "POST"})
49
     */
50
    public function resetPassword(Request $request)
51
    {
52
        $form = $this->createForm(PasswordRequestType::class);
53
        $form->handleRequest($request);
54
55
        try {
56
            if ($form->isSubmitted() && $form->isValid()) {
57
                $command = new ResetPasswordCommand(
58
                    $form->get('email')->getData()
59
                );
60
61
                $this->commandBus->dispatch($command);
62
                $this->addFlash('success', "An email has been sent to your address");
63
                return $this->redirectToRoute('reset_password');
64
            }
65
        } catch (\Exception $e) {
66
            $this->addFlash('danger','Error while resetting password');
67
            $error = new FormError("There is an error: ".$e->getMessage());
68
            $form->addError($error);
69
        }
70
71
        return $this->render('security/reset-password.html.twig', ['form' => $form->createView()]);
72
    }
73
74
    /**
75
     * @Route("/reset_password/confirm/{token}", name="reset_password_confirm", methods={"GET", "POST"})
76
     */
77
    public function resetPasswordCheck(Request $request, string $token) 
78
    {
79
        $token = filter_var($token, FILTER_SANITIZE_STRING);
80
81
        $form = $this->createForm(NewPasswordType::class);
82
        $form->handleRequest($request);
83
84
        try {
85
            if ($form->isSubmitted() && $form->isValid()) {
86
                $command = new ResetPasswordConfirmationCommand(
87
                    $token,
88
                    $form->get('password')->getData()
89
                );
90
91
                $this->commandBus->dispatch($command);
92
                $this->addFlash('success', "Your new password has been set");
93
                return $this->redirectToRoute('homepage');
94
            }
95
        } catch (\Exception $e) {
96
            $this->addFlash('danger','Error while resetting password');
97
            $error = new FormError("There is an error: ".$e->getMessage());
98
            $form->addError($error);
99
        }
100
101
        return $this->render('security/reset-password-confirm.html.twig', ['form' => $form->createView()]);
102
    }
103
}