VerifyAuthOrWhitelisted::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
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
use WITR\Services\Whitelist;
6
7
class VerifyAuthOrWhitelisted {
8
9
	/**
10
	 * The Guard implementation.
11
	 *
12
	 * @var Guard
13
	 */
14
	protected $auth;
15
16
	/**
17
	 * The Whitelist implementation.
18
	 *
19
	 * @var Whitelist
20
	 */
21
	protected $whitelist;
22
23
	/**
24
	 * Create a new filter instance.
25
	 *
26
	 * @param  Guard  $auth
27
	 * @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...
28
	 */
29
	public function __construct(Guard $auth, Whitelist $whitelist)
30
	{
31
		$this->auth = $auth;
32
		$this->whitelist = $whitelist;
33
	}
34
35
	/**
36
	 * Handle an incoming request.
37
	 *
38
	 * @param  \Illuminate\Http\Request  $request
39
	 * @param  \Closure  $next
40
	 * @return mixed
41
	 */
42
	public function handle($request, Closure $next)
43
	{
44
		if ($this->auth->guest() && !$this->whitelist->inRange($request))
45
		{
46
			if ($request->ajax())
47
			{
48
				return response('Unauthorized.', 401);
49
			}
50
			else
51
			{
52
				return redirect()->guest('auth/login');
53
			}
54
		}
55
56
		return $next($request);
57
	}
58
59
}
60