1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit; |
27
|
|
|
|
28
|
|
|
use Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface; |
29
|
|
|
use Sunspikes\Ratelimit\Throttle\Throttler\ThrottlerInterface; |
30
|
|
|
use Sunspikes\Ratelimit\Throttle\Factory\FactoryInterface as ThrottlerFactoryInterface; |
31
|
|
|
use Sunspikes\Ratelimit\Throttle\Hydrator\FactoryInterface as HydratorFactoryInterface; |
32
|
|
|
|
33
|
|
|
class RateLimiter |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var CacheAdapterInterface |
37
|
|
|
*/ |
38
|
|
|
protected $adapter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ThrottlerInterface[] |
42
|
|
|
*/ |
43
|
|
|
protected $throttlers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $limit; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $ttl; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ThrottlerFactoryInterface |
57
|
|
|
*/ |
58
|
|
|
protected $throttlerFactory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var HydratorFactoryInterface |
62
|
|
|
*/ |
63
|
|
|
protected $hydratorFactory; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ThrottlerFactoryInterface $throttlerFactory |
67
|
|
|
* @param HydratorFactoryInterface $hydratorFactory |
68
|
|
|
* @param CacheAdapterInterface $cacheAdapter |
69
|
|
|
* @param int $limit |
70
|
|
|
* @param int $ttl |
71
|
|
|
*/ |
72
|
4 |
|
public function __construct( |
73
|
|
|
ThrottlerFactoryInterface $throttlerFactory, |
74
|
|
|
HydratorFactoryInterface $hydratorFactory, |
75
|
|
|
CacheAdapterInterface $cacheAdapter, |
76
|
|
|
$limit, |
77
|
|
|
$ttl |
78
|
|
|
) { |
79
|
4 |
|
$this->throttlerFactory = $throttlerFactory; |
80
|
4 |
|
$this->hydratorFactory = $hydratorFactory; |
81
|
4 |
|
$this->adapter = $cacheAdapter; |
82
|
4 |
|
$this->limit = $limit; |
83
|
4 |
|
$this->ttl = $ttl; |
84
|
4 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Build the throttler for given data |
88
|
|
|
* |
89
|
|
|
* @param mixed $data |
90
|
|
|
* @param int|null $limit |
91
|
|
|
* @param int|null $ttl |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
* |
95
|
|
|
* @throws \InvalidArgumentException |
96
|
|
|
*/ |
97
|
4 |
|
public function get($data, $limit = null, $ttl = null) |
98
|
|
|
{ |
99
|
4 |
|
if (empty($data)) { |
100
|
|
|
throw new \InvalidArgumentException('Invalid data, please check the data.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
$limit = null === $limit ? $this->limit : $limit; |
104
|
4 |
|
$ttl = null === $ttl ? $this->ttl : $ttl; |
105
|
|
|
|
106
|
|
|
// Create the data object |
107
|
4 |
|
$dataObject = $this->hydratorFactory->make($data)->hydrate($data, $limit, $ttl); |
108
|
|
|
|
109
|
4 |
|
if (!isset($this->throttlers[$dataObject->getKey()])) { |
110
|
4 |
|
$this->throttlers[$dataObject->getKey()] = $this->throttlerFactory->make($dataObject, $this->adapter); |
111
|
4 |
|
} |
112
|
|
|
|
113
|
4 |
|
return $this->throttlers[$dataObject->getKey()]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|