Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

RegistrationController::checkEmailAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
3
namespace AWurth\SilexUser\Controller;
4
5
use AWurth\SilexUser\Entity\UserInterface;
6
use AWurth\SilexUser\Event\FilterUserResponseEvent;
7
use AWurth\SilexUser\Event\FormEvent;
8
use AWurth\SilexUser\Event\GetResponseUserEvent;
9
use AWurth\SilexUser\Event\Events;
10
use AWurth\SilexUser\Form\RegistrationFormType;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
/**
16
 * Silex User Registration Controller.
17
 *
18
 * @author Alexis Wurth <[email protected]>
19
 */
20
class RegistrationController extends Controller
21
{
22
    public function registerAction(Request $request)
23
    {
24
        $userManager = $this->getUserManager();
25
        $dispatcher = $this->getDispatcher();
26
27
        $user = $userManager->createUser();
28
        $user->setEnabled(true);
29
30
        $event = new GetResponseUserEvent($user, $request);
31
        $dispatcher->dispatch(Events::REGISTRATION_INITIALIZE, $event);
32
33
        if (null !== $event->getResponse()) {
34
            return $event->getResponse();
35
        }
36
37
        $form = $this->createForm(RegistrationFormType::class, $user);
38
39
        $form->handleRequest($request);
40
41
        if ($form->isSubmitted()) {
42
            if ($form->isValid()) {
43
                $event = new FormEvent($form, $request);
44
                $dispatcher->dispatch(Events::REGISTRATION_SUCCESS, $event);
45
46
                $userManager->updateUser($user);
47
48
                $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\SilexUser\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...
49
                if (null === $response) {
50
                    $response = $this->redirect('silex_user.registration_confirmed');
51
                }
52
53
                $dispatcher->dispatch(Events::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
0 ignored issues
show
Bug introduced by
It seems like $response defined by $event->getResponse() on line 48 can be null; however, AWurth\SilexUser\Event\F...nseEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
54
55
                return $response;
56
            }
57
58
            $event = new FormEvent($form, $request);
59
            $dispatcher->dispatch(Events::REGISTRATION_FAILURE, $event);
60
61
            $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\SilexUser\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...
62
            if (null !== $response) {
63
                return $response;
64
            }
65
        }
66
67
        return $this->render('silex_user/registration/register.twig', [
68
            'form' => $form->createView()
69
        ]);
70
    }
71
72
    public function checkEmailAction()
73
    {
74
        $session = $this->getSession();
75
        $email = $session->get('silex_user_confirmation_email');
76
77
        if (empty($email)) {
78
            return $this->redirect('silex_user.register');
79
        }
80
81
        $session->remove('silex_user_confirmation_email');
82
        $user = $this->getUserManager()->findUserByEmail($email);
83
84
        if (null === $user) {
85
            throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
86
        }
87
        
88
        return $this->render('silex_user/registration/check_email.twig', [
89
            'user' => $user
90
        ]);
91
    }
92
93
    public function confirmAction(Request $request, $token)
94
    {
95
        $userManager = $this->getUserManager();
96
97
        $user = $userManager->findUserByConfirmationToken($token);
98
99
        if (null === $user) {
100
            throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
101
        }
102
103
        $dispatcher = $this->getDispatcher();
104
105
        $user->setConfirmationToken(null);
106
        $user->setEnabled(true);
107
108
        $event = new GetResponseUserEvent($user, $request);
109
        $dispatcher->dispatch(Events::REGISTRATION_CONFIRM, $event);
110
111
        $userManager->updateUser($user);
112
113
        $response = $event->getResponse();
114
        if (null === $response) {
115
            $response = $this->redirect('silex_user.registration_confirmed');
116
        }
117
        
118
        $dispatcher->dispatch(Events::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response));
119
120
        return $response;
121
    }
122
123
    public function confirmedAction()
124
    {
125
        $user = $this->getUser();
126
        if (!is_object($user) || !$user instanceof UserInterface) {
127
            throw new AccessDeniedException('This user does not have access to this section.');
128
        }
129
130
        return $this->render('silex_user/registration/confirmed.twig');
131
    }
132
}
133