RegistrationController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 11
dl 0
loc 129
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B registerAction() 0 49 6
A checkEmailAction() 0 20 3
B confirmAction() 0 29 3
A confirmedAction() 0 11 3
1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\Controller;
13
14
use AWurth\Silex\User\Event\Events;
15
use AWurth\Silex\User\Event\FilterUserResponseEvent;
16
use AWurth\Silex\User\Event\FormEvent;
17
use AWurth\Silex\User\Event\GetResponseUserEvent;
18
use AWurth\Silex\User\Form\Type\RegistrationFormType;
19
use AWurth\Silex\User\Model\UserInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
24
25
/**
26
 * User Registration Controller.
27
 *
28
 * @author Alexis Wurth <[email protected]>
29
 */
30
class RegistrationController extends Controller
31
{
32
    public function registerAction(Request $request)
33
    {
34
        $userManager = $this->getUserManager();
35
        $dispatcher = $this->getDispatcher();
36
37
        $user = $userManager->createUser();
38
        $user->setEnabled(true);
39
40
        $event = new GetResponseUserEvent($user, $request);
41
        $dispatcher->dispatch(Events::REGISTRATION_INITIALIZE, $event);
42
43
        if (null !== $event->getResponse()) {
44
            return $event->getResponse();
45
        }
46
47
        $form = $this->createForm(RegistrationFormType::class, $user);
48
49
        $form->handleRequest($request);
50
51
        if ($form->isSubmitted()) {
52
            if ($form->isValid()) {
53
                $event = new FormEvent($form, $request);
54
                $dispatcher->dispatch(Events::REGISTRATION_SUCCESS, $event);
55
56
                $userManager->updateUser($user);
57
58
                $response = $event->getResponse();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $event->getResponse() (which targets AWurth\Silex\User\Event\FormEvent::getResponse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
                if (null === $response) {
60
                    $response = $this->redirect('silex_user.registration_confirmed');
61
                }
62
63
                $dispatcher->dispatch(Events::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
64
65
                return $response;
66
            }
67
68
            $event = new FormEvent($form, $request);
69
            $dispatcher->dispatch(Events::REGISTRATION_FAILURE, $event);
70
71
            $response = $event->getResponse();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $event->getResponse() (which targets AWurth\Silex\User\Event\FormEvent::getResponse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
            if (null !== $response) {
73
                return $response;
74
            }
75
        }
76
77
        return $this->render('silex_user/registration/register.twig', [
78
            'form' => $form->createView()
79
        ]);
80
    }
81
82
    /**
83
     * Tell the user to check their email provider.
84
     */
85
    public function checkEmailAction()
86
    {
87
        $session = $this->getSession();
88
        $email = $session->get('silex_user_confirmation_email');
89
90
        if (empty($email)) {
91
            return $this->redirect('silex_user.register');
92
        }
93
94
        $session->remove('silex_user_confirmation_email');
95
        $user = $this->getUserManager()->findUserByEmail($email);
96
97
        if (null === $user) {
98
            throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
99
        }
100
        
101
        return $this->render('silex_user/registration/check_email.twig', [
102
            'user' => $user
103
        ]);
104
    }
105
106
    /**
107
     * Receive the confirmation token from user email provider and login the user.
108
     *
109
     * @param Request $request
110
     * @param string $token
111
     *
112
     * @return Response
113
     */
114
    public function confirmAction(Request $request, $token)
115
    {
116
        $userManager = $this->getUserManager();
117
118
        $user = $userManager->findUserByConfirmationToken($token);
119
120
        if (null === $user) {
121
            throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
122
        }
123
124
        $dispatcher = $this->getDispatcher();
125
126
        $user->setConfirmationToken(null);
127
        $user->setEnabled(true);
128
129
        $event = new GetResponseUserEvent($user, $request);
130
        $dispatcher->dispatch(Events::REGISTRATION_CONFIRM, $event);
131
132
        $userManager->updateUser($user);
133
134
        $response = $event->getResponse();
135
        if (null === $response) {
136
            $response = $this->redirect('silex_user.registration_confirmed');
137
        }
138
        
139
        $dispatcher->dispatch(Events::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response));
140
141
        return $response;
142
    }
143
144
    /**
145
     * Tell the user their account is confirmed.
146
     */
147
    public function confirmedAction()
148
    {
149
        $user = $this->getUser();
150
        if (!is_object($user) || !$user instanceof UserInterface) {
151
            throw new AccessDeniedException('This user does not have access to this section.');
152
        }
153
154
        return $this->render('silex_user/registration/confirmed.twig', [
155
            'user' => $user
156
        ]);
157
    }
158
}
159