Total Complexity | 43 |
Total Lines | 251 |
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(){ |
||
20 | $this->initVar(); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * write login attemps if login attemp is triggered. |
||
25 | * |
||
26 | * @return void |
||
27 | */ |
||
28 | protected function eventFailedLogin(){ |
||
29 | |||
30 | |||
31 | if(!File::exists($this->dir)){ |
||
32 | File::makeDirectory($this->dir, 0755, true); |
||
33 | } |
||
34 | |||
35 | if(!File::exists($this->path)) |
||
36 | { |
||
37 | $login_fail = 1; |
||
38 | } else{ |
||
39 | |||
40 | $get = json_decode(File::get($this->path)); |
||
41 | $ip_list = $get->ip; |
||
42 | if(!$this->checkIp($ip_list,$this->ip)){ |
||
43 | array_push($ip_list,$this->ip); |
||
44 | } |
||
45 | if($get->attemps == "lock"){ |
||
46 | $login_fail = "lock"; |
||
47 | } else{ |
||
48 | $login_fail = $get->attemps+1; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | $content = ['username' => $this->input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$ip],'last_attemps' => date("Y-m-d H:i:s",time())]; |
||
|
|||
53 | File::put($this->path,json_encode($content)); |
||
54 | if(File::exists($this->path)){ |
||
55 | chmod($this->path,0755); |
||
56 | } |
||
57 | |||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Clean Lockout file if success login |
||
62 | * |
||
63 | * @param string $rootNamespace |
||
64 | * @return void |
||
65 | */ |
||
66 | protected function eventCleanLockoutAccount(){ |
||
67 | $this->unlock_account($this->input); |
||
68 | |||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Logging Failed Login attemps |
||
73 | * stored file in storage/logs/laravel.log |
||
74 | * |
||
75 | * @param string $middleware |
||
76 | * @return void |
||
77 | */ |
||
78 | protected function logging($middleware="WEB"){ |
||
79 | if(config('irfa.lockout.logging')){ |
||
80 | Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Check if user is locked |
||
86 | * |
||
87 | * @param string $username |
||
88 | * @return boolean |
||
89 | */ |
||
90 | protected function is_locked($username){ |
||
91 | |||
92 | if(File::exists($this->path)) |
||
93 | { |
||
94 | $get = json_decode(File::get($this->path)); |
||
95 | if($get->attemps > $attemps || $get->attemps == "lock"){ |
||
96 | return true; |
||
97 | } else{ |
||
98 | return false; |
||
99 | } |
||
100 | } else{ |
||
101 | return false; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Show message if failed x attemps |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | protected function showMessage(){ |
||
111 | if(Session::has(config('irfa.lockout.message_name'))){ |
||
112 | return Session::get(config('irfa.lockout.message_name')); |
||
113 | } |
||
114 | |||
115 | return null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Locked account if max attemps reached |
||
120 | * |
||
121 | * @return boolean |
||
122 | */ |
||
123 | protected function lockLogin(){ |
||
124 | |||
125 | if(File::exists($this->path)) |
||
126 | { |
||
127 | $get = json_decode(File::get($this->path)); |
||
128 | if($get->attemps == "lock"){ |
||
129 | return true; |
||
130 | } |
||
131 | if($get->attemps > $attemps){ |
||
132 | if($matchip){ |
||
133 | if($this->checkIp($ip_list,$this->ip)){ |
||
134 | return true; |
||
135 | } else{ |
||
136 | return false; |
||
137 | } |
||
138 | } else{ |
||
139 | return true; |
||
140 | } |
||
141 | } else{ |
||
142 | return false; |
||
143 | } |
||
144 | } else{ |
||
145 | return false; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Check ip locked |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | private function checkIp($ip_list,$ip){ |
||
155 | if(collect($ip_list)->contains($ip)){ |
||
156 | return true; |
||
157 | } else{ |
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Clear all locked account |
||
165 | * |
||
166 | * @return boolean |
||
167 | */ |
||
168 | public function clear_all(){ |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Unlocking account manually. |
||
179 | * |
||
180 | * @return boolean or json(if cli) |
||
181 | */ |
||
182 | public function unlock_account($username){ |
||
183 | if(File::exists($this->path)){ |
||
184 | $readf = File::get($this->path); |
||
185 | File::delete($this->path); |
||
186 | if(php_sapi_name() == "cli"){ |
||
187 | echo Lang::get('lockoutMessage.user_unlock_success')."\n"; |
||
188 | return $readf; |
||
189 | |||
190 | } else{ |
||
191 | return true; |
||
192 | } |
||
193 | } else{ |
||
194 | if(php_sapi_name() == "cli"){ |
||
195 | echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
||
196 | exit(); |
||
197 | } else{ |
||
198 | return false; |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Check account with details |
||
205 | * |
||
206 | * @param string $username |
||
207 | * @return boolean |
||
208 | */ |
||
209 | public function check_account($username){ |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Locking account manually |
||
231 | * |
||
232 | * @param string $username |
||
233 | * @return boolean |
||
234 | */ |
||
235 | public function lock_account($username){ |
||
261 | } |
||
262 | } |
||
264 | } |