Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

Banned   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 35
rs 10
c 1
b 0
f 1
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 26 5
1
<?php
2
3
namespace App\Http\Middleware\User;
4
5
use Closure;
6
use Auth;
7
8
class Banned
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next)
18
    {
19
        //check login
20
        if(!Auth::check()){
21
            return $next($request);
22
        }
23
        $user = Auth::user();
24
        $banned = $user->banneds()->orderBy('removed_at', 'desc')->first();
25
        //check if there are any banned records
26
        if(empty($banned)) {
27
            return $next($request);
28
        }
29
        //check the time of the last record
30
        if(strtotime($banned->removed_at) <= time()){
31
            return $next($request);
32
        }
33
        //return error page
34
        if($request->method() == 'GET'){
35
            return response()->view('errors.451',[
36
                'description' => "Your account is currently blocked and will remain so until {$banned->removed_at}. Here's why: {$banned->reason}",
37
            ]);
38
        }else{
39
            return response()->json([
40
                'ret' => 451,
41
                'desc' => 'Unavailable For Legal Reasons',
42
                'data' => "Your account is currently blocked and will remain so until {$banned->removed_at}. Here's why: {$banned->reason}"
43
            ]);
44
        }
45
    }
46
}
47