| Conditions | 13 |
| Paths | 164 |
| Total Lines | 158 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 102 | public function attemp() |
||
| 103 | { |
||
| 104 | # data to send |
||
| 105 | $data = []; |
||
| 106 | |||
| 107 | $post = $this->getPost(); |
||
| 108 | $this->setTerminal(true); |
||
| 109 | |||
| 110 | # TRY-CATCH-BLOCK |
||
| 111 | try { |
||
| 112 | |||
| 113 | # STANDARD VALIDATIONS [check method] |
||
| 114 | if (!$this->isPost()) |
||
| 115 | { |
||
| 116 | $http = new Http(); |
||
| 117 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 118 | |||
| 119 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 120 | } |
||
| 121 | |||
| 122 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 123 | $needles = ['username', 'password']; |
||
| 124 | |||
| 125 | array_walk($needles, function(&$item) use ($post) { |
||
| 126 | if (!array_key_exists($item, $post)) |
||
| 127 | { |
||
| 128 | $http = new Http(); |
||
| 129 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 130 | |||
| 131 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 132 | } |
||
| 133 | }); |
||
| 134 | |||
| 135 | $this->checkSession(); |
||
| 136 | |||
| 137 | $components = [ |
||
| 138 | "attributes" => [ |
||
| 139 | "username" => [ |
||
| 140 | "required" => true, |
||
| 141 | "type" => "text", |
||
| 142 | "minlength" => 4, |
||
| 143 | "maxlength" => 20 |
||
| 144 | ], |
||
| 145 | "password" => [ |
||
| 146 | "required" => true, |
||
| 147 | "type" => "text", |
||
| 148 | "minlength" => 4, |
||
| 149 | "maxlength" => 20 |
||
| 150 | ] |
||
| 151 | ], |
||
| 152 | ]; |
||
| 153 | |||
| 154 | $options = [ |
||
| 155 | "username" => [ |
||
| 156 | "label" => "Username", |
||
| 157 | "validators" => [ |
||
| 158 | "Alnum" => ["allowWhiteSpace" => false] |
||
| 159 | ] |
||
| 160 | ], |
||
| 161 | "password" => [ |
||
| 162 | "label" => "Password" |
||
| 163 | ] |
||
| 164 | ]; |
||
| 165 | |||
| 166 | $form = new Form($components); |
||
| 167 | $form->fill($post); |
||
| 168 | |||
| 169 | $validator = new FormValidator($form, $options); |
||
| 170 | $validator->validate(); |
||
| 171 | |||
| 172 | $data["validator"] = $validator; |
||
| 173 | |||
| 174 | # STANDARD VALIDATIONS [check argument constraints] |
||
| 175 | if (!$validator->isValid()) |
||
| 176 | { |
||
| 177 | $data["messages"] = $validator->getMessages(); |
||
| 178 | throw new \Drone\Exception\Exception("Form validation errors!"); |
||
| 179 | } |
||
| 180 | |||
| 181 | $config = include 'module/Auth/config/user.config.php'; |
||
| 182 | |||
| 183 | $username_str = $config["authentication"]["gateway"]["credentials"]["username"]; |
||
| 184 | $password_str = $config["authentication"]["gateway"]["credentials"]["password"]; |
||
| 185 | |||
| 186 | $row = $this->getUsersAdapter()->select([ |
||
| 187 | "$username_str" => $post["username"] |
||
| 188 | ]); |
||
| 189 | |||
| 190 | if (!count($row)) |
||
| 191 | throw new \Drone\Exception\Exception("Username or password are incorrect"); |
||
| 192 | |||
| 193 | $user = array_shift($row); |
||
| 194 | |||
| 195 | $securePass = $user->{$password_str}; |
||
| 196 | $password = $post["password"]; |
||
| 197 | |||
| 198 | if ($user->USER_STATE_ID == 1) |
||
| 199 | throw new \Drone\Exception\Exception("User pending of email checking!"); |
||
| 200 | |||
| 201 | $bcrypt = new Bcrypt(); |
||
| 202 | |||
| 203 | if (!$bcrypt->verify($password, $securePass)) |
||
| 204 | throw new \Drone\Exception\Exception("Username or password are incorrect"); |
||
| 205 | |||
| 206 | $key = $config["authentication"]["key"]; |
||
| 207 | $method = $config["authentication"]["method"]; |
||
| 208 | |||
| 209 | switch ($method) |
||
| 210 | { |
||
| 211 | case '_COOKIE': |
||
| 212 | setcookie($key, $user->USERNAME, time() + 2000000000, '/'); |
||
| 213 | break; |
||
| 214 | |||
| 215 | case '_SESSION': |
||
| 216 | $_SESSION[$key] = $user->USERNAME; |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | |||
| 220 | # SUCCESS-MESSAGE |
||
| 221 | $data["process"] = "success"; |
||
| 222 | } |
||
| 223 | catch (\Drone\Exception\Exception $e) |
||
| 224 | { |
||
| 225 | # ERROR-MESSAGE |
||
| 226 | $data["process"] = "warning"; |
||
| 227 | $data["message"] = $e->getMessage(); |
||
| 228 | } |
||
| 229 | catch (\Exception $e) |
||
| 230 | { |
||
| 231 | $file = str_replace('\\', '', __CLASS__); |
||
| 232 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 233 | |||
| 234 | # stores the error code |
||
| 235 | if (($errorCode = $storage->store($e)) === false) |
||
| 236 | { |
||
| 237 | $errors = $storage->getErrors(); |
||
| 238 | |||
| 239 | # if error storing is not possible, handle it (internal app error) |
||
| 240 | $this->handleErrors($errors, __METHOD__); |
||
| 241 | } |
||
| 242 | |||
| 243 | # errors retrived by the use of ErrorTrait |
||
| 244 | if (count($this->getErrors())) |
||
| 245 | $this->handleErrors($this->getErrors(), __METHOD__); |
||
| 246 | |||
| 247 | $data["code"] = $errorCode; |
||
| 248 | $data["message"] = $e->getMessage(); |
||
| 249 | |||
| 250 | $config = include 'config/application.config.php'; |
||
| 251 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 252 | |||
| 253 | # redirect view |
||
| 254 | $this->setMethod('error'); |
||
| 255 | |||
| 256 | return $data; |
||
| 257 | } |
||
| 258 | |||
| 259 | return $data; |
||
| 260 | } |
||
| 292 | } |