Signup::signup()   F
last analyzed

Complexity

Conditions 15
Paths 1008

Size

Total Lines 80
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 80
rs 1.7499
c 0
b 0
f 0
cc 15
nc 1008
nop 4

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
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq (Malik) <[email protected]>
7
 *
8
 * @link https://github.com/alphazframework/framework
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 * @since 1.0.0
13
 *
14
 * @license MIT
15
 */
16
17
namespace alphaz\Auth;
18
19
use alphaz\Common\PasswordManipulation;
20
use alphaz\Database\Db as DB;
21
use alphaz\Hashing\Hash;
22
use alphaz\Site\Site;
23
use alphaz\Validation\Validation;
24
25
class Signup extends Handler
26
{
27
    /**
28
     * Store the error msgs.
29
     *
30
     * @since 1.0.0
31
     *
32
     * @var array
33
     */
34
    protected $errors = [];
35
36
    /**
37
     * Signup the users.
38
     *
39
     * @param (mixed) $username username of user
40
     * @param (mixed) $email    email of user
41
     * @param (mixed) $password password of users
42
     * @param (array) $params   extra field like [name => value] array
43
     *
44
     * @since 1.0.0
45
     *
46
     * @return bool
47
     */
48
    public function signup($username, $email, $password, $params)
49
    {
50
        $rules = [
51
            'username' => ['required' => true],
52
            'email'    => ['required' => true, 'email' => true],
53
            'password' => ['required' => true],
54
        ];
55
        $inputs = [
56
            'username' => $username,
57
            'email'    => $email,
58
            'password' => $password,
59
        ];
60
        $requireValidate = new Validation($inputs, $rules);
61
        if ($requireValidate->fail()) {
62
            Error::set($requireValidate->error()->get());
63
        }
64
        $uniqueUsername = new Validation(['field'=> 'username', 'value'=>$username], __config()->auth->db_table, 'database');
0 ignored issues
show
Bug introduced by
The call to __config() has too few arguments starting with key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $uniqueUsername = new Validation(['field'=> 'username', 'value'=>$username], /** @scrutinizer ignore-call */ __config()->auth->db_table, 'database');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
        if ($uniqueUsername->fail()) {
66
            Error::set($uniqueUsername->error()->get());
67
        }
68
        $uniqueEmail = new Validation(['field'=> 'email', 'value'=>$email], __config()->auth->db_table, 'database');
69
        if ($uniqueEmail->fail()) {
70
            Error::set($uniqueEmail->error()->get());
71
        }
72
        if (is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
73
            foreach (array_keys($params) as $key => $value) {
74
                $paramsRules = [$value => ['required' => true]];
75
            }
76
            $paramsValidate = new Validation($params, $paramsRules);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $paramsRules seems to be defined by a foreach iteration on line 73. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
77
            if ($paramsValidate->fail()) {
78
                Error::set($paramsValidate->error()->get());
79
            }
80
            if (isset($params['passConfirm'])) {
81
                if ($password !== $params['passConfirm']) {
82
                    Error::set(__printl('auth:error:password:confirm'), 'password');
83
                }
84
            }
85
        }
86
        if (__config()->auth->sticky_password) {
87
            if (!(new PasswordManipulation())->isValid($password)) {
88
                Error::set(__printl('auth:error:password:sticky'), 'password');
89
            }
90
        }
91
        if (!(new User())->isLogin()) {
92
            if ($this->fail() !== true) {
93
                $salts = (new Site())::salts(12);
94
                $password_hash = Hash::make($password);
95
                if (__config()->auth->is_verify_email) {
96
                    $token = (new Site())::salts(8);
97
                } else {
98
                    $token = 'NULL';
99
                }
100
                $param = [
101
                    'username' => $username,
102
                    'email'    => $email,
103
                    'password' => $password_hash,
104
                    'salts'    => $salts,
105
                    'token'    => $token,
106
                ];
107
                $fields = [
108
                    'db_name' => __config()->auth->db_name,
109
                    'table'   => __config()->auth->db_table,
110
                ];
111
                unset($params['passConfirm']);
112
                $data = ['columns' => array_merge($param, $params)];
113
                $values = array_merge($fields, $data);
114
                $db = new DB();
115
                Success::set($db->db()->insert($values));
116
                $db->db()->close();
117
                if (__config()->auth->is_verify_email === true) {
118
                    $subject = __printl('auth:subject:need:verify');
119
                    $link = site_base_url().__config()->auth->verification_link.'/'.$token;
120
                    $html = __printl('auth:body:need:verify');
121
                    $html = str_replace(':email', $email, $html);
122
                    $html = str_replace(':link', $link, $html);
123
                    new EmailHandler($subject, $html, $email);
0 ignored issues
show
Bug introduced by
$html of type string is incompatible with the type alphaz\Auth\mixeds expected by parameter $html of alphaz\Auth\EmailHandler::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                    new EmailHandler($subject, /** @scrutinizer ignore-type */ $html, $email);
Loading history...
124
                }
125
            }
126
        } else {
127
            Error::set(__printl('auth:error:already:login'), 'login');
128
        }
129
    }
130
}
131