Completed
Pull Request — master (#63)
by
unknown
02:40
created

LogsOutBannedUser::handle()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.6111
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
     * @throws \Exception
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        $user = $this->auth->user();
49
        $redirect_url = config('ban.redirect_url', null);
50
        $errors = [
51
            'login' => 'This account is blocked.',
52
        ];
53
54
        if ($user && $user instanceof BannableContract && $user->isBanned()) {
55
            if ($this->auth instanceof StatefulGuardContract) {
56
                // TODO: Cover with tests
57
                $this->auth->logout();
58
            }
59
            
60
            f($redirect_url === null){
61
                return redirect()->back()->withInput()->withErrors($errors);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_RETURN on line 61 at column 16
Loading history...
62
            }
63
            else{
64
                return redirect($redirect_url)->withInput()->withErrors($errors);
65
            }
66
        }
67
68
        return $next($request);
69
    }
70
}
71