Created
September 11, 2018 19:15
-
-
Save 1e4/663c087c65f59896a37d945ff3175023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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