| Conditions | 13 |
| Paths | 800 |
| Total Lines | 62 |
| Code Lines | 39 |
| 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 |
||
| 71 | public function limit($target = '_global', $req_per_hour = null, $flush_on_abort = null, $show_header = null) { |
||
| 72 | $req_per_hour = $req_per_hour !== null ? $req_per_hour : $this->base_limit; |
||
| 73 | $flush_on_abort = $flush_on_abort !== null ? $flush_on_abort : $this->flush_on_abort; |
||
| 74 | $show_header = $show_header !== null ? $show_header : $this->header_show; |
||
| 75 | |||
| 76 | $truncated = $this->_truncate(); |
||
| 77 | if(!$truncated) { |
||
| 78 | log_message('DEBUG', 'WARN: Could not truncate rate limit table'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if($this->is_whitelisted()) { |
||
| 82 | $req_per_hour = 0; |
||
| 83 | } |
||
| 84 | |||
| 85 | $abort = FALSE; |
||
| 86 | if($req_per_hour > 0) { |
||
| 87 | $info = $this->get_limit_info($target); |
||
| 88 | |||
| 89 | if($info === FALSE) { |
||
| 90 | $info = new stdClass(); |
||
| 91 | $info->count = 0; |
||
| 92 | $info->reset_epoch = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 93 | $info->start = date('d M Y H:i:s'); |
||
| 94 | } |
||
| 95 | |||
| 96 | if($req_per_hour - $info->count > 0) { |
||
| 97 | $data = array('client' => $this->get_hash(), 'target' => $target); |
||
| 98 | $this->CI->db->query($this->_sql_update, $data); |
||
| 99 | $info->count++; |
||
| 100 | } else { |
||
| 101 | $abort = TRUE; |
||
| 102 | } |
||
| 103 | |||
| 104 | if($show_header === TRUE) { |
||
| 105 | $headers = array( |
||
| 106 | 'Limit' => $req_per_hour, |
||
| 107 | 'Remaining' => $req_per_hour - $info->count, |
||
| 108 | 'Reset' => strtotime($info->reset_epoch), |
||
| 109 | ); |
||
| 110 | |||
| 111 | foreach(array_keys($headers) as $h) { |
||
| 112 | $this->CI->output->set_header("$this->header_prefix$h: $headers[$h]"); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->_info_cache[$target] = $info; |
||
| 117 | |||
| 118 | |||
| 119 | if($abort) { |
||
| 120 | $retry_seconds = strtotime($info->reset_epoch) - strtotime(gmdate('d M Y H:i:s')); |
||
| 121 | $this->CI->output->set_header("Retry-After: $retry_seconds"); |
||
| 122 | $this->CI->output->set_status_header(503, 'Rate limit reached'); |
||
| 123 | |||
| 124 | if($flush_on_abort) { |
||
| 125 | $this->CI->output->_display(); |
||
| 126 | exit; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | return $abort; |
||
| 132 | } |
||
| 133 | |||
| 235 |