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

ActionSignup   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C signup() 0 76 11
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