1
|
|
|
<?php |
2
|
|
|
namespace BehEh\Flaps; |
3
|
|
|
|
4
|
|
|
use BehEh\Flaps\Violation\HttpViolationHandler; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Associates a number of throttling strategies with a storage handler and a violation handler. |
8
|
|
|
* |
9
|
|
|
* @since 0.1 |
10
|
|
|
* @author Benedict Etzel <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Flap |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var StorageInterface |
16
|
|
|
*/ |
17
|
|
|
protected $storage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @param StorageInterface $storage |
27
|
|
|
* @param string $name |
28
|
|
|
*/ |
29
|
|
|
public function __construct(StorageInterface $storage, $name) |
30
|
|
|
{ |
31
|
|
|
$this->storage = $storage; |
32
|
|
|
$this->name = $name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ThrottlingStrategyInterface[] |
37
|
|
|
*/ |
38
|
|
|
protected $throttlingStrategies = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ViolationHandlerInterface |
42
|
|
|
*/ |
43
|
|
|
protected $violationHandler = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Adds the throttling strategy to the internal list of throttling strategies. |
47
|
|
|
* @param BehEh\Flaps\ViolationHandlerInterface |
48
|
|
|
*/ |
49
|
|
|
public function pushThrottlingStrategy(ThrottlingStrategyInterface $throttlingStrategy) |
50
|
|
|
{ |
51
|
|
|
$throttlingStrategy->setStorage($this->storage); |
52
|
|
|
$this->throttlingStrategies[] = $throttlingStrategy; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets the violation handler. |
57
|
|
|
* @param ViolationHandlerInterface $violationHandler the violation handler to use |
58
|
|
|
*/ |
59
|
|
|
public function setViolationHandler(ViolationHandlerInterface $violationHandler) |
60
|
|
|
{ |
61
|
|
|
$this->violationHandler = $violationHandler; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the violation handler. |
66
|
|
|
* @return ViolationHandlerInterface the violation handler, if set |
67
|
|
|
*/ |
68
|
|
|
public function getViolationHandler() |
69
|
|
|
{ |
70
|
|
|
return $this->violationHandler; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Ensures a violation handler is set. If none is set, default to HttpViolationHandler. |
75
|
|
|
*/ |
76
|
|
|
protected function ensureViolationHandler() |
77
|
|
|
{ |
78
|
|
|
if ($this->violationHandler === null) { |
79
|
|
|
$this->violationHandler = new HttpViolationHandler(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Requests violation handling from the violation handler if identifier violates any throttling strategy. |
85
|
|
|
* @param string $identifier |
86
|
|
|
* @return mixed true, if no throttling strategy is violated, otherwise the return value of the violation handler's handleViolation |
87
|
|
|
*/ |
88
|
|
|
public function limit($identifier) |
89
|
|
|
{ |
90
|
|
|
if ($this->isViolator($identifier)) { |
91
|
|
|
$this->ensureViolationHandler(); |
92
|
|
|
return $this->violationHandler->handleViolation(); |
93
|
|
|
} |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks whether the entity violates any throttling strategy. |
99
|
|
|
* @param string $identifier a unique string identifying the entity to check |
100
|
|
|
* @return bool true if the entity violates any strategy |
101
|
|
|
*/ |
102
|
|
|
public function isViolator($identifier) |
103
|
|
|
{ |
104
|
|
|
$violation = false; |
105
|
|
|
foreach ($this->throttlingStrategies as $throttlingStrategy) { |
106
|
|
|
/** @var ThrottlingStrategyInterface $throttlingHandler */ |
107
|
|
|
if ($throttlingStrategy->isViolator($this->name.':'.$identifier)) { |
108
|
|
|
$violation = true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $violation; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|