mirror of
https://github.com/flarum/framework.git
synced 2025-02-15 19:32:44 +08:00
56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Post;
|
|
|
|
use DateTime;
|
|
use Flarum\Post\Event\CheckingForFlooding;
|
|
use Flarum\Post\Exception\FloodingException;
|
|
use Flarum\User\User;
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
|
class Floodgate
|
|
{
|
|
/**
|
|
* @var Dispatcher
|
|
*/
|
|
protected $events;
|
|
|
|
public function __construct(Dispatcher $events)
|
|
{
|
|
$this->events = $events;
|
|
}
|
|
|
|
/**
|
|
* @param User $actor
|
|
* @throws FloodingException
|
|
*/
|
|
public function assertNotFlooding(User $actor)
|
|
{
|
|
if ($this->isFlooding($actor)) {
|
|
throw new FloodingException;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param User $actor
|
|
* @return bool
|
|
*/
|
|
public function isFlooding(User $actor): bool
|
|
{
|
|
$isFlooding = $this->events->until(
|
|
new CheckingForFlooding($actor)
|
|
);
|
|
|
|
return $isFlooding ?? Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
|
|
}
|
|
}
|