Passed
Pull Request — master (#23)
by
unknown
03:07
created

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
1
<?php
2
/**
3
 * StupidlySimple - A PHP Framework For Lazy Developers.
4
 *
5
 * @author		Fariz Luqman <[email protected]>
6
 * @copyright	2017 Fariz Luqman
7
 * @license		MIT
8
 *
9
 * @link		https://stupidlysimple.github.io/
10
 */
11
12
namespace Controller;
13
14
use Request;
15
use Response;
16
use Sentry;
0 ignored issues
show
Bug introduced by
The type Sentry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
class User
19
{
20
    public function __construct()
21
    {
22
        // Always check whether the user is logged in or not to do the action
23
        if (Sentry::check() !== true) {
24
            Response::redirect('login');
25
        }
26
    }
27
28
    public function editUser()
29
    {
30
        $id = Request::get('id');
31
        $first_name = Request::get('first_name');
32
        $last_name = Request::get('last_name');
33
        $password = Request::get('password');
34
35
        try {
36
            if ($password == '') {
37
                // change first_name and last_name
38
                $user = \Model\User::find($id);
39
                $user->first_name = $first_name;
40
                $user->last_name = $last_name;
41
                $user->save();
42
                Response::redirect('admin?edit=success');
43
            } else {
44
                // Find the user using the user id
45
                $user = Sentry::findUserById($id);
46
47
                // Get the password reset code
48
                $resetCode = $user->getResetPasswordCode();
49
50
                // Check if the reset password code is valid
51
                if ($user->checkResetPasswordCode($resetCode)) {
52
                    // Attempt to reset the user password
53
                    if ($user->attemptResetPassword($resetCode, $password)) {
54
                        // change first_name and last_name
55
                        $user = \Model\User::find($id);
56
                        $user->first_name = $first_name;
57
                        $user->last_name = $last_name;
58
                        $user->save();
59
60
                        Response::redirect('admin?edit=success');
61
                    } else {
62
                        // Password reset failed
63
                        echo 'Password reset failed';
64
                    }
65
                } else {
66
                    // The provided password reset code is Invalid
67
                    echo 'Invalid password reset code';
68
                }
69
            }
70
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Controller\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
71
            echo 'User was not found.';
72
        }
73
    }
74
75
    public function deleteUser()
76
    {
77
        $id = Request::get('id');
78
79
        try {
80
            $user = \Model\User::find($id);
81
82
            if ($user === null) {
83
                Response::redirect('admin?delete=failed');
84
            }
85
86
            $user->delete();
87
        } catch (Exception $e) {
88
            Response::redirect('admin?delete=failed');
89
        }
90
91
        Response::redirect('admin?delete=success');
92
    }
93
}
94