Completed
Push — master ( 204804...0ed652 )
by Anton
11s
created

LogsOutBannedUser::handle()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 2
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
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
namespace Cog\Laravel\Ban\Http\Middleware;
13
14
use Closure;
15
use Cog\Contracts\Ban\Bannable as BannableContract;
16
use Illuminate\Contracts\Auth\Guard;
17
use Illuminate\Contracts\Auth\StatefulGuard as StatefulGuardContract;
18
19
/**
20
 * Class LogsOutBannedUser.
21
 *
22
 * @package Cog\Laravel\Ban\Http\Middleware
23
 */
24
class LogsOutBannedUser
25
{
26
    /**
27
     * The Guard implementation.
28
     *
29
     * @var \Illuminate\Contracts\Auth\Guard
30
     */
31
    protected $auth;
32
33
    /**
34
     * @param \Illuminate\Contracts\Auth\Guard $auth
35
     */
36
    public function __construct(Guard $auth)
37
    {
38
        $this->auth = $auth;
39
    }
40
41
    /**
42
     * Handle an incoming request.
43
     *
44
     * @param \Illuminate\Http\Request $request
45
     * @param \Closure $next
46
     * @return mixed
47
     * @throws \Exception
48
     */
49
    public function handle($request, Closure $next)
50
    {
51
        $user = $this->auth->user();
52
53
        if ($user && $user instanceof BannableContract && $user->isBanned()) {
54
            if ($this->auth instanceof StatefulGuardContract) {
55
                // TODO: Cover with tests
56
                $this->auth->logout();
57
            }
58
59
            return redirect()->back()->withInput()->withErrors([
60
                'login' => 'This account is blocked.',
61
            ]);
62
        }
63
64
        return $next($request);
65
    }
66
}
67