Test Failed
Branch develop (bbb827)
by Stone
04:24
created

RegistrationController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 21 4
A forgotPassword() 0 24 4
A __construct() 0 3 1
1
<?php
2
3
namespace App\Controller\User;
4
5
use App\Entity\User;
6
use App\Event\User\UserRegisteredEvent;
7
use App\Event\User\UserForgotpasswordEvent;
8
use App\Form\ForgotpasswordFormType;
9
use App\Form\RegistrationFormType;
10
use App\Services\FlashMessageCategory;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17
18
class RegistrationController extends AbstractController
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("/register", name="app_register")
32
     */
33
    public function register(Request $request, AuthorizationCheckerInterface $authChecker): Response
34
    {
35
        //if we are authenticated, no reason to be here
36
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
37
            return $this->redirectToRoute('trick.home');
38
        }
39
40
        $user = new User();
41
        $form = $this->createForm(RegistrationFormType::class, $user);
42
        $form->handleRequest($request);
43
44
        if ($form->isSubmitted() && $form->isValid()) {
45
46
            $event = new UserRegisteredEvent($user, $form->get('plainPassword')->getData());
47
            $this->dispatcher->dispatch(UserRegisteredEvent::NAME, $event);
48
49
            return $this->redirectToRoute('trick.home');
50
        }
51
52
        return $this->render('registration/register.html.twig', [
53
            'registrationForm' => $form->createView(),
54
        ]);
55
    }
56
57
58
59
    /**
60
     * @Route("/forgotpassword", name="app_forgotpassword")
61
     */
62
    public function forgotPassword(Request $request)
63
    {
64
65
        $form = $this->createForm(ForgotpasswordFormType::class);
66
        $form->handleRequest($request);
67
        if ($form->isSubmitted() && $form->isValid()) {
68
69
            //get the user object from the email or user
70
            $user = $this->getDoctrine()
71
                ->getRepository(User::class)
72
                ->findUserByMailOrUsername($form->get('userName')->getData());
73
74
            if ($user) {//Only send mail if an account was found
75
                $event = new UserForgotpasswordEvent($user);
76
                $this->dispatcher->dispatch(UserForgotpasswordEvent::NAME, $event);
77
            }
78
79
            //Do not say if account was found or not to avoid robots testing for emails. This can still be tested by a hacker by calculating the reply time but not as easy.
80
            $this->addFlash(FlashMessageCategory::INFO, 'If you have an account, then an email has been sent to your registered email');
81
            return $this->redirectToRoute('trick.home');
82
        }
83
84
        return $this->render('registration/forgotpassword.html.twig', [
85
            'forgotpasswordForm' => $form->createView(),
86
        ]);
87
    }
88
89
90
91
}
92