Skip to content

Commit

Permalink
Few updates after @goetas review
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Urien authored and goetas committed Aug 14, 2023
1 parent b3e5442 commit f8bb3fb
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions EventListener/RateLimitAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ public function onKernelController(ControllerEvent $event): void

// RateLimits used to be set by sensio/framework-extra-bundle by reading annotations
// Tests also use that mechanism, we should probably keep it for retrocompatibility
if ($event->getRequest()->attributes->has('_x-rate-limit')) {
$request = $event->getRequest();
if ($request->attributes->has('_x-rate-limit')) {
/** @var RateLimit[] $rateLimits */
$rateLimits = $event->getRequest()->attributes->get('_x-rate-limit', []);
$rateLimits = $request->attributes->get('_x-rate-limit', []);
} else {
$rateLimits = $this->getRateLimitsFromAttributes($event->getController());
}
$rateLimit = $this->findBestMethodMatch($event->getRequest(), $rateLimits);
$rateLimit = $this->findBestMethodMatch($request, $rateLimits);

// Another treatment before applying RateLimit ?
$checkedRateLimitEvent = new CheckedRateLimitEvent($event->getRequest(), $rateLimit);
$checkedRateLimitEvent = new CheckedRateLimitEvent($request, $rateLimit);
$this->eventDispatcher->dispatch($checkedRateLimitEvent, RateLimitEvents::CHECKED_RATE_LIMIT);
$rateLimit = $checkedRateLimitEvent->getRateLimit();

Expand All @@ -81,7 +82,6 @@ public function onKernelController(ControllerEvent $event): void


// Store the current rating info in the request attributes
$request = $event->getRequest();
$request->attributes->set('rate_limit_info', $rateLimitInfo);

// Reset the rate limits
Expand Down Expand Up @@ -153,13 +153,14 @@ protected function findBestMethodMatch(Request $request, array $rateLimits): ?Ra
private function getKey(ControllerEvent $event, RateLimit $rateLimit, array $rateLimits): string
{
// Let listeners manipulate the key
$keyEvent = new GenerateKeyEvent($event->getRequest(), '', $rateLimit->getPayload());
$request = $event->getRequest();
$keyEvent = new GenerateKeyEvent($request, '', $rateLimit->getPayload());

$rateLimitMethods = implode('.', $rateLimit->getMethods());
$keyEvent->addToKey($rateLimitMethods);

$rateLimitAlias = count($rateLimits) === 0
? str_replace('/', '.', $this->pathLimitProcessor->getMatchedPath($event->getRequest()))
? str_replace('/', '.', $this->pathLimitProcessor->getMatchedPath($request))
: $this->getAliasForRequest($event);
$keyEvent->addToKey($rateLimitAlias);
$this->eventDispatcher->dispatch($keyEvent, RateLimitEvents::GENERATE_KEY);
Expand Down Expand Up @@ -198,7 +199,7 @@ private function getAliasForRequest(ControllerEvent $event): string
/**
* @return RateLimit[]
*/
public function getRateLimitsFromAttributes(string|array|object $controller): array
private function getRateLimitsFromAttributes(string|array|object $controller): array
{
$rClass = $rMethod = null;
if (\is_array($controller) && method_exists(...$controller)) {
Expand Down

0 comments on commit f8bb3fb

Please sign in to comment.