LogsOutBannedUser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
use Illuminate\Contracts\Auth\StatefulGuard as StatefulGuardContract;
20
21
class LogsOutBannedUser
22
{
23
    /**
24
     * The Guard implementation.
25
     *
26
     * @var \Illuminate\Contracts\Auth\Guard
27
     */
28
    protected $auth;
29
30
    /**
31
     * @param \Illuminate\Contracts\Auth\Guard $auth
32
     */
33
    public function __construct(Guard $auth)
34
    {
35
        $this->auth = $auth;
36
    }
37
38
    /**
39
     * Handle an incoming request.
40
     *
41
     * @param \Illuminate\Http\Request $request
42
     * @param \Closure $next
43
     * @return mixed
44
     *
45
     * @throws \Exception
46
     */
47
    public function handle($request, Closure $next)
48
    {
49
        $user = $this->auth->user();
50
51
        if ($user && $user instanceof BannableContract && $user->isBanned()) {
52
            if ($this->auth instanceof StatefulGuardContract) {
53
                // TODO: Cover with tests
54
                $this->auth->logout();
55
            }
56
57
            $redirectUrl = config('ban.redirect_url', null);
58
            $errors = [
59
                'login' => 'This account is blocked.',
60
            ];
61
62
            $responseCode = $request->header('X-Inertia') ? 303 : 302;
63
            if ($redirectUrl === null) {
64
                return redirect()->back($responseCode)->withInput()->withErrors($errors);
65
            } else {
66
                return redirect($redirectUrl, $responseCode)->withInput()->withErrors($errors);
67
            }
68
        }
69
70
        return $next($request);
71
    }
72
}
73