Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Exception;

use Symfony\Component\RateLimiter\Limit;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @experimental in 5.2
*/
class RateLimitExceededException extends \RuntimeException
{
private $limit;

public function __construct(Limit $limit, $code = 0, \Throwable $previous = null)
{
parent::__construct('Rate Limit Exceeded', $code, $previous);

$this->limit = $limit;
}

public function getLimit(): Limit
{
return $this->limit;
}

public function getRetryAfter(): \DateTimeImmutable
{
return $this->limit->getRetryAfter();
}

public function getRemainingTokens(): int
{
return $this->limit->getRemainingTokens();
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/RateLimiter/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\RateLimiter;

use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;

/**
* @author Valentin Silvestre <vsilvestre.pro@gmail.com>
*
Expand All @@ -34,6 +36,18 @@ public function isAccepted(): bool
return $this->accepted;
}

/**
* @throws RateLimitExceededException if not accepted
*/
public function ensureAccepted(): self
{
if (!$this->accepted) {
throw new RateLimitExceededException($this);
}

return $this;
}

public function getRetryAfter(): \DateTimeImmutable
{
return $this->retryAfter;
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/Component/RateLimiter/Tests/LimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\Limit;

class LimitTest extends TestCase
{
public function testEnsureAcceptedDoesNotThrowExceptionIfAccepted()
{
$limit = new Limit(10, new \DateTimeImmutable(), true);

$this->assertSame($limit, $limit->ensureAccepted());
}

public function testEnsureAcceptedThrowsRateLimitExceptionIfNotAccepted()
{
$limit = new Limit(10, $retryAfter = new \DateTimeImmutable(), false);

try {
$limit->ensureAccepted();
} catch (RateLimitExceededException $exception) {
$this->assertSame($limit, $exception->getLimit());
$this->assertSame(10, $exception->getRemainingTokens());
$this->assertSame($retryAfter, $exception->getRetryAfter());

return;
}

$this->fail('RateLimitExceededException not thrown.');
}
}