Skip to content

Commit

Permalink
Merge pull request #52 from UseMuffin/cake5
Browse files Browse the repository at this point in the history
Cake5
  • Loading branch information
ADmad authored Sep 23, 2023
2 parents 88e0313 + 8b82d13 commit 7115931
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 122 deletions.
82 changes: 15 additions & 67 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
name: CI

on: [push, pull_request]
on:
push:
branches:
- master
- cake5
pull_request:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.4']
prefer-lowest: ['']
include:
- php-version: '7.2'
prefer-lowest: 'prefer-lowest'

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: pcov

- name: Composer install
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer install
fi
- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.4'
uses: codecov/codecov-action@v1
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extension: mbstring, intl
coverage: none
tools: cs2pr, phpstan:^0.12

- name: Composer Install
run: composer install

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr

- name: Run phpstan
if: success() || failure()
run: phpstan analyse
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.32" installed="1.10.32" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
25 changes: 22 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"source": "https://github.com/usemuffin/throttle"
},
"require": {
"cakephp/cakephp": "^4.0"
"cakephp/cakephp": "^5.0.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.0",
"phpunit/phpunit": "~8.5.0"
"phpunit/phpunit": "^10.1.0",
"cakephp/cakephp-codesniffer": "^5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -53,5 +53,24 @@
"Muffin\\Throttle\\Test\\": "tests",
"TestApp\\": "tests/test_app/src"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"cs-check": "phpcs --colors --parallel=16 -p src/ tests/",
"cs-fix": "phpcbf --colors --parallel=16 -p src/ tests/",
"phpstan": "tools/phpstan analyse",
"psalm": "tools/psalm --show-info=false",
"stan": [
"@phpstan",
"@psalm"
],
"stan-baseline": "tools/phpstan --generate-baseline",
"psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml",
"stan-setup": "phive install",
"test": "phpunit"
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Throttle">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />
</ruleset>
39 changes: 15 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/bootstrap.php"
colors="true"
stopOnFailure="false"
>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>
<testsuites>
<testsuite name="Throttle Test Cases">
<directory>./tests/</directory>
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
17 changes: 17 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="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"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
27 changes: 17 additions & 10 deletions src/Middleware/ThrottleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;

/**
* ThrottleMiddleware
*
* @implements \Cake\Event\EventDispatcherInterface<\Muffin\Throttle\Middleware\ThrottleMiddleware>
*/
class ThrottleMiddleware implements MiddlewareInterface, EventDispatcherInterface
{
use InstanceConfigTrait;

/**
* @use \Cake\Event\EventDispatcherTrait<\Muffin\Throttle\Middleware\ThrottleMiddleware>
*/
use EventDispatcherTrait;

public const EVENT_BEFORE_THROTTLE = 'Throttle.beforeThrottle';
Expand All @@ -34,7 +43,7 @@ class ThrottleMiddleware implements MiddlewareInterface, EventDispatcherInterfac
*
* @var array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'response' => [
'body' => 'Rate limit exceeded',
'type' => 'text/plain',
Expand All @@ -55,7 +64,7 @@ class ThrottleMiddleware implements MiddlewareInterface, EventDispatcherInterfac
*
* @var string
*/
protected $_identifier;
protected string $_identifier;

/**
* Throttle Middleware constructor.
Expand Down Expand Up @@ -85,6 +94,7 @@ public function __construct(array $config = [])
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Expand Down Expand Up @@ -128,11 +138,7 @@ protected function _getErrorResponse(RateLimitInfo $rateLimit): ResponseInterfac
}
}

if (isset($config['message'])) {
$message = $config['message'];
} else {
$message = $config['response']['body'];
}
$message = $config['message'] ?? $config['response']['body'];

$retryAfter = $rateLimit->getResetTimestamp() - time();
$response = $response
Expand Down Expand Up @@ -171,6 +177,7 @@ protected function _getThrottle(ServerRequestInterface $request): ThrottleInfo
*
* @param \Muffin\Throttle\ValueObject\ThrottleInfo $throttle Throttling info.
* @return \Muffin\Throttle\ValueObject\RateLimitInfo
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
protected function _rateLimit(ThrottleInfo $throttle): RateLimitInfo
{
Expand Down Expand Up @@ -251,11 +258,11 @@ protected function _initCache(): void
*/
protected function _getDefaultCacheConfigClassName(): string
{
/** @var array $config */
$config = Cache::getConfig('default');
$engine = (string)$config['className'];

$engine = !empty($config['className']) ? (string)$config['className'] : '';
// short cache engine names can be returned immediately
if (strpos($engine, '\\') === false) {
if (!str_contains($engine, '\\')) {
return $engine;
}
// fully namespace cache engine names need extracting class name
Expand Down
12 changes: 6 additions & 6 deletions src/Plugin.php → src/ThrottlePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@

use Cake\Core\BasePlugin;

class Plugin extends BasePlugin
class ThrottlePlugin extends BasePlugin
{
/**
* The name of this plugin
*
* @var string
* @var string|null
*/
protected $name = 'Throttle';
protected ?string $name = 'Throttle';

/**
* Do bootstrapping or not
*
* @var bool
*/
protected $bootstrapEnabled = false;
protected bool $bootstrapEnabled = false;

/**
* Load routes or not
*
* @var bool
*/
protected $routesEnabled = false;
protected bool $routesEnabled = false;

/**
* Console middleware
*
* @var bool
*/
protected $consoleEnabled = false;
protected bool $consoleEnabled = false;
}
6 changes: 3 additions & 3 deletions src/ValueObject/RateLimitInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class RateLimitInfo
/**
* @var int
*/
protected $limit;
protected int $limit;

/**
* @var int
*/
protected $calls;
protected int $calls;

/**
* @var int
*/
protected $resetTimestamp;
protected int $resetTimestamp;

public function __construct(int $limit = 0, int $calls = 0, int $resetTimestamp = 0)
{
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObject/ThrottleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class ThrottleInfo
/**
* @var int
*/
protected $limit;
protected int $limit;

/**
* @var int
*/
protected $period;
protected int $period;

/**
* @var string
*/
protected $key;
protected string $key;

public function __construct(string $key = '', int $limit = 0, int $period = 0)
{
Expand Down
Loading

0 comments on commit 7115931

Please sign in to comment.