Skip to content

Commit 7c9d391

Browse files
committed
Make throttle rate configurable via routes() and legacyRoutes() parameters
Users can now pass a custom rate or disable throttling entirely via named parameters, rather than having a hardcoded '60,1' rate: Subscriber::routes(throttle: '10,1'); // custom rate Subscriber::routes(throttle: false); // disable The default remains '60,1'. FakeSubscriber, facade docblock, and the published stub are updated to match. https://claude.ai/code/session_01R4pAjWwGY8xKspsdU8xnsy
1 parent cf9c66f commit 7c9d391

4 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/Facades/Subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*
1212
* @see \YlsIdeas\SubscribableNotifications\Subscriber
1313
*
14-
* @method static void routes()
15-
* @method static void legacyRoutes(string $defaultModel)
14+
* @method static void routes(mixed $router = null, string|false $throttle = '60,1')
15+
* @method static void legacyRoutes(string $defaultModel, mixed $router = null, string|false $throttle = '60,1')
1616
* @method static string routeName()
1717
* @method static void onCompletion(callable|string $handler)
1818
* @method static void onUnsubscribeFromMailingList(callable|string $handler)

src/Subscriber.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,34 @@ public function __construct(private readonly Application $app)
3030
{
3131
}
3232

33-
public function routes(mixed $router = null): void
33+
public function routes(mixed $router = null, string|false $throttle = '60,1'): void
3434
{
3535
$router = $router ?? $this->app->make('router');
36-
$router->match(['GET', 'POST'], $this->uri, $this->handler)
36+
$route = $router->match(['GET', 'POST'], $this->uri, $this->handler)
3737
->name($this->routeName)
38-
->where('subscriberType', '[^\d/][^/]*')
39-
->middleware('throttle:60,1');
38+
->where('subscriberType', '[^\d/][^/]*');
39+
40+
if ($throttle !== false) {
41+
$route->middleware("throttle:{$throttle}");
42+
}
4043
}
4144

42-
public function legacyRoutes(string $defaultModel, mixed $router = null): void
45+
public function legacyRoutes(string $defaultModel, mixed $router = null, string|false $throttle = '60,1'): void
4346
{
4447
$router = $router ?? $this->app->make('router');
4548

4649
$morphMap = Relation::morphMap();
4750
$this->legacySubscriberType = array_search($defaultModel, $morphMap, true) ?: $defaultModel;
4851

49-
$router->match(
52+
$route = $router->match(
5053
['GET', 'POST'],
5154
'unsubscribe/{subscriberId}/{mailingList?}',
5255
'\YlsIdeas\SubscribableNotifications\Controllers\LegacyUnsubscribeController'
53-
)->name($this->routeName . '.legacy')
54-
->middleware('throttle:60,1');
56+
)->name($this->routeName . '.legacy');
57+
58+
if ($throttle !== false) {
59+
$route->middleware("throttle:{$throttle}");
60+
}
5561
}
5662

5763
public function routeName(): string

src/Testing/FakeSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ final class FakeSubscriber
1414
private array $subscriptionStatusChecks = [];
1515
private bool $subscriptionStatus = true;
1616

17-
public function routes(mixed $router = null): void
17+
public function routes(mixed $router = null, string|false $throttle = '60,1'): void
1818
{
1919
}
2020

21-
public function legacyRoutes(string $defaultModel, mixed $router = null): void
21+
public function legacyRoutes(string $defaultModel, mixed $router = null, string|false $throttle = '60,1'): void
2222
{
2323
}
2424

stubs/SubscribableServiceProvider.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class SubscribableServiceProvider extends ServiceProvider
1111
{
1212
// Note: register this route outside the 'web' middleware group to avoid CSRF token
1313
// verification on the POST (RFC 8058 one-click) endpoint.
14+
// The throttle defaults to '60,1' (60 requests per minute). Adjust or disable:
15+
// Subscriber::routes(throttle: '10,1');
16+
// Subscriber::routes(throttle: false);
1417
Subscriber::routes();
1518

1619
Subscriber::onUnsubscribeFromMailingList(function ($notifiable, string $mailingList) {

0 commit comments

Comments
 (0)