UserController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 153
Duplicated Lines 20.26 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 10
dl 31
loc 153
ccs 0
cts 136
cp 0
rs 10
c 0
b 0
f 0

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
namespace App\Controllers;
4
5
use App\Config;
6
use App\User;
7
use App\Template;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Swift_Message;
11
use Zend\Diactoros\Response\RedirectResponse;
12
13
class UserController extends Base
14
{
15
16 View Code Duplication
    public function loginForm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
17
    {
18
        $template = new Template('login.twig');
19
        $template->title = 'Log in';
20
        $template->name = $this->getQueryParam($request, 'name');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
21
        $response->getBody()->write($template->render());
22
        return $response;
23
    }
24
25
    public function login(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
login uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
    {
27
        $name = $this->getBodyParam($request, 'name');
28
        $nameQueryString = $name ? "?name=$name" : '';
29
        if ($this->getBodyParam($request, 'register')) {
30
            return new RedirectResponse($this->config->baseUrl() . '/register' . $nameQueryString);
31
        }
32
        if ($this->getBodyParam($request, 'remind')) {
33
            return new RedirectResponse($this->config->baseUrl() . '/remind' . $nameQueryString);
34
        }
35
        $template = new Template('login.twig');
36
        $user = $this->db->query('SELECT id, password FROM users WHERE name=:n', ['n' => $name])->fetch();
37
        if (isset($user->password)) {
38
            if (password_verify($this->getBodyParam($request, 'password'), $user->password)) {
39
                session_regenerate_id(true);
40
                $_SESSION['userid'] = $user->id;
41
                return new RedirectResponse($this->config->baseUrl());
42
            }
43
        }
44
        $template->alert('warning', 'Acess denied.', true);
45
        return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name);
46
    }
47
48
    public function logout(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        session_unset();
51
        session_destroy();
52
        session_write_close();
53
        setcookie(session_name(), '', 0, '/');
54
        session_regenerate_id(true);
55
        return new RedirectResponse($this->config->baseUrl() . '/login');
56
    }
57
58
    public function registerForm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        $template = new Template('register.twig');
61
        $template->title = 'Register';
62
        $template->name = $this->getAnyParam($request, 'name');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
        $template->email = $this->getBodyParam($request, 'email');
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
        $response->getBody()->write($template->render());
65
        return $response;
66
    }
67
68
    public function register(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $name = $this->getBodyParam($request, 'name');
71
        $email = $this->getBodyParam($request, 'email');
72
        $template = new Template('register.twig');
73
        if ($this->getBodyParam($request, 'login')) {
74
            return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name);
75
        }
76
        $password = $this->getBodyParam($request, 'password');
77 View Code Duplication
        if ($password !== $this->getBodyParam($request, 'password-confirmation')) {
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...
78
            $template->alert('warning', 'Your passwords did not match.', true);
79
            return new RedirectResponse($this->config->baseUrl() . "/register?name=$name&email=$email");
80
        }
81
        $user = new User($this->db);
82
        $user->register($name, $email, $password);
83
        $template->alert('success', 'Thank you for registering.', true);
84
        return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $user->getName());
85
    }
86
87 View Code Duplication
    public function remindForm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        $template = new Template('remind.twig');
90
        $template->title = 'Remind';
91
        $template->name = $this->getBodyParam($request, 'name');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
        $response->getBody()->write($template->render());
93
        return $response;
94
    }
95
96
    public function remind(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    {
98
        $name = $this->getBodyParam($request, 'name');
99
        if ($this->getBodyParam($request, 'login')) {
100
            return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name);
101
        }
102
        $config = new Config();
103
        $user = new User($this->db);
104
        $user->loadByName($name);
105
        $template = new Template('remind_email.twig');
106
        if (!empty($user->getEmail())) {
107
            $template->user = $user;
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
            $template->token = $user->getReminder();
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
            $message = Swift_Message::newInstance()
110
                ->setSubject('Password reminder')
111
                ->setFrom(array($config->siteEmail() => $config->siteTitle()))
112
                ->setTo(array($user->getEmail() => $user->getName()))
113
                ->setBody($template->render(), 'text/html');
114
            $this->email($message);
115
        } else {
116
            // Pause for a moment, so it's not so obvious which users' names are resulting in mail being sent.
117
            sleep(5);
118
        }
119
        $template->alert('success', 'Please check your email', true);
120
        return new RedirectResponse($this->config->baseUrl() . '/remind?name=' . $name);
121
    }
122
123
    public function remindResetForm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125
        if (!isset($args['token'])) {
126
            return new RedirectResponse($this->config->baseUrl() . '/remind');
127
        }
128
        $template = new Template('remind_reset.twig');
129
        $template->title = 'Password Reset';
130
        $template->userid = $args['userid'];
0 ignored issues
show
Documentation introduced by
The property userid does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
        $template->token = $args['token'];
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<App\Template>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
        $response->getBody()->write($template->render());
133
        return $response;
134
    }
135
136
    public function remindReset(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        $template = new Template('remind_reset.twig');
139
        // First check that the passwords match.
140
        $password = $request->getAttribute('password');
141
        if ($password !== $request->getAttribute('password-confirmation')) {
142
            $template->alert('warning', 'Your passwords did not match.', true);
143
            return new RedirectResponse($this->config->baseUrl() . "/remind/" . $args['userid'] . "/" . $args['token']);
144
        }
145
        // Then see if the token is valid.
146
        $user = new User($this->db);
147
        $user->load($args['userid']);
148 View Code Duplication
        if (!$user->checkReminderToken($args['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...
149
            $template->alert('warning', 'That reminder token has expired. Please try again.', true);
150
            return new RedirectResponse($this->config->baseUrl() . "/remind");
151
        }
152
        // Finally change the password. This will delete the token as well.
153
        $user->changePassword($password);
154
        $template->alert('success', 'Your password has been changed. Please log in.', true);
155
        return new RedirectResponse($this->config->baseUrl() . "/login?name=" . $user->getName());
156
    }
157
158 View Code Duplication
    public function profile(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
159
    {
160
        $template = new Template('profile.twig');
161
        $template->title = 'Profile';
162
        $response->getBody()->write($template->render());
163
        return $response;
164
    }
165
}
166