Projekt

Obecné

Profil

Stáhnout (2.68 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Cache;
4

    
5
use Illuminate\Contracts\Cache\Repository as Cache;
6

    
7
class RateLimiter
8
{
9
    /**
10
     * The cache store implementation.
11
     *
12
     * @var \Illuminate\Contracts\Cache\Repository
13
     */
14
    protected $cache;
15

    
16
    /**
17
     * Create a new rate limiter instance.
18
     *
19
     * @param  \Illuminate\Contracts\Cache\Repository  $cache
20
     * @return void
21
     */
22
    public function __construct(Cache $cache)
23
    {
24
        $this->cache = $cache;
25
    }
26

    
27
    /**
28
     * Determine if the given key has been "accessed" too many times.
29
     *
30
     * @param  string  $key
31
     * @param  int  $maxAttempts
32
     * @param  int  $decayMinutes
33
     * @return bool
34
     */
35
    public function tooManyAttempts($key, $maxAttempts, $decayMinutes = 1)
36
    {
37
        if ($this->cache->has($key.':lockout')) {
38
            return true;
39
        }
40

    
41
        if ($this->attempts($key) > $maxAttempts) {
42
            $this->cache->add($key.':lockout', time() + ($decayMinutes * 60), $decayMinutes);
43

    
44
            $this->resetAttempts($key);
45

    
46
            return true;
47
        }
48

    
49
        return false;
50
    }
51

    
52
    /**
53
     * Increment the counter for a given key for a given decay time.
54
     *
55
     * @param  string  $key
56
     * @param  int  $decayMinutes
57
     * @return int
58
     */
59
    public function hit($key, $decayMinutes = 1)
60
    {
61
        $this->cache->add($key, 1, $decayMinutes);
62

    
63
        return (int) $this->cache->increment($key);
64
    }
65

    
66
    /**
67
     * Get the number of attempts for the given key.
68
     *
69
     * @param  string  $key
70
     * @return mixed
71
     */
72
    public function attempts($key)
73
    {
74
        return $this->cache->get($key, 0);
75
    }
76

    
77
    /**
78
     * Reset the number of attempts for the given key.
79
     *
80
     * @param  string  $key
81
     * @return mixed
82
     */
83
    public function resetAttempts($key)
84
    {
85
        return $this->cache->forget($key);
86
    }
87

    
88
    /**
89
     * Get the number of retries left for the given key.
90
     *
91
     * @param  string  $key
92
     * @param  int  $maxAttempts
93
     * @return int
94
     */
95
    public function retriesLeft($key, $maxAttempts)
96
    {
97
        $attempts = $this->attempts($key);
98

    
99
        return $attempts === 0 ? $maxAttempts : $maxAttempts - $attempts + 1;
100
    }
101

    
102
    /**
103
     * Clear the hits and lockout for the given key.
104
     *
105
     * @param  string  $key
106
     * @return void
107
     */
108
    public function clear($key)
109
    {
110
        $this->resetAttempts($key);
111

    
112
        $this->cache->forget($key.':lockout');
113
    }
114

    
115
    /**
116
     * Get the number of seconds until the "key" is accessible again.
117
     *
118
     * @param  string  $key
119
     * @return int
120
     */
121
    public function availableIn($key)
122
    {
123
        return $this->cache->get($key.':lockout') - time();
124
    }
125
}
(11-11/19)