Register::onError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Register Class Doc Comment
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  Registration-Module
9
 * @author   Ankit Jain <[email protected]>
10
 * @license  The MIT License (MIT)
11
 * @link     https://github.com/ankitjain28may/registration-module
12
 */
13
14
namespace AnkitJain\RegistrationModule;
15
use AnkitJain\RegistrationModule\Validate;
16
use AnkitJain\RegistrationModule\Session;
17
require_once dirname(__DIR__) . '/config/database.php';
18
19
/**
20
 * For Register the New User
21
 *
22
 * @category PHP
23
 * @package  Registration-Module
24
 * @author   Ankit Jain <[email protected]>
25
 * @license  The MIT License (MIT)
26
 * @link     https://github.com/ankitjain28may/registration-module
27
 */
28
29
class Register
30
{
31
    /*
32
    |--------------------------------------------------------------------------
33
    | Register Class
34
    |--------------------------------------------------------------------------
35
    |
36
    | For Register the New User.
37
    |
38
    */
39
40
    protected $error;
41
    protected $flag;
42
    protected $obValidate;
43
    protected $connect;
44
45
    /**
46
     * Create a new controller instance.
47
     *
48
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
49
     */
50
    public function __construct()
51
    {
52
        $this->error = array();
53
        $this->flag = 0;
54
        $this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
55
        $this->obValidate = new Validate();
56
    }
57
58
    /**
59
     * Credentials check for allowing new user to Register
60
     *
61
     * @param array $data Contains the User Credentials
62
     *
63
     * @return json
64
     */
65
    public function authRegister($data)
66
    {
67
        $data = $this->emptyValue($data);
68
        $name = $data["name"];
69
        $email = $data["email"];
70
        $username = $data["username"];
71
        $mob = $data["mob"];
72
        $password = $data["passRegister"];
73
        $userId = '';
0 ignored issues
show
Unused Code introduced by
$userId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
75
        if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
76
            $this->onError("email", " *Enter correct Email address");
77
        } elseif ($this->obValidate->validateEmailInDb($email) === 1) {
78
            $this->onError("email", " *Email is already registered");
79
        }
80
81
        if ($this->obValidate->validateUsernameInDb($username) === 1) {
82
            $this->onError("username", " *Username is already registered");
83
        }
84
85
        if (!preg_match("/^[0-9]{10}$/", $data["mob"])) {
86
            $this->onError("mob", " *Enter correct Mobile Number");
87
        }
88
89
        if ($this->flag == 1) {
90
            return json_encode($this->error);
91
        }
92
93
        $password = md5($password);
94
95
        $query = "INSERT INTO register VALUES(
96
        	null, '$email', '$username', '$password'
97
        )";
98 View Code Duplication
        if (!$this->connect->query($query)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
            return json_encode(
100
                [
101
                "Error" => "You are not registered, " . $this->connect->error
102
                ]
103
            );
104
        }
105
        $query = "SELECT id FROM register WHERE email = '$email'";
106
        if ($result = $this->connect->query($query)) {
107
            $row = $result->fetch_assoc();
108
            $userId = $row['id'];
109
            $query = "INSERT INTO login VALUES(
110
            	'$userId', '$name', '$email', '$username', '$mob'
111
            )";
112
113 View Code Duplication
            if (!$this->connect->query($query)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
                return json_encode(
115
                    [
116
                    "Error" => "You are not registered, " . $this->connect->error
117
                    ]
118
                );
119
            }
120
121
            Session::put('start', $userId);
122
            return json_encode(
123
                [
124
                "location" => URL . "/account.php"
125
                ]
126
            );
127
        }
128
    }
129
130
    /**
131
     * For generating Error array by key value pair
132
     *
133
     * @param string $key   Contains key
134
     * @param string $value Contains the Value for the key
135
     *
136
     * @return void
137
     */
138 View Code Duplication
    public function onError($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $this->flag = 1;
141
        $this->error = array_merge(
142
            $this->error,
143
            [
144
            [
145
            "key" => $key,
146
            "value" => $value
147
            ]
148
            ]
149
        );
150
    }
151
152
    /**
153
     * For checking whether the credentials are empty or not
154
     *
155
     * @param array $data Contains the Credentials
156
     *
157
     * @return array
158
     */
159
    public function emptyValue($data)
160
    {
161
        $errorCode = array(
162
            "name" => " *Enter the name",
163
            "email" => " *Enter the email address",
164
            "username" => " *Enter the username",
165
            "passRegister" => " *Enter the password",
166
            "mob" => " *Enter the Mobile Number"
167
        );
168
169 View Code Duplication
        foreach ($data as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
            $data[$key] = trim($data[$key]);
171
            $value = trim($value);
172
            if (empty($value)) {
173
                $this->onError($key, $errorCode[$key]);
174
            }
175
        }
176
        return $data;
177
    }
178
}
179