-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dispatch PSR-14 event when rate limit is exceeded
resolves: #1
- Loading branch information
1 parent
40bc8a7
commit 0f61d80
Showing
9 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "form_rate_limit" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\FormRateLimit\Event; | ||
|
||
use Brotkrueml\FormRateLimit\Domain\Dto\Options; | ||
|
||
final class RateLimitExceededEvent | ||
{ | ||
private string $formIdentifier; | ||
private Options $options; | ||
|
||
public function __construct( | ||
string $formIdentifier, | ||
Options $options | ||
) { | ||
$this->formIdentifier = $formIdentifier; | ||
$this->options = $options; | ||
} | ||
|
||
public function getFormIdentifier(): string | ||
{ | ||
return $this->formIdentifier; | ||
} | ||
|
||
public function getInterval(): string | ||
{ | ||
return $this->options->getInterval(); | ||
} | ||
|
||
public function getLimit(): int | ||
{ | ||
return $this->options->getLimit(); | ||
} | ||
|
||
public function getPolicy(): string | ||
{ | ||
return $this->options->getPolicy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
.. include:: /Includes.rst.txt | ||
|
||
.. index:: Events | ||
|
||
.. _events: | ||
|
||
============= | ||
PSR-14 events | ||
============= | ||
|
||
Target group: **Developers** | ||
|
||
Have a look into the :ref:`event dispatcher documentation <t3coreapi:EventDispatcher>`, | ||
if you are not familiar with PSR-14 events. | ||
|
||
RateLimitExceededEvent | ||
====================== | ||
|
||
.. versionadded:: 1.2.0 | ||
|
||
This event is dispatched when the rate limit for a form has been exceeded. This | ||
way you can create an event listener which notifies you about the exceeded | ||
limit: add a log entry, send an email, inform a third-party system, etc. | ||
|
||
The event :php:`\Brotkrueml\FormRateLimit\Event\RateLimitExceededEvent` | ||
provides the following methods: | ||
|
||
:php:`->getFormIdentifier()` | ||
Returns the form identifier. | ||
|
||
:php:`->getInterval()` | ||
Returns the configured :ref:`interval <options-interval>`. | ||
|
||
:php:`->getLimit()` | ||
Returns the configured :ref:`limit <options-limit>`. | ||
|
||
:php:`->getPolicy()` | ||
Returns the configured :ref:`policy <options-policy>`. | ||
|
||
Example | ||
------- | ||
|
||
This example adds an entry to the TYPO3 log: | ||
|
||
.. code-block:: php | ||
:caption: EXT:your_extension/Classes/EventListener/FormRateLimitExceededLogger.php | ||
<?php | ||
declare(strict_types=1); | ||
namespace YourVendor\YourExtension\EventListener; | ||
use Brotkrueml\FormRateLimit\Event\RateLimitExceededEvent; | ||
use Psr\Log\LoggerInterface; | ||
final class FormRateLimitExceededLogger | ||
{ | ||
private LoggerInterface $logger; | ||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
public function __invoke(RateLimitExceededEvent $event): void | ||
{ | ||
$this->logger->warning( | ||
'The form with identifier "{formIdentifier}" was sent more than {limit} times within {interval}', | ||
[ | ||
'formIdentifier' => $event->getFormIdentifier(), | ||
'limit' => $event->getLimit(), | ||
'interval' => $event->getInterval() | ||
] | ||
); | ||
} | ||
} | ||
Registration of the event listener: | ||
|
||
.. code-block:: yaml | ||
:caption: EXT:your_extension/Configuration/Services.yaml | ||
services: | ||
YourVendor\YourExtension\EventListener\FormRateLimitExceededLogger: | ||
tags: | ||
- name: event.listener | ||
identifier: 'yourFormRateLimitExceededLogger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "form_rate_limit" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\FormRateLimit\Tests\Unit\Event; | ||
|
||
use Brotkrueml\FormRateLimit\Domain\Dto\Options; | ||
use Brotkrueml\FormRateLimit\Event\RateLimitExceededEvent; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RateLimitExceededEventTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function valuesFromGettersReturnedCorrectly(): void | ||
{ | ||
$subject = new RateLimitExceededEvent( | ||
'mypage-42', | ||
new Options( | ||
'1 hour', | ||
3, | ||
'sliding_window', | ||
[] | ||
) | ||
); | ||
|
||
self::assertSame('mypage-42', $subject->getFormIdentifier()); | ||
self::assertSame('1 hour', $subject->getInterval()); | ||
self::assertSame(3, $subject->getLimit()); | ||
self::assertSame('sliding_window', $subject->getPolicy()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters