Authenticate::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Auth\Middleware;
12
13
use Closure;
14
use Longman\Platfourm\Contracts\Auth\AuthUserService as AuthUserServiceContract;
15
16
class Authenticate
17
{
18
19
    protected $authService;
20
21
    public function __construct(AuthUserServiceContract $authService)
22
    {
23
        $this->authService = $authService;
24
    }
25
26
    public function handle($request, Closure $next)
27
    {
28
29
        if ($this->authService->guest()) {
30
            if ($request->wantsJson()) {
31
                $headers = [
32
                    'WWW-Authenticate' => 'Auth',
33
                ];
34
                return response('Unauthorized.', 401, $headers);
35
            } else {
36
                return $this->redirect();
37
            }
38
        }
39
40
        return $next($request);
41
    }
42
43
    protected function redirect()
44
    {
45
        $url = app()->isAdmin() ? admin_url('login') : site_url('login');
46
        return redirect()->guest($url)->with(['error' => 'You must log in first']);
0 ignored issues
show
Bug introduced by
It seems like $url defined by app()->isAdmin() ? admin...n') : site_url('login') on line 45 can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Illuminate\Routing\Redirector::guest() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
    }
48
49
}
50