| Total Complexity | 50 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Core extends Variable |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Initializing Variable. |
||
| 15 | * Irfa\Lockout\Initializing\Variable |
||
| 16 | * |
||
| 17 | * @return void |
||
| 18 | */ |
||
| 19 | public function __construct(){ |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * write login attemps if login attemp is triggered. |
||
| 25 | * |
||
| 26 | * @param string $username |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | protected function eventFailedLogin($username=null){ |
||
| 30 | |||
| 31 | if($username != null){ |
||
|
|
|||
| 32 | $this->setPath($username); |
||
| 33 | } |
||
| 34 | if(!File::exists($this->dir)){ |
||
| 35 | File::makeDirectory($this->dir, 0755, true); |
||
| 36 | } |
||
| 37 | |||
| 38 | if(!File::exists($this->path)) |
||
| 39 | { |
||
| 40 | $login_fail = 1; |
||
| 41 | } else{ |
||
| 42 | |||
| 43 | $get = json_decode(File::get($this->path)); |
||
| 44 | $ip_list = $get->ip; |
||
| 45 | if(!$this->checkIp($ip_list,$this->ip)){ |
||
| 46 | array_push($ip_list,$this->ip); |
||
| 47 | } |
||
| 48 | if($get->attemps == "lock"){ |
||
| 49 | $login_fail = "lock"; |
||
| 50 | } else{ |
||
| 51 | $login_fail = $get->attemps+1; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | $content = ['username' => $this->input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$this->ip],'last_attemps' => date("Y-m-d H:i:s",time())]; |
||
| 56 | File::put($this->path,json_encode($content)); |
||
| 57 | if(File::exists($this->path)){ |
||
| 58 | chmod($this->path,0755); |
||
| 59 | } |
||
| 60 | |||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Clean Lockout file if success login |
||
| 65 | * |
||
| 66 | * @param string $rootNamespace |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | protected function eventCleanLockoutAccount(){ |
||
| 70 | $this->unlock_account($this->input); |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Logging Failed Login attemps |
||
| 76 | * stored file in storage/logs/laravel.log |
||
| 77 | * |
||
| 78 | * @param string $middleware |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | protected function logging($middleware="WEB"){ |
||
| 82 | if(config('irfa.lockout.logging')){ |
||
| 83 | Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if user is locked |
||
| 89 | * |
||
| 90 | * @param string $username |
||
| 91 | * @return boolean |
||
| 92 | */ |
||
| 93 | protected function is_locked($username){ |
||
| 94 | $this->setPath($username); |
||
| 95 | if(File::exists($this->path)) |
||
| 96 | { |
||
| 97 | $get = json_decode(File::get($this->path)); |
||
| 98 | if($get->attemps > $this->attemps || $get->attemps == "lock"){ |
||
| 99 | return true; |
||
| 100 | } else{ |
||
| 101 | return false; |
||
| 102 | } |
||
| 103 | } else{ |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Show message if failed x attemps |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | protected function showMessage(){ |
||
| 114 | if(Session::has(config('irfa.lockout.message_name'))){ |
||
| 115 | return Session::get(config('irfa.lockout.message_name')); |
||
| 116 | } |
||
| 117 | |||
| 118 | return null; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Locked account if max attemps reached |
||
| 123 | * |
||
| 124 | * @return boolean |
||
| 125 | */ |
||
| 126 | protected function lockLogin($username = null){ |
||
| 127 | |||
| 128 | if(php_sapi_name() == "cli" AND $username != null){ |
||
| 129 | $this->setPath($username); |
||
| 130 | } |
||
| 131 | |||
| 132 | if(File::exists($this->path)) |
||
| 133 | { |
||
| 134 | $get = json_decode(File::get($this->path)); |
||
| 135 | if($get->attemps == "lock"){ |
||
| 136 | return true; |
||
| 137 | } |
||
| 138 | if($get->attemps > $this->attemps){ |
||
| 139 | if($matchip){ |
||
| 140 | if($this->checkIp($ip_list,$this->ip)){ |
||
| 141 | return true; |
||
| 142 | } else{ |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | } else{ |
||
| 146 | return true; |
||
| 147 | } |
||
| 148 | } else{ |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | } else{ |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Check ip locked |
||
| 158 | * |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | private function checkIp($ip_list,$ip){ |
||
| 162 | if(collect($ip_list)->contains($ip)){ |
||
| 163 | return true; |
||
| 164 | } else{ |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Clear all locked account |
||
| 172 | * |
||
| 173 | * @return boolean |
||
| 174 | */ |
||
| 175 | public function clear_all(){ |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Unlocking account manually. |
||
| 186 | * |
||
| 187 | * @param string $username |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | public function unlock_account($username){ |
||
| 191 | $this->setPath($username); |
||
| 192 | if(File::exists($this->path)){ |
||
| 193 | $readf = File::get($this->path); |
||
| 194 | File::delete($this->path); |
||
| 195 | if(php_sapi_name() == "cli"){ |
||
| 196 | echo Lang::get('lockoutMessage.user_unlock_success')."\n"; |
||
| 197 | return $readf; |
||
| 198 | |||
| 199 | } else{ |
||
| 200 | return true; |
||
| 201 | } |
||
| 202 | } else{ |
||
| 203 | if(php_sapi_name() == "cli"){ |
||
| 204 | echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
||
| 205 | return false; |
||
| 206 | } else{ |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * For Testing |
||
| 214 | * |
||
| 215 | * @return boolean or json(if cli) |
||
| 216 | */ |
||
| 217 | public function test_unlock_account($username){ |
||
| 218 | $this->setPath($username); |
||
| 219 | if(File::exists($this->path)){ |
||
| 220 | $readf = File::get($this->path); |
||
| 221 | File::delete($this->path); |
||
| 222 | if(php_sapi_name() == "cli"){ |
||
| 223 | return true; |
||
| 224 | |||
| 225 | } else{ |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | } else{ |
||
| 229 | if(php_sapi_name() == "cli"){ |
||
| 230 | return false; |
||
| 231 | } else{ |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Check account with details |
||
| 239 | * |
||
| 240 | * @param string $username |
||
| 241 | * @return boolean |
||
| 242 | */ |
||
| 243 | public function check_account($username){ |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Locking account manually |
||
| 266 | * |
||
| 267 | * @param string $username |
||
| 268 | * @return boolean |
||
| 269 | */ |
||
| 270 | public function lock_account($username){ |
||
| 296 | } |
||
| 297 | } |
||
| 299 | } |