Skip to content

Commit

Permalink
Working on next version
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 5, 2023
1 parent 991c5b4 commit d5d0ef7
Show file tree
Hide file tree
Showing 29 changed files with 267 additions and 288 deletions.
24 changes: 20 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ jobs:

strategy:
matrix:
php: ['7.4', '8.0', '8.1']
laravel: ['8', '9']
php: ['7.4', '8.0', '8.1', '8.2']
laravel: ['8', '9', '10']
exclude:
- php: '7.4'
laravel: '9'
- php: '7.4'
laravel: '10'
- php: '8.0'
laravel: '10'
- php: '8.2'
laravel: '8'

steps:
- name: Checkout Code
Expand All @@ -27,6 +33,8 @@ jobs:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
env:
update: true

- name: Setup Problem Matchers
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
Expand All @@ -36,17 +44,25 @@ jobs:
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:^8.75" --no-update --no-interaction
command: composer require "laravel/framework:^8.83.27" "phpunit/phpunit:^9.6.3" --no-update --no-interaction
if: "matrix.laravel == '8'"

- name: Select Laravel 9
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:^9.0" --no-update --no-interaction
command: composer require "laravel/framework:^9.52.4" "phpunit/phpunit:^9.6.3" --no-update --no-interaction
if: "matrix.laravel == '9'"

- name: Select Laravel 10
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:^10.1.5" "phpunit/phpunit:^10.0.12" --no-update --no-interaction
if: "matrix.laravel == '10'"

- name: Install PHP Dependencies
uses: nick-invision/retry@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013-2022 Graham Campbell <[email protected]>
Copyright (c) 2013-2023 Graham Campbell <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Laravel Throttle was created by, and is maintained by [Graham Campbell](https://
![Banner](https://user-images.githubusercontent.com/2829600/71477509-693e3c80-27e2-11ea-8497-46b3a1c8f1f4.png)

<p align="center">
<a href="https://github.com/GrahamCampbell/Laravel-Throttle/actions?query=workflow%3ATests"><img src="https://img.shields.io/github/workflow/status/GrahamCampbell/Laravel-Throttle/Tests?label=Tests&style=flat-square" alt="Build Status"></img></a>
<a href="https://github.com/GrahamCampbell/Laravel-Throttle/actions?query=workflow%3ATests"><img src="https://img.shields.io/github/actions/workflow/status/GrahamCampbell/Laravel-Throttle/tests.yml?label=Tests&style=flat-square" alt="Build Status"></img></a>
<a href="https://github.styleci.io/repos/15437427"><img src="https://github.styleci.io/repos/15437427/shield" alt="StyleCI Status"></img></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen?style=flat-square" alt="Software License"></img></a>
<a href="https://packagist.org/packages/graham-campbell/throttle"><img src="https://img.shields.io/packagist/dt/graham-campbell/throttle?style=flat-square" alt="Packagist Downloads"></img></a>
Expand All @@ -16,21 +16,19 @@ Laravel Throttle was created by, and is maintained by [Graham Campbell](https://

## Installation

This version requires [PHP](https://www.php.net/) 7.4-8.1 and supports [Laravel](https://laravel.com/) 8-9.
This version requires [PHP](https://www.php.net/) 7.4-8.2 and supports [Laravel](https://laravel.com/) 8-10.

| Throttle | L5.1 | L5.2 | L5.3 | L5.4 | L5.5 | L5.6 | L5.7 | L5.8 | L6 | L7 | L8 | L9 |
|----------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|
| 4.1 | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: |
| 5.3 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: |
| 6.0 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :x: |
| 7.5 | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: |
| 8.2 | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| 9.0 | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: |
| Throttle | L5.5 | L5.6 | L5.7 | L5.8 | L6 | L7 | L8 | L9 | L10 |
|----------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|
| 7.5 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: |
| 8.2 | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
| 9.0 | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :x: |
| 10.0 | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: |

To get the latest version, simply require the project using [Composer](https://getcomposer.org/):

```bash
$ composer require "graham-campbell/throttle:^9.0"
$ composer require "graham-campbell/throttle:^10.0"
```

Once installed, if you are not using automatic package discovery, then you need to register the `GrahamCampbell\Throttle\ThrottleServiceProvider` service provider in your `config/app.php`.
Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
],
"require": {
"php": "^7.4.15 || ^8.0.2",
"illuminate/cache": "^8.75 || ^9.0",
"illuminate/contracts": "^8.75 || ^9.0",
"illuminate/http": "^8.75 || ^9.0",
"illuminate/support": "^8.75 || ^9.0"
"illuminate/cache": "^8.75 || ^9.0 || ^10.0",
"illuminate/contracts": "^8.75 || ^9.0 || ^10.0",
"illuminate/http": "^8.75 || ^9.0 || ^10.0",
"illuminate/support": "^8.75 || ^9.0 || ^10.0"
},
"require-dev": {
"graham-campbell/analyzer": "^3.1",
"graham-campbell/testbench": "^5.7",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5"
"graham-campbell/analyzer": "^4.0",
"graham-campbell/testbench": "^6.0",
"mockery/mockery": "^1.5.1",
"phpunit/phpunit": "^9.6.3 || ^10.0.12"
},
"autoload": {
"psr-4": {
Expand All @@ -42,7 +42,5 @@
"GrahamCampbell\\Throttle\\ThrottleServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
38 changes: 11 additions & 27 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Laravel Throttle Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" colors="true" failOnRisky="true" failOnWarning="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<testsuites>
<testsuite name="Laravel Throttle Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
22 changes: 11 additions & 11 deletions src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ class Data
*
* @var string
*/
protected $ip;
private string $ip;

/**
* The route.
*
* @var string
*/
protected $route;
private string $route;

/**
* The request limit.
*
* @var int
*/
protected $limit;
private int $limit;

/**
* The expiration time in minutes.
*
* @var int
*/
protected $time;
private int $time;

/**
* The unique key.
*
* @var string
* @var ?string
*/
protected $key;
private ?string $key = null;

/**
* Create a new instance.
Expand All @@ -78,7 +78,7 @@ public function __construct(string $ip, string $route, int $limit = 10, int $tim
*
* @return string
*/
public function getIp()
public function getIp(): string
{
return $this->ip;
}
Expand All @@ -88,7 +88,7 @@ public function getIp()
*
* @return string
*/
public function getRoute()
public function getRoute(): string
{
return $this->route;
}
Expand All @@ -98,7 +98,7 @@ public function getRoute()
*
* @return int
*/
public function getLimit()
public function getLimit(): int
{
return $this->limit;
}
Expand All @@ -108,7 +108,7 @@ public function getLimit()
*
* @return int
*/
public function getTime()
public function getTime(): int
{
return $this->time;
}
Expand All @@ -120,7 +120,7 @@ public function getTime()
*
* @return string
*/
public function getKey()
public function getKey(): string
{
if (!$this->key) {
$this->key = sha1($this->ip.$this->route);
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Throttle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Throttle extends Facade
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'throttle';
}
Expand Down
16 changes: 3 additions & 13 deletions src/Factory/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
*
* @author Graham Campbell <[email protected]>
*/
class CacheFactory implements FactoryInterface
final class CacheFactory implements FactoryInterface
{
/**
* The cache instance.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
private Repository $cache;

/**
* Create a new instance.
Expand All @@ -50,18 +50,8 @@ public function __construct(Repository $cache)
*
* @return \GrahamCampbell\Throttle\Throttler\CacheThrottler
*/
public function make(Data $data)
public function make(Data $data): CacheThrottler
{
return new CacheThrottler($this->cache->getStore(), $data->getKey(), $data->getLimit(), $data->getTime() * 60);
}

/**
* Get the cache instance.
*
* @return \Illuminate\Contracts\Cache\Repository
*/
public function getCache()
{
return $this->cache;
}
}
3 changes: 2 additions & 1 deletion src/Factory/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace GrahamCampbell\Throttle\Factory;

use GrahamCampbell\Throttle\Data;
use GrahamCampbell\Throttle\Throttler\ThrottlerInterface;

/**
* This is the throttler factory interface.
Expand All @@ -29,5 +30,5 @@ interface FactoryInterface
*
* @return \GrahamCampbell\Throttle\Throttler\ThrottlerInterface
*/
public function make(Data $data);
public function make(Data $data): ThrottlerInterface;
}
9 changes: 5 additions & 4 deletions src/Http/Middleware/ThrottleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Closure;
use GrahamCampbell\Throttle\Throttle;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

/**
Expand All @@ -29,7 +30,7 @@ class ThrottleMiddleware
*
* @var \GrahamCampbell\Throttle\Throttle
*/
protected $throttle;
protected Throttle $throttle;

/**
* Create a new throttle middleware instance.
Expand All @@ -48,14 +49,14 @@ public function __construct(Throttle $throttle)
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int $limit
* @param int $time
* @param int|string $limit
* @param int|string $time
*
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*
* @return mixed
*/
public function handle($request, Closure $next, $limit = 10, $time = 60)
public function handle(Request $request, Closure $next, $limit = 10, $time = 60)
{
if (!$this->throttle->attempt($request, (int) $limit, (int) $time)) {
throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceeded.');
Expand Down
Loading

0 comments on commit d5d0ef7

Please sign in to comment.