Passed
Push — master ( 17b30a...9c28de )
by Mihail
14:15
created

ActionSignup::signup()   C

Complexity

Conditions 11
Paths 15

Size

Total Lines 76
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 37
nc 15
nop 0
dl 0
loc 76
rs 5.4429
c 0
b 0
f 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\User;
4
5
use Apps\ActiveRecord\Invite;
6
use Apps\Model\Front\User\FormLogin;
7
use Apps\Model\Front\User\FormRegister;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionSignup
17
 * @package Apps\Controller\Front\User
18
 * @property View $view
19
 * @property Response $response
20
 * @property Request $request
21
 * @method array getConfigs()
22
 */
23
trait ActionSignup
24
{
25
26
    /**
27
     * View register form and process submit action
28
     * @throws ForbiddenException
29
     * @throws \Ffcms\Core\Exception\SyntaxException
30
     */
31
    public function signup(): ?string
32
    {
33
        // check if user is authorized
34
        if (App::$User->isAuth()) {
35
            throw new ForbiddenException(__('You are always authorized on website, registration not allowed'));
36
        }
37
38
        // load configs
39
        $configs = $this->getConfigs();
40
41
        // init register model
42
        $registerForm = new FormRegister($configs['captchaOnRegister'] === 1);
43
44
        // registration based on invite. Check conditions.
45
        if ($configs['registrationType'] === 0) {
46
            // get token and email
47
            $inviteToken = $this->request->query->get('token');
48
            $inviteEmail = $this->request->query->get('email');
49
            // check if token length & email is valid format
50
            if (Str::length($inviteToken) < 32 || !Str::isEmail($inviteEmail)) {
51
                throw new ForbiddenException(__('Registration allowed only if you have invite!'));
52
            }
53
54
            // remove deprecated data
55
            Invite::clean();
56
            // try to find token
57
            $find = Invite::where('token', $inviteToken)
58
                ->where('email', $inviteEmail)
59
                ->count();
60
61
            // token not foud? invalid invite key
62
            if ($find !== 1) {
63
                throw new ForbiddenException(__('Your invite token is invalid! Contact with administrator'));
64
            }
65
66
            // notify the invite token is accepted
67
            if (!$registerForm->send()) {
68
                App::$Session->getFlashBag()->add('success', __('Invite was accepted! Continue registration'));
69
            }
70
71
            // set email from token data
72
            $registerForm->email = $inviteEmail;
73
        }
74
75
        // if register data is send and valid
76
        if ($registerForm->send() && $registerForm->validate()) {
77
            $activation = $configs['registrationType'] === 1;
78
            if ($registerForm->tryRegister($activation)) {
79
                // initialize succes signup event
80
                App::$Event->run(static::EVENT_USER_REGISTER_SUCCESS, [
81
                    'model' => $registerForm
82
                ]);
83
                // if no activation is required - just open session and redirect user to main page
84
                if (!$activation) {
85
                    $loginModel = new FormLogin();
86
                    $loginModel->openSession($registerForm->_userObject);
0 ignored issues
show
Bug introduced by
It seems like $registerForm->_userObject can be null; however, openSession() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
87
                    $this->response->redirect('/'); // session is opened, refresh page
88
                }
89
                // send notification of successful registering
90
                App::$Session->getFlashBag()->add('success', __('Your account is registered. You must confirm account via email'));
91
            } else {
92
                // init fail signup event
93
                App::$Event->run(static::EVENT_USER_REGISTER_FAIL, [
94
                    'model' => $registerForm
95
                ]);
96
                App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
97
            }
98
        }
99
100
        // render view
101
        return $this->view->render('signup', [
102
            'model' => $registerForm,
103
            'config' => $configs,
104
            'useCaptcha' => $configs['captchaOnRegister'] === 1
105
        ]);
106
    }
107
}
108