AuthorizeContest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 4
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Repositories\ContestRepository;
6
use Illuminate\Http\Request;
7
8
class AuthorizeContest
9
{
10
    /**
11
     * @param Request $request
12
     * @param         $next
13
     *
14
     * @return \Illuminate\Http\RedirectResponse
15
     */
16
    public function handle($request, $next)
17
    {
18
        $route = app('router')->getRoutes()->match($request);
19
        $contest = $route->parameter('contest');
20
21
        /** @var \App\Entities\Contest $contest */
22
        $contest = app(ContestRepository::class)->findOrFail($contest);
23
24
        if (! $contest->isAvailable()) {
25
            return back()->withErrors('Contest not found!');
26
        }
27
28
        if ($contest->isPublic() || can_attend($contest)) {
29
            return $next($request);
30
        }
31
32
        $errorMessage = 'You do not have privilege access contest '.$contest->id;
33
34
        return redirect(route('contest.index'))->withErrors($errorMessage);
35
    }
36
}
37