UseMuffin / Throttle

@@ -2,9 +2,11 @@
Loading
2 2
namespace Muffin\Throttle;
3 3
4 4
use Cake\Cache\Cache;
5 +
use Cake\Event\EventDispatcherTrait;
5 6
6 7
trait ThrottleTrait
7 8
{
9 +
    use EventDispatcherTrait;
8 10
9 11
    /*
10 12
     * Default config for Throttle Middleware
@@ -15,7 +17,7 @@
Loading
15 17
        'response' => [
16 18
            'body' => 'Rate limit exceeded',
17 19
            'type' => 'text/html',
18 -
            'headers' => []
20 +
            'headers' => [],
19 21
        ],
20 22
        'interval' => '+1 minute',
21 23
        'limit' => 10,
@@ -23,7 +25,7 @@
Loading
23 25
            'limit' => 'X-RateLimit-Limit',
24 26
            'remaining' => 'X-RateLimit-Remaining',
25 27
            'reset' => 'X-RateLimit-Reset',
26 -
        ]
28 +
        ],
27 29
    ];
28 30
29 31
    /**

@@ -2,16 +2,18 @@
Loading
2 2
namespace Muffin\Throttle\Middleware;
3 3
4 4
use Cake\Core\InstanceConfigTrait;
5 +
use Cake\Event\EventDispatcherInterface;
5 6
use Cake\Http\Response;
6 7
use Cake\Http\ServerRequest;
7 8
use Muffin\Throttle\ThrottleTrait;
8 9
9 -
class ThrottleMiddleware
10 +
class ThrottleMiddleware implements EventDispatcherInterface
10 11
{
11 -
12 12
    use InstanceConfigTrait;
13 13
    use ThrottleTrait;
14 14
15 +
    const EVENT_BEFORE_THROTTLE = 'Throttle.beforeThrottle';
16 +
15 17
    /**
16 18
     * Default Configuration array
17 19
     *
@@ -40,6 +42,13 @@
Loading
40 42
     */
41 43
    public function __invoke(ServerRequest $request, Response $response, callable $next)
42 44
    {
45 +
        $event = $this->dispatchEvent(self::EVENT_BEFORE_THROTTLE, [
46 +
            'request' => $request,
47 +
        ]);
48 +
        if ($event->isStopped()) {
49 +
            return $next($request, $response);
50 +
        }
51 +
43 52
        $this->_setIdentifier($request);
44 53
        $this->_initCache();
45 54
        $this->_count = $this->_touch();

@@ -2,15 +2,17 @@
Loading
2 2
namespace Muffin\Throttle\Routing\Filter;
3 3
4 4
use Cake\Event\Event;
5 +
use Cake\Event\EventDispatcherInterface;
5 6
use Cake\Http\Response;
6 7
use Cake\Routing\DispatcherFilter;
7 8
use Muffin\Throttle\ThrottleTrait;
8 9
9 -
class ThrottleFilter extends DispatcherFilter
10 +
class ThrottleFilter extends DispatcherFilter implements EventDispatcherInterface
10 11
{
11 -
12 12
    use ThrottleTrait;
13 13
14 +
    const EVENT_BEFORE_THROTTLE = 'Throttle.beforeThrottle';
15 +
14 16
    /**
15 17
     * Class constructor.
16 18
     *
@@ -31,6 +33,15 @@
Loading
31 33
     */
32 34
    public function beforeDispatch(Event $event)
33 35
    {
36 +
        $_event = $this->dispatchEvent(self::EVENT_BEFORE_THROTTLE, [
37 +
            'request' => $event->getData('request'),
38 +
        ]);
39 +
        if ($_event->isStopped()) {
40 +
            $event->stopPropagation();
41 +
42 +
            return;
43 +
        }
44 +
34 45
        $this->_setIdentifier($event->getData('request'));
35 46
        $this->_initCache();
36 47
        $this->_count = $this->_touch();
@@ -55,7 +66,7 @@
Loading
55 66
        $response = new Response([
56 67
            'body' => $message,
57 68
            'status' => 429,
58 -
            'type' => $config['response']['type']
69 +
            'type' => $config['response']['type'],
59 70
        ]);
60 71
61 72
        if (is_array($config['response']['headers'])) {
Files Complexity Coverage
src 29 98.00%
Project Totals (3 files) 29 98.00%
230.1
7.0=.0
TRAVIS_OS_NAME=linux
229.1
7.0=.0
TRAVIS_OS_NAME=linux
231.1
7.0=.0
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading