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