Login::onError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Login Class Doc Comment
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  OpenChat
9
 * @author   Ankit Jain <[email protected]>
10
 * @license  The MIT License (MIT)
11
 * @link     https://github.com/ankitjain28may/openchat
12
 */
13
namespace ChatApp;
14
15
require_once dirname(__DIR__).'/vendor/autoload.php';
16
use ChatApp\Session;
17
use mysqli;
18
use Dotenv\Dotenv;
19
$dotenv = new Dotenv(dirname(__DIR__));
20
$dotenv->load();
21
22
/**
23
 * To Login the User
24
 *
25
 * @category PHP
26
 * @package  OpenChat
27
 * @author   Ankit Jain <[email protected]>
28
 * @license  The MIT License (MIT)
29
 * @link     https://github.com/ankitjain28may/openchat
30
 */
31
class Login
32
{
33
    /*
34
    |--------------------------------------------------------------------------
35
    | Login Class
36
    |--------------------------------------------------------------------------
37
    |
38
    | To Login the User.
39
    |
40
    */
41
42
    protected $flag;
43
    protected $error;
44
    protected $connect;
45
46
    /**
47
     * Create a new class instance.
48
     *
49
     * @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...
50
     */
51 View Code Duplication
    public function __construct()
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...
52
    {
53
        $this->flag = 0;
54
        $this->connect = new mysqli(
55
            getenv('DB_HOST'),
56
            getenv('DB_USER'),
57
            getenv('DB_PASSWORD'),
58
            getenv('DB_NAME')
59
        );
60
        $this->error = array();
61
    }
62
63
    /**
64
     * To Authenticate User Credentials
65
     *
66
     * @param array $data To store User Credentials
67
     *
68
     * @return string
69
     */
70
    public function authLogin($data)
71
    {
72
        $data = $this->emptyValue($data);
73
74
        $login = $data["login"];
75
        $password = $data["passLogin"];
76
77
        if (preg_match("/^.+[@]{1}.+$/", $login)) {
78
            if (filter_var($login, FILTER_VALIDATE_EMAIL) == false) {
79
                $this->onError("login", " *Enter correct Email address");
80
            }
81
        }
82
83
        if ($this->flag == 0) {
84
            $password = md5($password);
85
            $query = "SELECT * FROM login WHERE email = '$login' or
86
                        username = '$login'";
87
            if ($result = $this->connect->query($query)) {
88
                if ($result->num_rows > 0) {
89
                    $row = $result->fetch_assoc();
90
                    $loginID = $row['login_id'];
91
                    $query = "SELECT id FROM register WHERE id = '$loginID' and
92
                                password = '$password'";
93
94
                    if ($result = $this->connect->query($query)) {
95
                        if ($result->num_rows > 0) {
96
                            Session::put('start', $loginID);
97
                            return json_encode(
98
                                [
99
                                "location" => getenv('APP_URL')."/views/account.php"
100
                                ]
101
                            );
102
                        }
103
                        $this->onError("passLogin", " *Invalid password");
104
                        return json_encode($this->error);
105
                    }
106
                    return json_encode(
107
                        ["Error" => "You are not registered, ".$this->connect->error]
108
                    );
109
                }
110
                $this->onError("login", " *Invalid username or email");
111
                return json_encode($this->error);
112
            }
113
            return json_encode(
114
                ["Error" => "You are not registered, ".$this->connect->error]
115
            );
116
        } else {
117
            return json_encode($this->error);
118
        }
119
    }
120
121
    /**
122
     * For Adding Error statements
123
     *
124
     * @param string $key   To store Key
125
     * @param string $value To store Key
126
     *
127
     * @return void
128
     */
129 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...
130
    {
131
        $this->flag = 1;
132
        $this->error = array_merge(
133
            $this->error,
134
            [["key" => $key, "value" => $value]]
135
        );
136
    }
137
138
    /**
139
     * For Traversing data to check for error
140
     *
141
     * @param array $data To store Data
142
     *
143
     * @return array
144
     */
145
    public function emptyValue($data)
146
    {
147
        $errorCode = array(
148
            "login" => " *Enter the login field",
149
            "passLogin" => " *Enter the password"
150
        );
151
152 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...
153
            $data[$key] = trim($data[$key]);
154
            $value = trim($value);
155
            if (empty($value)) {
156
                $this->onError($key, $errorCode[$key]);
157
            }
158
        }
159
        return $data;
160
    }
161
162
}
163
164