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