Limit.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Illuminate\Cache\RateLimiting;
  3. class Limit
  4. {
  5. /**
  6. * The rate limit signature key.
  7. *
  8. * @var mixed
  9. */
  10. public $key;
  11. /**
  12. * The maximum number of attempts allowed within the given number of minutes.
  13. *
  14. * @var int
  15. */
  16. public $maxAttempts;
  17. /**
  18. * The number of minutes until the rate limit is reset.
  19. *
  20. * @var int
  21. */
  22. public $decayMinutes;
  23. /**
  24. * The response generator callback.
  25. *
  26. * @var callable
  27. */
  28. public $responseCallback;
  29. /**
  30. * Create a new limit instance.
  31. *
  32. * @param mixed $key
  33. * @param int $maxAttempts
  34. * @param int $decayMinutes
  35. * @return void
  36. */
  37. public function __construct($key = '', int $maxAttempts = 60, int $decayMinutes = 1)
  38. {
  39. $this->key = $key;
  40. $this->maxAttempts = $maxAttempts;
  41. $this->decayMinutes = $decayMinutes;
  42. }
  43. /**
  44. * Create a new rate limit.
  45. *
  46. * @param int $maxAttempts
  47. * @return static
  48. */
  49. public static function perMinute($maxAttempts)
  50. {
  51. return new static('', $maxAttempts);
  52. }
  53. /**
  54. * Create a new rate limit using minutes as decay time.
  55. *
  56. * @param int $decayMinutes
  57. * @param int $maxAttempts
  58. * @return static
  59. */
  60. public static function perMinutes($decayMinutes, $maxAttempts)
  61. {
  62. return new static('', $maxAttempts, $decayMinutes);
  63. }
  64. /**
  65. * Create a new rate limit using hours as decay time.
  66. *
  67. * @param int $maxAttempts
  68. * @param int $decayHours
  69. * @return static
  70. */
  71. public static function perHour($maxAttempts, $decayHours = 1)
  72. {
  73. return new static('', $maxAttempts, 60 * $decayHours);
  74. }
  75. /**
  76. * Create a new rate limit using days as decay time.
  77. *
  78. * @param int $maxAttempts
  79. * @param int $decayDays
  80. * @return static
  81. */
  82. public static function perDay($maxAttempts, $decayDays = 1)
  83. {
  84. return new static('', $maxAttempts, 60 * 24 * $decayDays);
  85. }
  86. /**
  87. * Create a new unlimited rate limit.
  88. *
  89. * @return static
  90. */
  91. public static function none()
  92. {
  93. return new Unlimited;
  94. }
  95. /**
  96. * Set the key of the rate limit.
  97. *
  98. * @param mixed $key
  99. * @return $this
  100. */
  101. public function by($key)
  102. {
  103. $this->key = $key;
  104. return $this;
  105. }
  106. /**
  107. * Set the callback that should generate the response when the limit is exceeded.
  108. *
  109. * @param callable $callback
  110. * @return $this
  111. */
  112. public function response(callable $callback)
  113. {
  114. $this->responseCallback = $callback;
  115. return $this;
  116. }
  117. }