1 | <?php |
||
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) |
||
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) |
||
54 | |||
55 | /** |
||
56 | * Sets the violation handler. |
||
57 | * @param ViolationHandlerInterface $violationHandler the violation handler to use |
||
58 | */ |
||
59 | public function setViolationHandler(ViolationHandlerInterface $violationHandler) |
||
63 | |||
64 | /** |
||
65 | * Returns the violation handler. |
||
66 | * @return ViolationHandlerInterface the violation handler, if set |
||
67 | */ |
||
68 | public function getViolationHandler() |
||
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) |
||
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) |
||
113 | |||
114 | } |
||
115 |