Skip to content

Instantly share code, notes, and snippets.

@1e4
Created September 11, 2018 19:15
Show Gist options
  • Save 1e4/663c087c65f59896a37d945ff3175023 to your computer and use it in GitHub Desktop.
Save 1e4/663c087c65f59896a37d945ff3175023 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
class TimerCheck
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $timer)
{
if(!$timer || !$request->has('id'))
return response()->json([
'message' => "Invalid action",
'errors' => [
'action' => 'An invalid action was taken'
]
], 422);
switch($timer)
{
case 'crimes':
$timer = "crimes-timer-{$request->id}";
break;
}
$user_timer = request()->user()->timers->where('key', $timer)->first();
if(!$user_timer)
return $next($request);
else
{
if($user_timer->value->lte(new Carbon()))
return $next($request);
}
return response()->json([
'message' => "Please wait",
'errors' => [
'cooldown' => 'You must wait before performing this action again'
]
], 422);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment