Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to retrieve periods from limiter #3

Merged
merged 3 commits into from Dec 29, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/rate/Limit.php
Expand Up @@ -62,31 +62,31 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function getName(): string
{
return $this->name;
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function getLimit(): int
{
return $this->limit;
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function getDuration(): int
{
return $this->duration;
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function isHidden(): bool
{
Expand Down
10 changes: 10 additions & 0 deletions src/rate/limit/Period.php
Expand Up @@ -43,6 +43,16 @@ public function __construct(
$this->timeout = $timeout;
}

/**
* Retrieve the limit instance
*
* @return \sndsgd\rate\LimitInterface
*/
public function getLimit(): \sndsgd\rate\LimitInterface
{
return $this->limit;
}

/**
* Determine whether the limit has been exceeded
*
Expand Down
25 changes: 16 additions & 9 deletions src/rate/limiter/LimiterAbstract.php
Expand Up @@ -16,7 +16,7 @@ abstract class LimiterAbstract implements LimiterInterface
*
* @var array<\sndsgd\rate\limit\Period>
*/
protected $periods = [];
protected $periods;

/**
* @param array<\sndsgd\rate\Limit> $limits
Expand All @@ -35,16 +35,12 @@ public function __construct(array $limits)
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function isExceeded(): bool
{
if (empty($this->periods)) {
$this->increment();
}

$result = false;
foreach ($this->periods as $period) {
foreach ($this->getPeriods() as $period) {
if ($period->isLimitExceeded()) {
return true;
}
Expand All @@ -53,17 +49,28 @@ public function isExceeded(): bool
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function getHeaders(): array
{
$ret = [];
foreach ($this->periods as $period) {
foreach ($this->getPeriods() as $period) {
$header = $period->getHeader();
if ($header !== "") {
$ret[] = $header;
}
}
return $ret;
}

/**
* @inheritDoc
*/
public function getPeriods(): array
{
if ($this->periods === null) {
$this->increment();
}
return $this->periods ?? [];
}
}
7 changes: 7 additions & 0 deletions src/rate/limiter/LimiterInterface.php
Expand Up @@ -28,4 +28,11 @@ public function isExceeded(): bool;
* @return array<string,string>
*/
public function getHeaders(): array;

/**
* Retrieve an array of rate limit periods
*
* @return array<\sndsgd\rate\limit\Period>
*/
public function getPeriods(): array;
}
2 changes: 1 addition & 1 deletion src/rate/limiter/RedisLimiter.php
Expand Up @@ -22,7 +22,7 @@ public function __construct(\Redis $redis, array $limits)
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function increment(int $incrementBy = 1): LimiterInterface
{
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/rate/limit/PeriodTest.php
Expand Up @@ -4,6 +4,13 @@

class PeriodTest extends \PHPUnit_Framework_TestCase
{
public function testGetLimit()
{
$limit = new \sndsgd\rate\Limit('', '');
$period = new Period($limit);
$this->assertSame($limit, $period->getLimit());
}

/**
* @dataProvider providerIsLimitExceeded
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/rate/limiter/LimiterAbstractTest.php
Expand Up @@ -99,4 +99,15 @@ public function providerGetHeaders()
[[$makePeriod("a"), $makePeriod("b")], ["a", "b"]],
];
}

public function testGetPeriods()
{
$mock = $this->getMockBuilder(LimiterAbstract::class)
->disableOriginalConstructor()
->setMethods(["increment"])
->getMockForAbstractClass();

$mock->expects($this->once())->method("increment");
$mock->getPeriods();
}
}