diff options
author | Marvin Borner | 2018-05-23 22:23:28 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-23 22:23:28 +0200 |
commit | b66a61addb6c8e66cb26fcf74b532d68891267e4 (patch) | |
tree | 05e9449ff25bdc98f68105f41923ccb9f6ef5095 /main/app/sprinkles/core/src/Throttle | |
parent | 1d4ef435177a5f9b6d1a289800d933e49be0c550 (diff) |
Refactored code, many fixes and improvements in chat backend+frontend
Diffstat (limited to 'main/app/sprinkles/core/src/Throttle')
-rw-r--r-- | main/app/sprinkles/core/src/Throttle/ThrottleRule.php | 25 | ||||
-rw-r--r-- | main/app/sprinkles/core/src/Throttle/Throttler.php | 28 | ||||
-rw-r--r-- | main/app/sprinkles/core/src/Throttle/ThrottlerException.php | 1 |
3 files changed, 21 insertions, 33 deletions
diff --git a/main/app/sprinkles/core/src/Throttle/ThrottleRule.php b/main/app/sprinkles/core/src/Throttle/ThrottleRule.php index b71f296..c5e0c82 100644 --- a/main/app/sprinkles/core/src/Throttle/ThrottleRule.php +++ b/main/app/sprinkles/core/src/Throttle/ThrottleRule.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Throttle; /** @@ -35,8 +36,7 @@ class ThrottleRule * @param int $interval The amount of time, in seconds, to look back in determining attempts to consider. * @param int[] $delays A mapping of minimum observation counts (x) to delays (y), in seconds. */ - public function __construct($method, $interval, $delays) - { + public function __construct($method, $interval, $delays) { $this->setMethod($method); $this->setInterval($interval); $this->setDelays($delays); @@ -48,8 +48,7 @@ class ThrottleRule * @param Carbon\Carbon $lastEventTime The timestamp for the last countable event. * @param int $count The total number of events which have occurred in an interval. */ - public function getDelay($lastEventTime, $count) - { + public function getDelay($lastEventTime, $count) { // Zero occurrences always maps to a delay of 0 seconds. if ($count == 0) { return 0; @@ -75,8 +74,7 @@ class ThrottleRule * * @return int[] */ - public function getDelays() - { + public function getDelays() { return $this->delays; } @@ -85,8 +83,7 @@ class ThrottleRule * * @return int */ - public function getInterval() - { + public function getInterval() { return $this->interval; } @@ -95,8 +92,7 @@ class ThrottleRule * * @return string */ - public function getMethod() - { + public function getMethod() { return $this->method; } @@ -105,8 +101,7 @@ class ThrottleRule * * @param int[] A mapping of minimum observation counts (x) to delays (y), in seconds. */ - public function setDelays($delays) - { + public function setDelays($delays) { // Sort the array by key, from highest to lowest value $this->delays = $delays; krsort($this->delays); @@ -119,8 +114,7 @@ class ThrottleRule * * @param int The amount of time, in seconds, to look back in determining attempts to consider. */ - public function setInterval($interval) - { + public function setInterval($interval) { $this->interval = $interval; return $this; @@ -131,8 +125,7 @@ class ThrottleRule * * @param string Set to 'ip' for ip-based throttling, 'data' for request-data-based throttling. */ - public function setMethod($method) - { + public function setMethod($method) { $this->method = $method; return $this; diff --git a/main/app/sprinkles/core/src/Throttle/Throttler.php b/main/app/sprinkles/core/src/Throttle/Throttler.php index 0d42442..4ab9dd6 100644 --- a/main/app/sprinkles/core/src/Throttle/Throttler.php +++ b/main/app/sprinkles/core/src/Throttle/Throttler.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Throttle; use Carbon\Carbon; @@ -32,8 +33,7 @@ class Throttler * * @param ClassMapper $classMapper Maps generic class identifiers to specific class names. */ - public function __construct(ClassMapper $classMapper) - { + public function __construct(ClassMapper $classMapper) { $this->classMapper = $classMapper; $this->throttleRules = []; } @@ -44,9 +44,8 @@ class Throttler * @param string $type The type of throttle event to check against. * @param ThrottleRule $rule The rule to use when throttling this type of event. */ - public function addThrottleRule($type, $rule) - { - if (!($rule instanceof ThrottleRule || ($rule === null))) { + public function addThrottleRule($type, $rule) { + if (!($rule instanceof ThrottleRule || ($rule === NULL))) { throw new ThrottlerException('$rule must be of type ThrottleRule (or null).'); } @@ -62,8 +61,7 @@ class Throttler * @param mixed[] $requestData Any additional request parameters to use in checking the throttle. * @return bool */ - public function getDelay($type, $requestData = []) - { + public function getDelay($type, $requestData = []) { $throttleRule = $this->getRule($type); if (is_null($throttleRule)) { @@ -93,11 +91,11 @@ class Throttler // then filter out this event from the collection. foreach ($requestData as $name => $value) { if (!isset($data->$name) || ($data->$name != $value)) { - return false; + return FALSE; } } - return true; + return TRUE; }); } @@ -112,8 +110,7 @@ class Throttler * @throws ThrottlerException * @return ThrottleRule[] */ - public function getRule($type) - { + public function getRule($type) { if (!array_key_exists($type, $this->throttleRules)) { throw new ThrottlerException("The throttling rule for '$type' could not be found."); } @@ -126,8 +123,7 @@ class Throttler * * @return ThrottleRule[] */ - public function getThrottleRules() - { + public function getThrottleRules() { return $this->throttleRules; } @@ -137,8 +133,7 @@ class Throttler * @param string $type the type of event * @param string[] $requestData an array of field names => values that are relevant to throttling for this event (e.g. username, email, etc). */ - public function logEvent($type, $requestData = []) - { + public function logEvent($type, $requestData = []) { // Just a check to make sure the rule exists $throttleRule = $this->getRule($type); @@ -164,8 +159,7 @@ class Throttler * @param ThrottleRule $throttleRule a rule representing the strategy to use for throttling a particular type of event. * @return int seconds remaining until a particular event is permitted to be attempted again. */ - protected function computeDelay($events, $throttleRule) - { + protected function computeDelay($events, $throttleRule) { // If no matching events found, then there is no delay if (!$events->count()) { return 0; diff --git a/main/app/sprinkles/core/src/Throttle/ThrottlerException.php b/main/app/sprinkles/core/src/Throttle/ThrottlerException.php index 2fd9035..08f2919 100644 --- a/main/app/sprinkles/core/src/Throttle/ThrottlerException.php +++ b/main/app/sprinkles/core/src/Throttle/ThrottlerException.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Throttle; /** |