How do I use the flood interface? I'm working on a two-factor login form that sends a text message to the user. With a &quot;did not recieve message&quot; 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()-&gt;register('eventName', 1800, $user-&gt;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()-&gt;register('send message', 900, $user-&gt;getUuid());
  $token = 12345;
  $this-&gt;sendMessage($token);
}

private function sendMessage($token) {
  $is_allowed = \Drupal::flood()-&gt;isAllowed('send message', 3, 900, $user-&gt;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?
They are two different parameters that share the same name (and can share the same value by default). Per the docs for FloodInterface:

::register():



int $window: (optional) Number of seconds before this event expires. Defaults to 3600 (1 hour). Typically uses the same value as the isAllowed() $window parameter. Expired events are purged on cron run to prevent the flood table from growing indefinitely.




::isAllowed():



int $window: (optional) Number of seconds in the time window for this event (default is 3600 seconds, or 1 hour).





The register() parameter specifies how long that flood event will be stored (remembered) after the time of registration before ::garbageCollection() clears it out. The isAllowed() parameter specifies the max number of instances that can occur within that window before isAllowed() will return FALSE