UsersController::confirm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
4
5
/**
6
 * UsersController Class
7
 *
8
 * Implements actions regarding user management
9
 */
10
class UsersController extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
13
    /**
14
     * Displays the form for account creation
15
     *
16
     * @return  Illuminate\Http\Response
17
     */
18
    public function create()
19
    {
20
        return View::make(Config::get('confide::signup_form'));
21
    }
22
23
    /**
24
     * Stores new account
25
     *
26
     * @return  Illuminate\Http\Response
27
     */
28
    public function store()
29
    {
30
        $repo = App::make('UserRepository');
31
        $user = $repo->signup(Input::all());
32
33
        if ($user->id) {
34
            if (Config::get('confide::signup_email')) {
35
                Mail::queueOn(
36
                    Config::get('confide::email_queue'),
37
                    Config::get('confide::email_account_confirmation'),
38
                    compact('user'),
39
                    function ($message) use ($user) {
40
                        $message
41
                            ->to($user->email, $user->username)
42
                            ->subject(Lang::get('confide::confide.email.account_confirmation.subject'));
43
                    }
44
                );
45
            }
46
47
            return Redirect::action('UsersController@login')
48
                ->with('notice', Lang::get('confide::confide.alerts.account_created'));
49
        } else {
50
            $error = $user->errors()->all(':message');
51
52
            return Redirect::action('UsersController@create')
53
                ->withInput(Input::except('password'))
54
                ->with('error', $error);
55
        }
56
    }
57
58
    /**
59
     * Displays the login form
60
     *
61
     * @return  Illuminate\Http\Response
62
     */
63
    public function login()
64
    {
65
        if (Confide::user()) {
66
            return Redirect::to('/');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Redirect::to('/'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by UsersController::login of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
        } else {
68
            return View::make(Config::get('confide::login_form'));
69
        }
70
    }
71
72
    /**
73
     * Attempt to do login
74
     *
75
     * @return  Illuminate\Http\Response
76
     */
77
    public function doLogin()
78
    {
79
        $repo = App::make('UserRepository');
80
        $input = Input::all();
81
82
        if ($repo->login($input)) {
83
            return Redirect::intended('/');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Redirect::intended('/'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by UsersController::doLogin of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
        } else {
85
            if ($repo->isThrottled($input)) {
86
                $err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
87
            } elseif ($repo->existsButNotConfirmed($input)) {
88
                $err_msg = Lang::get('confide::confide.alerts.not_confirmed');
89
            } else {
90
                $err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
91
            }
92
93
            return Redirect::action('UsersController@login')
94
                ->withInput(Input::except('password'))
95
                ->with('error', $err_msg);
96
        }
97
    }
98
99
    /**
100
     * Attempt to confirm account with code
101
     *
102
     * @param  string $code
103
     *
104
     * @return  Illuminate\Http\Response
105
     */
106
    public function confirm($code)
107
    {
108
        if (Confide::confirm($code)) {
109
            $notice_msg = Lang::get('confide::confide.alerts.confirmation');
110
            return Redirect::action('UsersController@login')
111
                ->with('notice', $notice_msg);
112
        } else {
113
            $error_msg = Lang::get('confide::confide.alerts.wrong_confirmation');
114
            return Redirect::action('UsersController@login')
115
                ->with('error', $error_msg);
116
        }
117
    }
118
119
    /**
120
     * Displays the forgot password form
121
     *
122
     * @return  Illuminate\Http\Response
123
     */
124
    public function forgotPassword()
125
    {
126
        return View::make(Config::get('confide::forgot_password_form'));
127
    }
128
129
    /**
130
     * Attempt to send change password link to the given email
131
     *
132
     * @return  Illuminate\Http\Response
133
     */
134
    public function doForgotPassword()
135
    {
136
        if (Confide::forgotPassword(Input::get('email'))) {
137
            $notice_msg = Lang::get('confide::confide.alerts.password_forgot');
138
            return Redirect::action('UsersController@login')
139
                ->with('notice', $notice_msg);
140
        } else {
141
            $error_msg = Lang::get('confide::confide.alerts.wrong_password_forgot');
142
            return Redirect::action('UsersController@doForgotPassword')
143
                ->withInput()
144
                ->with('error', $error_msg);
145
        }
146
    }
147
148
    /**
149
     * Shows the change password form with the given token
150
     *
151
     * @param  string $token
152
     *
153
     * @return  Illuminate\Http\Response
154
     */
155
    public function resetPassword($token)
156
    {
157
        return View::make(Config::get('confide::reset_password_form'))
158
                ->with('token', $token);
159
    }
160
161
    /**
162
     * Attempt change password of the user
163
     *
164
     * @return  Illuminate\Http\Response
165
     */
166
    public function doResetPassword()
167
    {
168
        $repo = App::make('UserRepository');
169
        $input = array(
170
            'token'                 =>Input::get('token'),
171
            'password'              =>Input::get('password'),
172
            'password_confirmation' =>Input::get('password_confirmation'),
173
        );
174
175
        // By passing an array with the token, password and confirmation
176
        if ($repo->resetPassword($input)) {
177
            $notice_msg = Lang::get('confide::confide.alerts.password_reset');
178
            return Redirect::action('UsersController@login')
179
                ->with('notice', $notice_msg);
180
        } else {
181
            $error_msg = Lang::get('confide::confide.alerts.wrong_password_reset');
182
            return Redirect::action('UsersController@resetPassword', array('token'=>$input['token']))
183
                ->withInput()
184
                ->with('error', $error_msg);
185
        }
186
    }
187
188
    /**
189
     * Log the user out of the application.
190
     *
191
     * @return  Illuminate\Http\Response
192
     */
193
    public function logout()
194
    {
195
        Confide::logout();
196
197
        return Redirect::to('/');
198
    }
199
}
200