Passed
Push — master ( b7618a...8c3cc9 )
by Mihail
15:19
created

User::actionSignup()   C

Complexity

Conditions 10
Paths 12

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 56
rs 6.7742
cc 10
eloc 27
nc 12
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\Invite;
6
use Apps\ActiveRecord\UserRecovery;
7
use Apps\Model\Front\User\FormRecovery;
8
use Apps\Model\Front\User\FormRegister;
9
use Extend\Core\Arch\FrontAppController;
10
use Ffcms\Core\App;
11
use Apps\Model\Front\User\FormLogin;
12
use Ffcms\Core\Exception\ForbiddenException;
13
use Ffcms\Core\Exception\NotFoundException;
14
use Ffcms\Core\Helper\Type\Obj;
15
use Ffcms\Core\Helper\Type\Str;
16
17
/**
18
 * Class User - standard user controller: login/signup/logout/etc
19
 * @package Apps\Controller\Front
20
 */
21
class User extends FrontAppController
22
{
23
    /**
24
     * View login form and process submit action
25
     * @throws ForbiddenException
26
     */
27
    public function actionLogin()
28
    {
29
        if (App::$User->isAuth()) { // always auth? get the f*ck out
30
            throw new ForbiddenException();
31
        }
32
33
        $configs = $this->getConfigs();
34
        // load login model
35
        $loginForm = new FormLogin($configs['captchaOnLogin'] === 1);
36
37
        // check if data is send and valid
38
        if ($loginForm->send() && $loginForm->validate()) {
39
            if ($loginForm->tryAuth()) {
40
                App::$Response->redirect('/'); // void header change & exit()
41
            }
42
            App::$Session->getFlashBag()->add('error', __('User is never exist or password is incorrect!'));
43
        }
44
45
        // render view
46
        $this->response = App::$View->render('login', [
47
            'model' => $loginForm->export(),
48
            'useCaptcha' => $configs['captchaOnLogin'] === 1
49
        ]);
50
    }
51
52
    /**
53
     * View register form and process submit action
54
     * @throws ForbiddenException
55
     */
56
    public function actionSignup()
57
    {
58
        if (App::$User->isAuth()) { // always auth? prevent any actions
59
            throw new ForbiddenException();
60
        }
61
62
        // load configs
63
        $configs = $this->getConfigs();
64
65
        // init register model
66
        $registerForm = new FormRegister($configs['captchaOnRegister'] === 1);
67
68
        // registration based on invite. Check conditions.
69
        if ($configs['registrationType'] === 0) {
70
            // get token and email
71
            $inviteToken = App::$Request->query->get('token');
72
            $inviteEmail = App::$Request->query->get('email');
73
            // data sounds like a invalid?
74
            if (Str::length($inviteToken) < 32 || !Str::isEmail($inviteEmail)) {
75
                throw new ForbiddenException(__('Registration allowed only if you have invite!'));
76
            }
77
            // remove oldest data
78
            Invite::clean();
79
            // try to find token
80
            $find = Invite::where('token', '=', $inviteToken)
81
                ->where('email', '=', $inviteEmail)->count();
82
83
            // token not foud? invalid invite key
84
            if ($find !== 1) {
85
                throw new ForbiddenException(__('Your invite token is invalid! Contact with administrator'));
86
            }
87
            // notify the invite token is accepted
88
            if (!$registerForm->send()) {
89
                App::$Session->getFlashBag()->add('success', __('Invite was accepted! Continue registration'));
90
            }
91
92
            // set email from token data
93
            $registerForm->email = $inviteEmail;
94
        }
95
96
        // if register data is send and valid
97
        if ($registerForm->send() && $registerForm->validate()) {
98
            if ($registerForm->tryRegister($configs['registrationType'] === 1)) {
99
                App::$Session->getFlashBag()->add('success', __('Your account is registered. You must confirm account via email'));
100
            } else {
101
                App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
102
            }
103
        }
104
105
        // render view
106
        $this->response = App::$View->render('signup', [
107
            'model' => $registerForm->export(),
108
            'config' => $configs,
109
            'useCaptcha' => $configs['captchaOnRegister'] === 1
110
        ]);
111
    }
112
113
    /**
114
     * Recovery form and recovery submit action
115
     * @param int|null $id
116
     * @param string|null $token
117
     * @throws ForbiddenException
118
     * @throws NotFoundException
119
     * @throws \Ffcms\Core\Exception\SyntaxException
120
     */
121
    public function actionRecovery($id = null, $token = null)
122
    {
123
        if (App::$User->isAuth()) { // always auth? prevent any actions
124
            throw new ForbiddenException();
125
        }
126
127
        // is recovery submit?
128
        if (Obj::isLikeInt($id) && Str::length($token) >= 64) {
129
            $rObject = UserRecovery::where('id', '=', $id)
130
                ->where('token', '=', $token)
131
                ->where('archive', '=', false);
132
            // check if recovery row exist
133
            if ($rObject->count() !== 1) {
134
                throw new NotFoundException('This recovery data is not found');
135
            }
136
137
            $rData = $rObject->first();
138
            // check if user with this "user_id" in recovery row exist
139
            $rUser = App::$User->identity($rData->user_id);
140
            if ($rUser === null) {
141
                throw new NotFoundException('User is not found');
142
            }
143
144
            // all is ok, lets set new pwd
145
            $rUser->password = $rData->password;
0 ignored issues
show
Bug introduced by
Accessing password on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
146
            $rUser->save();
147
148
            $rData->archive = true;
149
            $rData->save();
150
151
            // add notification
152
            App::$Session->getFlashBag()->add('success', __('Your account are successful recovered. We recommend you change password'));
153
154
            // lets open user session with recovered data
155
            $loginModel = new FormLogin();
156
            $loginModel->openSession($rUser);
157
            App::$Response->redirect('/'); // session is opened, refresh page
158
        }
159
160
        // lets work with recovery form data
161
        $model = new FormRecovery();
162
        if ($model->send()) {
163
            if ($model->validate()) {
164
                $model->make();
165
                App::$Session->getFlashBag()->add('success', __('We send to you email with instruction to recovery your account'));
166
            } else {
167
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
168
            }
169
        }
170
171
        // render visual form content
172
        $this->response = App::$View->render('recovery', [
173
            'model' => $model
174
        ]);
175
    }
176
177
    /**
178
     * Make logout if user is signIn
179
     * @throws ForbiddenException
180
     */
181
    public function actionLogout()
182
    {
183
        if (!App::$User->isAuth()) { // not auth? what you wanna?
184
            throw new ForbiddenException();
185
        }
186
187
        // unset session data
188
        App::$Session->invalidate();
189
190
        // redirect to main
191
        App::$Response->redirect('/');
192
    }
193
194
    /**
195
     * Approve user profile via $email and $token params
196
     * @param $email
197
     * @param $token
198
     * @throws ForbiddenException
199
     */
200
    public function actionApprove($email, $token)
201
    {
202
        // sounds like a not valid token
203
        if (App::$User->isAuth() || Str::length($token) < 32 || !Str::isEmail($email)) {
204
            throw new ForbiddenException();
205
        }
206
        // lets find token&email
207
        $find = App::$User->where('approve_token', '=', $token)
0 ignored issues
show
Bug introduced by
The method where() does not seem to exist on object<Ffcms\Core\Interfaces\iUser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
208
            ->where('email', '=', $email);
209
210
        // not found? exit
211
        if ($find->count() !== 1) {
212
            throw new ForbiddenException();
213
        }
214
215
        // get row and update approve information
216
        $user = $find->first();
217
        $user->approve_token = '0';
218
        $user->save();
219
220
        // open session and redirect to main
221
        $loginModel = new FormLogin();
222
        $loginModel->openSession($user);
223
        App::$Response->redirect('/'); // session is opened, refresh page
224
    }
225
}