ThrottleFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 47
ccs 10
cts 10
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A for() 0 3 1
A __construct() 0 4 1
A throttle() 0 3 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