. */ namespace App\Services; use App\Account; use Carbon\Carbon; class BlockingService { public function __construct(public Account $account) { } public function checkBlock(): bool { if ($this->account->blocked) { return true; } $isBlockable = $this->isBlockable(); if ($isBlockable) { $this->account->blocked = true; $this->account->save(); } return $isBlockable; } public function isBlockable(): bool { if (config('app.blocking_amount_events_authorized_during_period') == 0) { return false; } return $this->countEvents() >= config('app.blocking_amount_events_authorized_during_period'); } private function countEvents(): int { $events = 0; $events += $this->account->recoveryCodes()->where( 'created_at', '>', Carbon::now()->subMinutes(config('app.blocking_time_period_check'))->toDateTimeString() )->count(); $events += $this->account->phoneChangeCodes()->where( 'created_at', '>', Carbon::now()->subMinutes(config('app.blocking_time_period_check'))->toDateTimeString() )->count(); $events += $this->account->emailChangeCodes()->where( 'created_at', '>', Carbon::now()->subMinutes(config('app.blocking_time_period_check'))->toDateTimeString() )->count(); return $events; } }