Passed
Push — master ( a8be6a...01edf4 )
by Gerard
02:10
created

PasswordResetController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 22
dl 0
loc 42
rs 10
c 1
b 0
f 1
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 32 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gbere\SimpleAuth\Controller;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\OptimisticLockException;
9
use Doctrine\ORM\ORMException;
10
use Exception;
11
use Gbere\SimpleAuth\Entity\User;
12
use Symfony\Bridge\Twig\Mime\NotificationEmail;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
18
use Symfony\Component\Mailer\MailerInterface;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
21
22
final class PasswordResetController extends AbstractController
23
{
24
    /**
25
     * @Route("/login/password/reset/{token}", name="gbere_auth_password_reset")
26
     *
27
     * @throws Exception
28
     * @throws ORMException
29
     * @throws OptimisticLockException
30
     * @throws TransportExceptionInterface
31
     */
32
    public function __invoke(string $token, Request $request, UserPasswordEncoderInterface $passwordEncoder, MailerInterface $mailer): Response
33
    {
34
        /** @var EntityManager $manager */
35
        $manager = $this->getDoctrine()->getManager();
36
        /** @var User|null $user */
37
        $user = $manager->getRepository(User::class)->findOneBy(['confirmationToken' => $token]);
38
        if (null === $user) {
39
            $this->addFlash('warning', 'The token is invalid');
40
41
            return $this->redirectToRoute('gbere_auth_login');
42
        }
43
44
        $form = $this->createFormBuilder()->add('plainPassword', PasswordType::class)->getForm();
45
        $form->handleRequest($request);
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            $user->setPassword($passwordEncoder->encodePassword($user, $form->get('plainPassword')->getData()));
48
            $user->hasEnabled(true);
49
            $user->setConfirmationToken(null);
50
            $manager->persist($user);
51
            $manager->flush();
52
            $this->addFlash('success', 'The password was updated');
53
            $mailer->send((new NotificationEmail())
54
                ->from($this->getParameter('email.sender'))
55
                ->to($user->getEmail())
56
                ->subject('Password reset')
57
                ->htmlTemplate('emails/password-reset-notification.html.twig')
58
            );
59
60
            return $this->redirectToRoute('gbere_auth_login');
61
        }
62
63
        return $this->render('frontend/password-reset.html.twig', ['form' => $form->createView()]);
64
    }
65
}
66