Test Setup Failed
Push — develop ( dc2cdb...883a72 )
by Stone
04:31
created

RegistrationController::sendVerifiedHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Event\User\UserRegisteredEvent;
7
use App\Event\User\UserValidatedEvent;
8
use App\Form\RegistrationFormType;
9
use App\Services\FlashMessageCategory;
10
use App\Security\UserAutoLogon;
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
     * @Route("/validate/{token}", name="app_validate", methods={"GET"}, requirements={
59
     *     "token": "[a-h0-9]*"
60
     * })
61
     */
62
    public function validate(
63
        $token,
64
        AuthorizationCheckerInterface $authChecker,
65
        UserAutoLogon $autoLogon
66
    ) {
67
        //if we are authenticated, no reason to be here
68
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
69
            return $this->redirectToRoute('trick.home');
70
        }
71
72
        $user = $this->getDoctrine()
73
            ->getRepository(User::class)
74
            ->findUserByHash($token);
75
76
        if (!$user) {
77
            //no user found
78
            $this->addFlash(FlashMessageCategory::ERROR, 'Invalid Token, please use this form to resend a link');
79
            return $this->redirectToRoute('app_forgotpassword');
80
        }
81
82
        if ($user->getVerified()) {
83
            //Account already active
84
            $this->addFlash(FlashMessageCategory::INFO, 'Mail already verified');
85
            return $this->redirectToRoute('app_login');
86
        }
87
88
        //checking the date
89
        if ($user->isVerifiedDateTimeValid()) {
90
91
            $event = new UserValidatedEvent($user);
92
            $this->dispatcher->dispatch(UserValidatedEvent::NAME, $event);
93
94
            //autologon
95
            $autoLogon->autoLogon($user);
96
97
            return $this->redirectToRoute('trick.home');
98
        }
99
100
        //Error, redirect to the forgot password
101
        $this->addFlash(FlashMessageCategory::ERROR, 'Your verification link is no longer valid, please use this form to resend a link');
102
        return $this->redirectToRoute('app_forgotpassword');
103
    }
104
105
    /**
106
     * @Route("/forgotpassword", name="app_forgotpassword")
107
     */
108
    public function forgotPassword()
109
    {
110
        //TODO: Form Posted, Call user event caught bu the userRegisteredSubscriber
111
        //TODO: Also need a route for the reset password, this will probably use the same validation so make private function ?
112
113
        //TODO: show forgot password form, for now just reusing the Error template
114
        return $this->render('registration/error.html.twig', [
115
116
        ]);
117
    }
118
}
119