Passed
Pull Request — master (#53)
by
unknown
05:24
created

OwnTournament   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10
c 2
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 18 8
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Tournament;
6
use Closure;
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Support\Facades\Auth;
9
10
class OwnTournament
11
{
12
    /**
13
     * Check the ownership of tournaments
14
     *
15
     * @param  \Illuminate\Http\Request $request
16
     * @param  \Closure $next
17
     * @return mixed
18
     * @throws AuthorizationException
19
     */
20 11
    public function handle($request, Closure $next)
21
    {
22
        // Check if user own tournament
23
24 11
        if (Auth::check() && ($request->tournament != null || $request->tournamentId != null)) {
25 10
            $userLogged = Auth::user();
26 10
            $tournaments = $userLogged->tournaments;
27 10
            $tournament = $request->tournament;
28 10
            if (!$tournament instanceof Tournament) {
29 1
                $tournament = Tournament::where('slug', $tournament)->first();
30
            }
31 10
            if ($tournament != null) {
32 9
                if (!$tournaments->contains($tournament) && !$userLogged->isSuperAdmin()) {
33 2
                    throw new AuthorizationException();
34
                }
35
            }
36
        }
37 10
        return $next($request);
38
    }
39
}
40