Passed
Push — master ( 876065...f10f6d )
by Kevin
01:15
created

ThrottleBuilder::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\Governator;
4
5
/**
6
 * Fluent interface for configuring and creating throttles.
7
 *
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class ThrottleBuilder
11
{
12
    private ThrottleFactory $factory;
13
    private array $resource;
14
    private ?int $limit = null;
15
    private ?int $ttl = null;
16
17 44
    public function __construct(ThrottleFactory $factory, string ...$resource)
18
    {
19 44
        $this->factory = $factory;
20 44
        $this->resource = $resource;
21 44
    }
22
23 1
    public function with(string ...$resource): self
24
    {
25 1
        $this->resource = \array_merge($this->resource, $resource);
26
27 1
        return $this;
28
    }
29
30
    /**
31
     * @param int $limit The maximum number of throttle "hits" in its "time window"
32
     */
33 43
    public function allow(int $limit): self
34
    {
35 43
        $this->limit = $limit;
36
37 43
        return $this;
38
    }
39
40
    /**
41
     * @param int $seconds the "time window" for the throttle in seconds
42
     */
43 43
    public function every(int $seconds): self
44
    {
45 43
        $this->ttl = $seconds;
46
47 43
        return $this;
48
    }
49
50
    /**
51
     * Create the throttle for the current configuration.
52
     *
53
     * @throws \LogicException If the limit or TTL was not set
54
     */
55 44
    public function create(): Throttle
56
    {
57 44
        if (null === $this->limit) {
58 1
            throw new \LogicException(\sprintf('You must set a "Limit" for the throttle via "%s::allow($limit)"', self::class));
59
        }
60
61 43
        if (null === $this->ttl) {
62 1
            throw new \LogicException(\sprintf('You must set a "TTL" for the throttle via "%s::every($ttl)"', self::class));
63
        }
64
65 42
        if (empty($this->resource)) {
66 1
            throw new \LogicException('The resource for the throttle cannot be blank.');
67
        }
68
69 41
        return $this->factory->create(\implode('', $this->resource), $this->limit, $this->ttl);
70
    }
71
72
    /**
73
     * @see Throttle::acquire()
74
     */
75 30
    public function acquire(int $blockFor = 0): Quota
76
    {
77 30
        return $this->create()->acquire($blockFor);
78
    }
79
80
    /**
81
     * @see Throttle::hit()
82
     */
83 2
    public function hit(): Quota
84
    {
85 2
        return $this->create()->hit();
86
    }
87
88
    /**
89
     * @see Throttle::status()
90
     */
91 1
    public function status(): Quota
92
    {
93 1
        return $this->create()->status();
94
    }
95
96
    /**
97
     * Create the throttle for the current configuration and resets it.
98
     *
99
     * @see Throttle::reset()
100
     */
101 10
    public function reset(): void
102
    {
103 10
        $this->create()->reset();
104 10
    }
105
}
106