-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLimitHelper.php
More file actions
70 lines (53 loc) · 2.18 KB
/
LimitHelper.php
File metadata and controls
70 lines (53 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Saloon\RateLimitPlugin\Helpers;
use Saloon\RateLimitPlugin\Limit;
use Saloon\RateLimitPlugin\Exceptions\LimitException;
class LimitHelper
{
/**
* Hydrate the limits
*
* @param array<Limit> $limits
* @return array<Limit>
* @throws LimitException
*/
public static function configureLimits(array $limits, ?string $prefix, ?Limit $tooManyAttemptsLimit = null): array
{
// Firstly, we will clean up the limits array to only ensure the `Limit` classes
// are being processed.
$limits = array_filter($limits, static fn (mixed $value) => $value instanceof Limit);
// Now we'll make a clone of each of the limits at their empty state
// This is important because otherwise we'll be mutating the original
// objects and keep adding to the same object instances.
$limits = array_map(static fn (Limit $limit) => clone $limit, $limits);
// Next we will append our "too many attempts" limit which will be used when
// the response actually hits a 429 status.
if (isset($tooManyAttemptsLimit)) {
$limits[] = $tooManyAttemptsLimit->name('too_many_attempts_limit');
}
// Next we will set the prefix on each of the limits.
$limits = array_map(static fn (Limit $limit) => $limit->setPrefix($prefix), $limits);
// Finally, we will check if there are any duplicate limits. If there are, then we will
// throw an exception instead of continuing.
if ($duplicateLimit = self::getDuplicate($limits)) {
throw new LimitException(sprintf('Duplicate limit name "%s". Consider using a custom name on the limit.', $duplicateLimit));
}
return $limits;
}
/**
* Get the first duplicate limit
*
* @param array<Limit> $limits
*/
private static function getDuplicate(array $limits): ?string
{
$limitNames = array_map(static fn (Limit $limit) => $limit->getName(), $limits);
foreach (array_count_values($limitNames) as $name => $count) {
if ($count > 1) {
return $name;
}
}
return null;
}
}