public function __invoke(Request $request, Application $app) { $flap = new Flap(new FlapsStorage($app['database']), 'api'); $flap->pushThrottlingStrategy(new LeakyBucketStrategy(50, '1m')); if ($flap->isViolator($request->getClientIp())) { throw new TooManyRequestsException('You have reached the maximum allowed number of requests per minute. Calm down, buddy.'); } }
/** * Sets up a rate limiter with a throttling strategy * @param BehEh\Flaps\Flap $flap * @param BehEh\Flaps\ThrottlingStrategyInterface $trottlingStrategy */ public function __construct(Flap $flap, ThrottlingStrategyInterface $throttlingStrategy) { $this->flap = $flap; // @todo allow multiple strategies $this->flap->pushThrottlingStrategy($throttlingStrategy); }