1 | <?php |
||
25 | final class RateLimitMiddleware |
||
26 | { |
||
27 | const LIMIT_EXCEEDED_HTTP_STATUS_CODE = 429; //HTTP 429 "Too Many Requests" (RFC 6585) |
||
28 | |||
29 | const HEADER_LIMIT = 'X-RateLimit-Limit'; |
||
30 | const HEADER_REMAINING = 'X-RateLimit-Remaining'; |
||
31 | const HEADER_RESET = 'X-RateLimit-Reset'; |
||
32 | |||
33 | /** |
||
34 | * @var RateLimiterInterface |
||
35 | */ |
||
36 | private $rateLimiter; |
||
37 | |||
38 | /** |
||
39 | * @var IdentityResolverInterface |
||
40 | */ |
||
41 | private $identityResolver; |
||
42 | |||
43 | /** |
||
44 | * @var Options |
||
45 | */ |
||
46 | private $options; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $identity; |
||
52 | |||
53 | public function __construct(RateLimiterInterface $rateLimiter, IdentityResolverInterface $identityResolver, Options $options) |
||
59 | |||
60 | public static function createDefault(RateLimiterInterface $rateLimiter, array $options = []) |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $out = null) |
||
88 | |||
89 | private function isWhitelisted(RequestInterface $request) : bool |
||
95 | |||
96 | private function resolveIdentity(RequestInterface $request) : string |
||
100 | |||
101 | private function onLimitExceeded(RequestInterface $request, ResponseInterface $response) : ResponseInterface |
||
113 | |||
114 | private function onBelowLimit(RequestInterface $request, ResponseInterface $response, callable $out = null) : ResponseInterface |
||
120 | |||
121 | private function next(RequestInterface $request, ResponseInterface $response, callable $out = null) |
||
125 | |||
126 | private function setRateLimitHeaders(ResponseInterface $response) : ResponseInterface |
||
134 | } |
||
135 |