Test Setup Failed
Push — develop ( 82cf6b...198473 )
by Stone
06:50 queued 11s
created

RegistrationController::validate()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 5
dl 0
loc 32
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Form\RegistrationFormType;
7
use App\Services\Registration\RegistrationAutoLogon;
8
use App\Services\Registration\RegistrationMailer;
9
use App\Services\Registration\RegistrationSetHash;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
17
18
class RegistrationController extends AbstractController
19
{
20
    /**
21
     * @var EntityManagerInterface
22
     */
23
    private $em;
24
25
    public function __construct(EntityManagerInterface $em)
26
    {
27
        $this->em = $em;
28
    }
29
30
    /**
31
     * @Route("/register", name="app_register")
32
     */
33
    public function register(
34
        Request $request,
35
        UserPasswordEncoderInterface $passwordEncoder,
36
        AuthorizationCheckerInterface $authChecker,
37
        RegistrationMailer $registrationMailer,
38
        RegistrationSetHash $registrationSetHash
39
    ): Response {
40
        //if we are authenticated, no reason to be here
41
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
42
            return $this->redirectToRoute('trick.home');
43
        }
44
45
        $user = new User();
46
        $form = $this->createForm(RegistrationFormType::class, $user);
47
        $form->handleRequest($request);
48
49
        if ($form->isSubmitted() && $form->isValid()) {
50
            // encode the plain password
51
            $user->setPassword(
52
                $passwordEncoder->encodePassword(
53
                    $user,
54
                    $form->get('plainPassword')->getData()
55
                )
56
            );
57
            $registrationSetHash->setHash($user);
58
59
            //send validation link
60
            $registrationMailer->sendHash($user);
61
62
            $this->addFlash('success', 'Account created, we have sent an email to ' . $user->getEmail() . ' with a validation link');
63
64
            return $this->redirectToRoute('trick.home');
65
        }
66
67
        return $this->render('registration/register.html.twig', [
68
            'registrationForm' => $form->createView(),
69
        ]);
70
    }
71
72
    /**
73
     * @Route("/validate/{id}/{token}", name="app_validate", methods={"GET"}, requirements={
74
     *     "id": "\d+",
75
     *     "token": "[a-h0-9]*"
76
     * })
77
     * @param User $user
78
     * @param $token
79
     * @param AuthorizationCheckerInterface $authChecker
80
     * @param RegistrationAutoLogon $autoLogon
81
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
82
     * @throws \Exception
83
     */
84
    public function validate(
85
        User $user,
86
        $token,
87
        AuthorizationCheckerInterface $authChecker,
88
        RegistrationAutoLogon $autoLogon,
89
        Request $request
90
    ) {
91
92
        //if we are authenticated, no reason to be here
93
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
94
            return $this->redirectToRoute('trick.home');
95
        }
96
97
        if ($user->getVerified()) {
98
            //Account already active
99
            return $this->redirectToRoute('app_login');
100
        }
101
102
        //checking the hash and valid date
103
        if ($user->isHashValid($token) && $user->isVerifiedDateTimeValid()) {
104
            $user->setVerified(true);
105
            $this->em->flush();
106
107
            $this->addFlash('success', 'Account is verified');
108
109
            //autologon
110
            $autoLogon->autoLogon($user, $request);
111
112
            return $this->redirectToRoute('trick.home');
113
        }
114
        return $this->render('registration/error.html.twig', [
115
            'user' => $user
116
        ]);
117
    }
118
119
    /**
120
     * @Route("/resendhash/{id}", name="app_resendhash", requirements={
121
     *     "id": "\d+"
122
     * })
123
     */
124
    public function sendVerifiedHash(User $user, RegistrationMailer $registrationMailer, RegistrationSetHash $registrationSetHash)
125
    {
126
        if (!$user->getVerified()) {
127
            $registrationSetHash->setHash($user);
128
            $registrationMailer->sendHash($user);
129
            $this->addFlash('success', 'Verification link sent to ' . $user->getEmail());
130
        }
131
        return $this->redirectToRoute('trick.home');
132
    }
133
}
134