ForbidBannedUser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 19 6
1
<?php
2
3
/*
4
 * This file is part of Laravel Ban.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Ban\Http\Middleware;
15
16
use Closure;
17
use Cog\Contracts\Ban\Bannable as BannableContract;
18
use Illuminate\Contracts\Auth\Guard;
19
20
class ForbidBannedUser
21
{
22
    /**
23
     * The Guard implementation.
24
     *
25
     * @var \Illuminate\Contracts\Auth\Guard
26
     */
27
    protected $auth;
28
29
    /**
30
     * @param \Illuminate\Contracts\Auth\Guard $auth
31
     */
32
    public function __construct(Guard $auth)
33
    {
34
        $this->auth = $auth;
35
    }
36
37
    /**
38
     * Handle an incoming request.
39
     *
40
     * @param \Illuminate\Http\Request $request
41
     * @param \Closure $next
42
     * @return mixed
43
     *
44
     * @throws \Exception
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        $user = $this->auth->user();
49
50
        if ($user && $user instanceof BannableContract && $user->isBanned()) {
51
            $redirectUrl = config('ban.redirect_url', null);
52
            $errors = [
53
                'login' => 'This account is blocked.',
54
            ];
55
56
            $responseCode = $request->header('X-Inertia') ? 303 : 302;
57
            if ($redirectUrl === null) {
58
                return redirect()->back($responseCode)->withInput()->withErrors($errors);
59
            } else {
60
                return redirect($redirectUrl, $responseCode)->withInput()->withErrors($errors);
61
            }
62
        }
63
64
        return $next($request);
65
    }
66
}
67