Skip to content

Commit

Permalink
fixed pslam
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-shabani committed Jan 3, 2021
1 parent 77e9a42 commit d9f58d8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1080:{a:2:{s:7:"defects";a:6:{s:73:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_work_correctly";i:4;s:91:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_throw_exeption_if_otp_is_limited";i:4;s:85:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_otp_could_be_limited_for_specific_key";i:4;s:60:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_limiter_rate";i:4;s:62:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_remaining_time";i:4;s:63:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_throw_exception";i:4;}s:5:"times";a:6:{s:73:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_work_correctly";d:0.037;s:91:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_throw_exeption_if_otp_is_limited";d:0.005;s:85:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_otp_could_be_limited_for_specific_key";d:0.002;s:60:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_limiter_rate";d:0.002;s:62:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_remaining_time";d:0.002;s:63:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_throw_exception";d:0.002;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":1080:{a:2:{s:7:"defects";a:6:{s:73:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_work_correctly";i:4;s:91:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_throw_exeption_if_otp_is_limited";i:3;s:85:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_otp_could_be_limited_for_specific_key";i:4;s:60:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_limiter_rate";i:4;s:62:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_remaining_time";i:4;s:63:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_throw_exception";i:4;}s:5:"times";a:6:{s:73:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_work_correctly";d:0.035;s:91:"BdpRaymon\OtpLimiter\Tests\MiddlewareTest::test_middleware_throw_exeption_if_otp_is_limited";d:0.004;s:85:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_otp_could_be_limited_for_specific_key";d:0.002;s:60:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_limiter_rate";d:0.002;s:62:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_remaining_time";d:0.002;s:63:"BdpRaymon\OtpLimiter\Tests\OtpLimiterTest::test_throw_exception";d:0.002;}}}
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_script:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

script:
- vendor/bin/psalm --shepherd
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<psalm
errorLevel="7"
resolveFromConfigFile="true"
allowStringToStandInForClass="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/OtpRequestExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OtpRequestExceededException extends Exception

protected int $remainingTime;

public function __construct($key, $remainingTime)
public function __construct(string $key, int $remainingTime)
{
parent::__construct("Otp rate limit request for {$key} exceeded, for new request you should wait {$remainingTime} seconds");
$this->key = $key;
Expand Down
7 changes: 7 additions & 0 deletions src/Middleware/OtpLimiterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace BdpRaymon\OtpLimiter\Middleware;

use Closure;
use Exception;
use Illuminate\Http\Request;
use BdpRaymon\OtpLimiter\OtpLimiter;

Expand All @@ -17,6 +18,12 @@ public function __construct(OtpLimiter $otpLimiter)
$this->otpLimiter = $otpLimiter;
}

/**
* @param Request $request
* @param Closure $next
* @param array ...$params
* @throws Exception
*/
public function handle(Request $request, Closure $next, ...$params): void
{
$key = $this->getKey($request, $params);
Expand Down
23 changes: 16 additions & 7 deletions src/OtpLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
namespace BdpRaymon\OtpLimiter;

use Carbon\Carbon;
use Exception;
use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository;
use Illuminate\Support\Arr;

class OtpLimiter
{
protected string $key;

protected string $suffix = 'otp-limiter';

protected int $rate;

protected CacheManager $cacheManager;

protected array $config;
Expand Down Expand Up @@ -57,15 +54,23 @@ public function remained(string $key): int

public function set(string $key): bool
{
$time = now()->addSeconds($this->rate());
return $this->cacheManager->set($this->key($key), $time, $time);
return $this->cacheManager->set(
$this->key($key),
now()->addSeconds($this->rate()),
$this->rate()
);
}

protected function rate(): int
{
return Arr::get($this->config, 'custom.otp-rate-limiter', 60 * 3);
}

/**
* @param string $key
* @throws Exception
* @return void
*/
public function throw(string $key): void
{
$exception = $this->getException();
Expand All @@ -74,7 +79,11 @@ public function throw(string $key): void
return;
}

throw new $exception($key, $this->remained($key));
$throwable = new $exception($key, $this->remained($key));

if ($throwable instanceof Exception) {
throw $throwable;
}
}

protected function getException(): ?string
Expand Down
4 changes: 3 additions & 1 deletion src/OtpLimiterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace BdpRaymon\OtpLimiter;

use Illuminate\Container\Container;
use Illuminate\Support\ServiceProvider;


class OtpLimiterServiceProvider extends ServiceProvider
{

Expand All @@ -22,7 +24,7 @@ public function register(): void
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'otp-limiter');

// Register the main class to use with the facade
$this->app->bind('otp-limiter', function ($app) {
$this->app->bind('otp-limiter', function (Container $app) {
return new OtpLimiter(
$app->make('cache'),
$app->make('config')
Expand Down

0 comments on commit d9f58d8

Please sign in to comment.