Skip to content

Commit

Permalink
Php cs fixer (#15)
Browse files Browse the repository at this point in the history
* apply php-cs-fixer

* fix tests
  • Loading branch information
IndraGunawan authored Mar 7, 2020
1 parent b5124b4 commit 9fd1f28
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 96 deletions.
1 change: 1 addition & 0 deletions Annotation/ApiRateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class ApiRateLimit

/**
* @var array
*
* @example ["GET", "POST"]
*/
public $methods = [];
Expand Down
1 change: 0 additions & 1 deletion DependencyInjection/IndragunawanApiRateLimitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Indragunawan\ApiRateLimitBundle\DependencyInjection;

use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down
3 changes: 0 additions & 3 deletions EventListener/HeaderModificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function __construct(array $header)
$this->header = $header;
}

/**
* @param ResponseEvent $event
*/
public function onKernelResponse(ResponseEvent $event)
{
if (false === $this->header['display']) {
Expand Down
4 changes: 1 addition & 3 deletions EventListener/RateLimitListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public function onKernelRequest(RequestEvent $event)
/**
* Returns an RateLimitExceededException.
*
* @param Request $request
*
* @return RateLimitExceededException
*/
protected function createRateLimitExceededException(Request $request)
Expand All @@ -91,7 +89,7 @@ protected function createRateLimitExceededException(Request $request)
$username = null;

if (null !== $token = $this->tokenStorage->getToken()) {
if (is_object($token->getUser())) {
if (\is_object($token->getUser())) {
$username = $token->getUsername();
}
}
Expand Down
33 changes: 5 additions & 28 deletions Service/RateLimitHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ class RateLimitHandler

/**
* RateLimitHandler constructor.
*
* @param CacheItemPoolInterface $cacheItemPool
* @param TokenStorageInterface $tokenStorage
* @param AuthorizationCheckerInterface $authorizationChecker
* @param array $throttleConfig
*/
public function __construct(
CacheItemPoolInterface $cacheItemPool,
Expand Down Expand Up @@ -105,37 +100,25 @@ public function isRateLimitExceeded()
return $this->rateLimitExceeded;
}

/**
* @return array
*/
public function getRateLimitInfo(): array
{
return [
'limit' => $this->limit,
'limit' => $this->limit,
'remaining' => $this->remaining,
'reset' => $this->reset,
'reset' => $this->reset,
];
}

/**
* @param string $ip
* @param string|null $username
* @param string|null $userRole
*
* @return string
*/
public static function generateCacheKey(string $ip, string $username = null, string $userRole = null): string
{
if (!empty($username) && !empty($userRole)) {
return sprintf('_api_rate_limit_metadata$%s', sha1($userRole . $username));
return sprintf('_api_rate_limit_metadata$%s', sha1($userRole.$username));
}

return sprintf('_api_rate_limit_metadata$%s', sha1($ip));
}

/**
* @param Request $request
*
* @throws \Doctrine\Common\Annotations\AnnotationException
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
Expand All @@ -151,7 +134,7 @@ public function handle(Request $request)

if (null !== $annotation) {
$this->enabled = $annotation->enabled;
if (!in_array($request->getMethod(), array_map('strtoupper', $annotation->methods), true) && !empty($annotation->methods)) {
if (!\in_array($request->getMethod(), array_map('strtoupper', $annotation->methods), true) && !empty($annotation->methods)) {
// The annotation is ignored as the method is not corresponding
$annotation = new ApiRateLimit();
}
Expand All @@ -167,10 +150,6 @@ public function handle(Request $request)
}

/**
* @param string $key
* @param int $limit
* @param int $period
*
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function decreaseRateLimitRemaining(string $key, int $limit, int $period)
Expand Down Expand Up @@ -218,15 +197,13 @@ protected function decreaseRateLimitRemaining(string $key, int $limit, int $peri
}

/**
* @param Request $request
*
* @return array
*/
private function getThrottle(Request $request, ApiRateLimit $annotation)
{
if (null !== $token = $this->tokenStorage->getToken()) {
// no anonymous
if (is_object($token->getUser())) {
if (\is_object($token->getUser())) {
$rolesConfig = $this->throttleConfig['roles'];
if (!empty($annotation->throttle['roles'])) {
$rolesConfig = $annotation->throttle['roles'];
Expand Down
2 changes: 1 addition & 1 deletion Tests/Annotation/ApiRateLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function testAssignation()
$rateLimit->throttle = ['foo' => 'bar'];

$this->assertFalse($rateLimit->enabled);
$this->assertEquals(['foo' => 'bar'], $rateLimit->throttle);
$this->assertSame(['foo' => 'bar'], $rateLimit->throttle);
}
}
8 changes: 4 additions & 4 deletions Tests/EventListener/HeaderModificationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function testSetResponseHeaders()

$attributes = $this->prophesize(ParameterBag::class);
$attributes->get('_api_rate_limit_info', null)->willReturn([
'limit' => 60,
'remaining' => 59,
'limit' => '60',
'remaining' => '59',
'reset' => $resetTime,
])->shouldBeCalledTimes(1);

Expand All @@ -100,8 +100,8 @@ public function testSetResponseHeaders()
$listener = new HeaderModificationListener($headerConfig);
$listener->onKernelResponse($event);

$this->assertEquals(60, $response->headers->get('X-RateLimit-Limit'));
$this->assertEquals(59, $response->headers->get('X-RateLimit-Remaining'));
$this->assertSame('60', $response->headers->get('X-RateLimit-Limit'));
$this->assertSame('59', $response->headers->get('X-RateLimit-Remaining'));
$this->assertSame($resetTime, $response->headers->get('X-RateLimit-Reset'));
}
}
38 changes: 19 additions & 19 deletions Tests/EventListener/RateLimitListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testDisabledApiRateLimit()

$event->expects($this->never())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);

$tokenStorage = $this->createMock(TokenStorage::class);

Expand All @@ -44,11 +44,11 @@ public function testNotMasterRequest()

$event->expects($this->once())
->method('isMasterRequest')
->will($this->returnValue(false));
->willReturn(false);

$event->expects($this->never())
->method('getRequest')
->will($this->returnValue(Request::create('/api/me')));
->willReturn(Request::create('/api/me'));

$tokenStorage = $this->createMock(TokenStorage::class);

Expand All @@ -63,13 +63,13 @@ public function testNoApiResourceClass()

$event->expects($this->once())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);

$request = Request::create('/api/me');

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
->willReturn($request);

$tokenStorage = $this->createMock(TokenStorage::class);

Expand All @@ -86,7 +86,7 @@ public function testRateLimitHandlerDisabled()

$rateLimitHandler->expects($this->once())
->method('isEnabled')
->will($this->returnValue(false));
->willReturn(false);

$rateLimitHandler->expects($this->never())
->method('isRateLimitExceeded');
Expand All @@ -95,14 +95,14 @@ public function testRateLimitHandlerDisabled()

$event->expects($this->once())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);

$request = Request::create('/api/me');
$request->attributes->set('_api_resource_class', 'Foo');

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
->willReturn($request);

$tokenStorage = $this->createMock(TokenStorage::class);

Expand All @@ -124,24 +124,24 @@ public function testRateLimitExceededForAnonymousUser()

$rateLimitHandler->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
->willReturn(true);

$rateLimitHandler->expects($this->once())
->method('isRateLimitExceeded')
->will($this->returnValue(true));
->willReturn(true);

$event = $this->createMock(RequestEvent::class);

$event->expects($this->once())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);

$request = Request::create('/api/me');
$request->attributes->set('_api_resource_class', 'Foo');

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
->willReturn($request);

$exceptionConfig = [
'status_code' => 429,
Expand All @@ -168,24 +168,24 @@ public function testRateLimitExceededForAuthenticatedUser()

$rateLimitHandler->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
->willReturn(true);

$rateLimitHandler->expects($this->once())
->method('isRateLimitExceeded')
->will($this->returnValue(true));
->willReturn(true);

$event = $this->createMock(RequestEvent::class);

$event->expects($this->once())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);

$request = Request::create('/api/me');
$request->attributes->set('_api_resource_class', 'Foo');

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
->willReturn($request);

$exceptionConfig = [
'status_code' => 429,
Expand All @@ -197,16 +197,16 @@ public function testRateLimitExceededForAuthenticatedUser()
$token = $this->createMock(UsernamePasswordToken::class);
$token->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
->willReturn($user);

$token->expects($this->once())
->method('getUsername')
->will($this->returnValue('user'));
->willReturn('user');

$tokenStorage = $this->createMock(TokenStorage::class);
$tokenStorage->expects($this->once())
->method('getToken')
->will($this->returnValue($token));
->willReturn($token);

$listener = new RateLimitListener(true, $rateLimitHandler, $exceptionConfig, $tokenStorage);
$listener->onKernelRequest($event);
Expand Down
11 changes: 10 additions & 1 deletion Tests/Fixtures/Entity/ThrottleConfigured.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the ApiRateLimitBundle
*
* (c) Indra Gunawan <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Indragunawan\ApiRateLimitBundle\Tests\Fixtures\Entity;

use Indragunawan\ApiRateLimitBundle\Annotation\ApiRateLimit;
Expand Down Expand Up @@ -49,4 +58,4 @@ public function setBar(string $bar)
{
$this->bar = $bar;
}
}
}
Loading

0 comments on commit 9fd1f28

Please sign in to comment.