ThrottleFactory::for()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Zenstruck\Governator;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class ThrottleFactory
9
{
10
    private Store $store;
11
    private string $prefix;
12
13
    /**
14
     * @param string $prefix Global resource prefix for created throttles
15
     */
16 114
    public function __construct(Store $store, string $prefix = 'throttle_')
17
    {
18 114
        $this->store = $store;
19 114
        $this->prefix = $prefix;
20 114
    }
21
22
    /**
23
     * Create for passed connection DSN/object.
24
     *
25
     * @param string|object $connection
26
     * @param string        $prefix     Global resource prefix for created throttles
27
     */
28 7
    public static function for($connection, string $prefix = 'throttle_'): self
29
    {
30 7
        return new self(StoreFactory::create($connection), $prefix);
31
    }
32
33
    /**
34
     * Create a throttle.
35
     *
36
     * @param string $resource Unique identifier for the throttle
37
     * @param int    $limit    The maximum number of throttle "hits" in its "time window"
38
     * @param int    $ttl      the "time window" for the throttle in seconds
39
     */
40 111
    public function create(string $resource, int $limit, int $ttl): Throttle
41
    {
42 111
        return new Throttle($this->store, new Key($resource, $limit, $ttl, $this->prefix));
43
    }
44
45
    /**
46
     * Create a fluent interface throttle builder.
47
     *
48
     * @see ThrottleBuilder
49
     *
50
     * @param string ...$resource Unique identifier(s) for the throttle
51
     */
52 41
    public function throttle(string ...$resource): ThrottleBuilder
53
    {
54 41
        return new ThrottleBuilder($this, ...$resource);
55
    }
56
}
57