AuthorizeEditor::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 15
loc 15
rs 9.2
cc 4
eloc 7
nc 3
nop 2
1
<?php namespace WITR\Http\Middleware;
2
3
use Closure;
4
use Illuminate\Contracts\Auth\Guard;
5
6 View Code Duplication
class AuthorizeEditor {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
7
8
	/**
9
	 * The Guard implementation.
10
	 *
11
	 * @var Guard
12
	 */
13
	protected $auth;
14
15
	/**
16
	 * Create a new filter instance.
17
	 *
18
	 * @param  Guard  $auth
19
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
	 */
21
	public function __construct(Guard $auth)
22
	{
23
		$this->auth = $auth;
24
	}
25
26
	/**
27
	 * Handle an incoming request.
28
	 *
29
	 * @param  \Illuminate\Http\Request  $request
30
	 * @param  \Closure  $next
31
	 * @return mixed
32
	 */
33
	public function handle($request, Closure $next)
34
	{
35
		$user = $this->auth->user();
36
		if ($user->hasRole('editor') || $user->hasRole('admin'))
37
		{
38
			return $next($request);
39
		}
40
41
		if ($request->ajax())
42
		{
43
			return response('Unauthorized.', 401);
44
		}
45
		
46
		return redirect()->route('home');
47
	}
48
49
}
50