LoginController   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 138
Duplicated Lines 11.59 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 76.81%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 9
dl 16
loc 138
ccs 53
cts 69
cp 0.7681
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B resetPasswordEmailAction() 4 33 3
C resetPasswordGetAction() 4 22 8
B resetPasswordPostAction() 8 26 6
A setUserPassword() 0 12 1
A setResetToken() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of Webcook security bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook
7
 */
8
9
namespace Webcook\Cms\SecurityBundle\Controller;
10
11
use Webcook\Cms\CoreBundle\Base\BaseRestController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Webcook\Cms\SecurityBundle\Entity\User;
15
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
16
use FOS\RestBundle\Controller\Annotations\Post;
17
use FOS\RestBundle\Controller\Annotations\Get;
18
19
/**
20
 * Login controller.
21
 */
22
class LoginController extends BaseRestController
23
{
24
25
    /**
26
    * Send an email with a link to reset user's password.
27
    *
28
    * @ApiDoc(
29
    *  description="Send an email with a link to reset user's password."
30
    * )
31
    * @Post("password/email/reset", options={"i18n"=false})
32
    */
33
    public function resetPasswordEmailAction(Request $request): Response
34 7
    {
35
        $email = $request->request->get('email');
36 7
        $user  = $this->getEntityManager()->getRepository('Webcook\Cms\SecurityBundle\Entity\User')->findOneBy(array('email'=> $email));
37 7
38 View Code Duplication
        if ($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39 7
            $view = $this->getViewWithMessage(null, 404, 'This email does not exist. Please enter a valid email.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40 1
            return $this->handleView($view);
41 1
        }
42
43
        $resetLink = $this->setResetToken($user);
44 6
        $message = \Swift_Message::newInstance()
45 6
            ->setSubject('Password Reset')
46 6
            ->setFrom('[email protected]')
47 6
            ->setTo($email)
48 6
            ->setBody($this->render(
49 6
                'WebcookCmsSecurityBundle:Auth:emailTemplate.html.twig',
50 6
                array(
51
                    'user'      => $user->getUsername(),
52 6
                    'resetLink' => $resetLink
53 6
                )
54
            ))
55
            ->setContentType("text/html");
56 6
57
        $result = $this->get('mailer')->send($message);
58 6
        if ($result) {
59 6
            $view = $this->getViewWithMessage(null, 200, 'Your password reset link was sent to your e-mail address.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60 5
        } else {
61
            $view = $this->getViewWithMessage(null, 400, 'Cannot send an email.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62 1
        }
63
64
        return $this->handleView($view);
65 6
    }
66
67
    /**
68
    * Reset password view.
69
    *
70
    * @ApiDoc(
71
    *  description="Reset password view."
72
    * )
73
    * @Get("password/reset", options={"i18n"=false})
74
    */
75
    public function resetPasswordGetAction(Request $request): Response
76 3
    {
77
        $token = $request->query->get('token');
78 3
        $user  = $this->getEntityManager()->getRepository('Webcook\Cms\SecurityBundle\Entity\User')->findOneBy(array('passwordResetToken'=> $token));
79 3 View Code Duplication
        if ($user === null || empty($token)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80 3
            $view = $this->getViewWithMessage(null, 404, 'This token is invalid.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81 1
            return $this->handleView($view);
82 1
        }
83
84
        $dateDiff = date_diff(
85 2
            new \DateTime(),
86 2
            $user->getPasswordResetExpiration()
87 2
        );
88
89
        $view          = $this->getViewWithMessage(null, 400, 'This token has expired.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90 2
        $diffInSeconds = $dateDiff->i * 60 + $dateDiff->s;
91 2
        if ($diffInSeconds < 600 && $dateDiff->y == 0 && $dateDiff->m == 0 && $dateDiff->d == 0 && $dateDiff->h == 0) {
92 2
            $view = $this->getViewWithMessage(null, 200, 'Please enter your new password.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93 1
        }
94
95
        return $this->handleView($view);
96 2
    }
97
98
    /**
99
     * Reset password action.
100
     *
101
     * @ApiDoc(
102
     *  description="Reset password action."
103
     * )
104
     * @Post("password/reset", options={"i18n"=false})
105
     */
106
    public function resetPasswordPostAction(Request $request): Response
107 2
    {
108
        $password       = $request->request->get('password');
109 2
        $repeatPassword = $request->request->get('repeatPassword');
110 2
        $token          = $request->request->get('token');
111 2 View Code Duplication
        if (empty($password) || empty($repeatPassword) || empty($token)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112 2
            $view = $this->getViewWithMessage(null, 400, 'Passwords and token can\'t be empty.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113 1
            return $this->handleView($view);
114 1
        }
115
116
        if ($password == $repeatPassword) {
117 1
            $user = $this->getEntityManager()->getRepository('Webcook\Cms\SecurityBundle\Entity\User')->findOneBy(array('passwordResetToken'=> $token));
118 View Code Duplication
            if ($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                $view = $this->getViewWithMessage(null, 404, 'This token is invalid.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
                return $this->handleView($view);
121
            }
122
123
            $this->setUserPassword($user, $password);
124
125
            $view = $this->getViewWithMessage(null, 200, 'Password has been changed.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
            return $this->handleView($view);
127
        }
128
129
        $view = $this->getViewWithMessage(null, 400, 'Passwords dont\'t match.');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130 1
        return $this->handleView($view);
131 1
    }
132
133
    private function setUserPassword(User $user, String $password)
134
    {
135
        $factory  = $this->container->get('security.encoder_factory');
136
        $encoder  = $factory->getEncoder($user);
137
        $password = $encoder->encodePassword($password, $user->getSalt());
138
139
        $user->setPassword($password);
140
        $user->setPasswordResetToken(null);
141
        $user->setPasswordResetExpiration(null);
142
143
        $this->getEntityManager()->flush();
144
    }
145
146
    private function setResetToken(User $user): string
147 6
    {
148
        $token     = md5(uniqid(mt_rand(), true));
149 6
        $resetLink = $this->generateUrl('reset_password_get', array('token' => $token), true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150 6
        $date      = new \DateTime();
151 6
152
        $user->setPasswordResetToken($token);
153 6
        $user->setPasswordResetExpiration($date);
154 6
155
        $this->getEntityManager()->flush();
156 6
157
        return $resetLink;
158 6
    }
159
}
160