Total Complexity | 46 |
Total Lines | 197 |
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 |
||
9 | class Core |
||
10 | { |
||
11 | protected function eventFailedLogin(){ |
||
12 | $ip = Request::ip(); |
||
13 | $input = Request::input(config('irfa.lockout.input_name')); |
||
14 | $matchip= config('irfa.lockout.match_ip') == true ? $ip :null; |
||
|
|||
15 | $dir = config('irfa.lockout.lockout_file_path'); |
||
16 | $path = $dir.md5($input); |
||
17 | |||
18 | if(!File::exists($dir)){ |
||
19 | File::makeDirectory($dir, 0750, true); |
||
20 | } |
||
21 | |||
22 | if(!File::exists($path)) |
||
23 | { |
||
24 | $login_fail = 1; |
||
25 | } else{ |
||
26 | |||
27 | $get = json_decode(File::get($path)); |
||
28 | $ip_list = $get->ip; |
||
29 | if(!$this->checkIp($ip_list,$ip)){ |
||
30 | array_push($ip_list,$ip); |
||
31 | } |
||
32 | if($get->attemps == "lock"){ |
||
33 | $login_fail = "lock"; |
||
34 | } else{ |
||
35 | $login_fail = $get->attemps+1; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | $content = ['username' => $input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$ip],'last_attemps' => date("Y-m-d H:i:s",time())]; |
||
40 | File::put($path,json_encode($content)); |
||
41 | if(File::exists($path)){ |
||
42 | chmod($path,0750); |
||
43 | } |
||
44 | |||
45 | } |
||
46 | protected function eventCleanLockoutAccount(){ |
||
47 | $input = Request::input(config('irfa.lockout.input_name')); |
||
48 | $this->unlock_account($input); |
||
49 | |||
50 | } |
||
51 | protected function logging($middleware="WEB"){ |
||
52 | if(config('irfa.lockout.logging')){ |
||
53 | Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL); |
||
54 | } |
||
55 | } |
||
56 | protected function is_locked($username){ |
||
57 | $dir = config('irfa.lockout.lockout_file_path'); |
||
58 | $attemps = config('irfa.lockout.login_attemps'); |
||
59 | $path = $dir.md5($username); |
||
60 | if(File::exists($path)) |
||
61 | { |
||
62 | $get = json_decode(File::get($path)); |
||
63 | if($get->attemps > $attemps || $get->attemps == "lock"){ |
||
64 | return true; |
||
65 | } else{ |
||
66 | return false; |
||
67 | } |
||
68 | } else{ |
||
69 | return false; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | protected function showMessage(){ |
||
74 | if(Session::has(config('irfa.lockout.message_name'))){ |
||
75 | return Session::get(config('irfa.lockout.message_name')); |
||
76 | } |
||
77 | |||
78 | return null; |
||
79 | } |
||
80 | protected function lockLogin(){ |
||
81 | $ip = Request::ip(); |
||
82 | $input = Request::input(config('irfa.lockout.input_name')); |
||
83 | $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip'); |
||
84 | $dir = config('irfa.lockout.lockout_file_path'); |
||
85 | $attemps = config('irfa.lockout.login_attemps'); |
||
86 | $path = $dir.md5($input); |
||
87 | if(File::exists($path)) |
||
88 | { |
||
89 | $get = json_decode(File::get($path)); |
||
90 | // dd($get->attemps.">".$attemps); |
||
91 | if($get->attemps == "lock"){ |
||
92 | return true; |
||
93 | } |
||
94 | if($get->attemps > $attemps){ |
||
95 | if($matchip){ |
||
96 | if($this->checkIp($ip_list,$ip)){ |
||
97 | return true; |
||
98 | } else{ |
||
99 | return false; |
||
100 | } |
||
101 | } else{ |
||
102 | return true; |
||
103 | } |
||
104 | } else{ |
||
105 | return false; |
||
106 | } |
||
107 | } else{ |
||
108 | return false; |
||
109 | } |
||
110 | } |
||
111 | private function checkIp($ip_list,$ip){ |
||
112 | if(collect($ip_list)->contains($ip)){ |
||
113 | return true; |
||
114 | } else{ |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | } |
||
119 | public function clear_all(){ |
||
125 | } |
||
126 | } |
||
127 | public function unlock_account($username){ |
||
128 | $ip = Request::ip(); |
||
129 | $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip'); |
||
130 | $dir = config('irfa.lockout.lockout_file_path'); |
||
131 | $attemps = config('irfa.lockout.attemps'); |
||
132 | $path = $dir.md5($username); |
||
133 | |||
134 | if(File::exists($path)){ |
||
135 | $readf = File::get($path); |
||
136 | File::delete($path); |
||
137 | if(php_sapi_name() == "cli"){ |
||
138 | echo Lang::get('lockoutMessage.user_unlock_success')."\n"; |
||
139 | return $readf; |
||
140 | |||
141 | } else{ |
||
142 | return true; |
||
143 | } |
||
144 | } else{ |
||
145 | if(php_sapi_name() == "cli"){ |
||
146 | echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
||
147 | exit(); |
||
148 | } else{ |
||
149 | return false; |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | public function check_account($username){ |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | public function lock_account($username){ |
||
206 | } |
||
207 | } |
||
209 | } |