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

OwnTournament::handle()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 10
nc 7
nop 2
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 8
rs 8.4444
c 2
b 0
f 0
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