Conditions | 9 |
Paths | 66 |
Total Lines | 71 |
Code Lines | 38 |
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 |
||
35 | private function DoesRuleExist() |
||
36 | { |
||
37 | $ipRule = $this->driver->get($this->ip, 'rule'); |
||
38 | |||
39 | if (empty($ipRule)) { |
||
40 | return false; |
||
41 | } |
||
42 | |||
43 | $ruleType = (int) $ipRule['type']; |
||
44 | |||
45 | // Apply the status code. |
||
46 | $this->result = $ruleType; |
||
|
|||
47 | |||
48 | if ($ruleType === kernel::ACTION_ALLOW) { |
||
49 | return true; |
||
50 | } |
||
51 | |||
52 | // Current visitor has been blocked. If he still attempts accessing the site, |
||
53 | // then we can drop him into the permanent block list. |
||
54 | $attempts = $ipRule['attempts'] ?? 0; |
||
55 | $attempts = (int) $attempts; |
||
56 | $now = time(); |
||
57 | $logData = []; |
||
58 | $handleType = 0; |
||
59 | |||
60 | $logData['log_ip'] = $ipRule['log_ip']; |
||
61 | $logData['ip_resolve'] = $ipRule['ip_resolve']; |
||
62 | $logData['time'] = $now; |
||
63 | $logData['type'] = $ipRule['type']; |
||
64 | $logData['reason'] = $ipRule['reason']; |
||
65 | $logData['attempts'] = $attempts; |
||
66 | |||
67 | // @since 0.2.0 |
||
68 | $attemptPeriod = $this->properties['record_attempt_detection_period']; |
||
69 | $attemptReset = $this->properties['reset_attempt_counter']; |
||
70 | |||
71 | $lastTimeDiff = $now - $ipRule['time']; |
||
72 | |||
73 | if ($lastTimeDiff <= $attemptPeriod) { |
||
74 | $logData['attempts'] = ++$attempts; |
||
75 | } |
||
76 | |||
77 | if ($lastTimeDiff > $attemptReset) { |
||
78 | $logData['attempts'] = 0; |
||
79 | } |
||
80 | |||
81 | if ($ruleType === kernel::ACTION_TEMPORARILY_DENY) { |
||
82 | $ratd = $this->determineAttemptsTemporaryDeny($logData, $handleType, $attempts); |
||
83 | $logData = $ratd['log_data']; |
||
84 | $handleType = $ratd['handle_type']; |
||
85 | } |
||
86 | |||
87 | if ($ruleType === kernel::ACTION_DENY) { |
||
88 | $rapd = $this->determineAttemptsPermanentDeny($logData, $handleType, $attempts); |
||
89 | $logData = $rapd['log_data']; |
||
90 | $handleType = $rapd['handle_type']; |
||
91 | } |
||
92 | |||
93 | // We only update data when `deny_attempt_enable` is enable. |
||
94 | // Because we want to get the last visited time and attempt counter. |
||
95 | // Otherwise, we don't update it everytime to avoid wasting CPU resource. |
||
96 | if ($this->event['update_rule_table']) { |
||
97 | $this->driver->save($this->ip, $logData, 'rule'); |
||
98 | } |
||
99 | |||
100 | // Notify this event to messenger. |
||
101 | if ($this->event['trigger_messengers']) { |
||
102 | $this->prepareMessengerBody($logData, $handleType); |
||
103 | } |
||
104 | |||
105 | return true; |
||
106 | } |
||
199 |