I'm working on a two-factor login form that sends a text message to the user. With a "did not recieve message" button. I would like to limit the number of times a user can request for a new text message.
I know that I can register flood for a certain event like: \Drupal::flood()->register('eventName', 1800, $user->getUuid())
. The FloodInterface has a isAllowed($name, $threshold, $window = 3600, $identifier = NULL);
method that, I imagine, tests when a user tries to do a certain $name
event, it tests if that event has been done more often than the $threshold
within the $window
. If so then the method returns false
and otherwise true
.
In my example I can register the event somewhere in the chain of events/functions that send the message and also check there if the event isAllowed
?
Something like so:?
public function someFunction() {
\Drupal::flood()->register('send message', 900, $user->getUuid());
$token = 12345;
$this->sendMessage($token);
}
private function sendMessage($token) {
$is_allowed = \Drupal::flood()->isAllowed('send message', 3, 900, $user->getUuid());
if ($is_allowed) {
// Send the message
}
}
What I don't get is why I also have to indicate the $window
in the isAllowed
method when I already assign that in the register method?