Total Complexity | 49 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 | * Except account. |
||
14 | * |
||
15 | * @return boolean |
||
16 | */ |
||
17 | protected function exceptAccount(){ |
||
18 | $this->initVar(); |
||
19 | if($this->except_enabled){ |
||
20 | if(in_array($this->input, $this->except_accounts)){ |
||
21 | return true; |
||
22 | } else{ |
||
23 | return false; |
||
24 | } |
||
25 | } else{ |
||
26 | return false; |
||
27 | } |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * write login attemps if login attemp is triggered. |
||
32 | * |
||
33 | * @param string $username |
||
34 | * @return void |
||
35 | */ |
||
36 | protected function eventFailedLogin($username=null){ |
||
37 | $this->initVar(); |
||
38 | if($username !== null){ |
||
39 | $this->setPath($username); |
||
40 | } |
||
41 | if(!File::exists($this->dir)){ |
||
42 | File::makeDirectory($this->dir, 0755, true); |
||
43 | } |
||
44 | |||
45 | if(!File::exists($this->path)) |
||
46 | { |
||
47 | $login_fail = 1; |
||
48 | } else{ |
||
49 | |||
50 | $get = json_decode(File::get($this->path)); |
||
51 | $ip_list = $get->ip; |
||
52 | if(!$this->checkIp($ip_list,$this->ip)){ |
||
53 | array_push($ip_list,$this->ip); |
||
54 | } |
||
55 | if($get->attemps == "lock"){ |
||
56 | $login_fail = "lock"; |
||
57 | } else{ |
||
58 | $login_fail = $get->attemps+1; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | $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())]; |
||
63 | File::put($this->path,json_encode($content)); |
||
64 | if(File::exists($this->path)){ |
||
65 | chmod($this->path,0755); |
||
66 | } |
||
67 | |||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Clean Lockout file if success login |
||
72 | * |
||
73 | * @param string $rootNamespace |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function eventCleanLockoutAccount() { |
||
77 | $this->initVar(); |
||
78 | $this->unlock_account($this->input); |
||
79 | |||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Logging Failed Login attemps |
||
84 | * stored file in storage/logs/laravel.log |
||
85 | * |
||
86 | * @param string $middleware |
||
87 | * @return void |
||
88 | */ |
||
89 | protected function logging($middleware = "WEB") { |
||
90 | if (config('irfa.lockout.logging')) { |
||
91 | Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Check if user is locked |
||
97 | * |
||
98 | * @param string $username |
||
99 | * @return boolean |
||
100 | */ |
||
101 | protected function is_locked($username){ |
||
102 | $this->initVar(); |
||
103 | $this->setPath($username); |
||
104 | if(File::exists($this->path)) |
||
105 | { |
||
106 | $get = json_decode(File::get($this->path)); |
||
107 | if($get->attemps > $this->attemps || $get->attemps == "lock"){ |
||
108 | return true; |
||
109 | } else{ |
||
110 | return false; |
||
111 | } |
||
112 | } else{ |
||
113 | return false; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Show message if failed x attemps |
||
119 | * |
||
120 | * @return mixed |
||
121 | */ |
||
122 | protected function showMessage() { |
||
123 | if (Session::has(config('irfa.lockout.message_name'))) { |
||
124 | return Session::get(config('irfa.lockout.message_name')); |
||
125 | } |
||
126 | |||
127 | return null; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Locked account if max attemps reached |
||
132 | * |
||
133 | * @return boolean |
||
134 | */ |
||
135 | protected function lockLogin($username = null){ |
||
136 | $this->initVar(); |
||
137 | if(php_sapi_name() == "cli" AND $username != null){ |
||
138 | $this->setPath($username); |
||
139 | } |
||
140 | |||
141 | if(File::exists($this->path)) |
||
142 | { |
||
143 | $get = json_decode(File::get($this->path)); |
||
144 | if($get->attemps == "lock"){ |
||
145 | return true; |
||
146 | } |
||
147 | if($get->attemps > $this->attemps){ |
||
148 | |||
149 | return true; |
||
150 | } else { |
||
151 | return false; |
||
152 | } |
||
153 | } else { |
||
154 | return false; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Check ip locked |
||
160 | * |
||
161 | * @return boolean |
||
162 | */ |
||
163 | private function checkIp($ip_list,$ip){ |
||
164 | $this->initVar(); |
||
165 | if(collect($ip_list)->contains($ip)){ |
||
166 | return true; |
||
167 | } else{ |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Clear all locked account |
||
175 | * |
||
176 | * @return boolean |
||
177 | */ |
||
178 | public function clear_all(){ |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Unlocking account manually. |
||
190 | * |
||
191 | * @param string $username |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function unlock_account($username){ |
||
195 | $this->initVar(); |
||
196 | $this->setPath($username); |
||
197 | if(File::exists($this->path)){ |
||
198 | $readf = File::get($this->path); |
||
199 | File::delete($this->path); |
||
200 | if(php_sapi_name() == "cli"){ |
||
201 | echo Lang::get('lockoutMessage.user_unlock_success')."\n"; |
||
202 | return $readf; |
||
203 | |||
204 | } else{ |
||
205 | return true; |
||
206 | } |
||
207 | } else{ |
||
208 | if(php_sapi_name() == "cli"){ |
||
209 | echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
||
210 | return false; |
||
211 | } else{ |
||
212 | return false; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * For Testing |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function test_unlock_account($username){ |
||
223 | $this->initVar(); |
||
224 | $this->setPath($username); |
||
225 | if(File::exists($this->path)){ |
||
226 | $readf = File::get($this->path); |
||
|
|||
227 | File::delete($this->path); |
||
228 | if(php_sapi_name() == "cli"){ |
||
229 | return true; |
||
230 | |||
231 | } else{ |
||
232 | return true; |
||
233 | } |
||
234 | } else{ |
||
235 | if(php_sapi_name() == "cli"){ |
||
236 | return false; |
||
237 | } else{ |
||
238 | return false; |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Check account with details |
||
245 | * |
||
246 | * @param string $username |
||
247 | * @return mixed |
||
248 | */ |
||
249 | public function check_account($username){ |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Locking account manually |
||
273 | * |
||
274 | * @param string $username |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public function lock_account($username){ |
||
304 | } |
||
305 | } |
||
307 | } |