Authenticate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 10
c 3
b 0
f 1
dl 0
loc 44
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 14 3
1
<?php
2
3
namespace SwooleTW\Http\Websocket\Middleware;
4
5
use Closure;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Contracts\Auth\Factory as Auth;
8
9
/**
10
 * Class Authenticate
11
 */
12
class Authenticate
13
{
14
    /**
15
     * The authentication factory instance.
16
     *
17
     * @var \Illuminate\Contracts\Auth\Factory|mixed
18
     */
19
    protected $auth;
20
21
    /**
22
     * Create a new middleware instance.
23
     *
24
     * @param  \Illuminate\Contracts\Auth\Factory $auth
25
     *
26
     * @return void
27
     */
28
    public function __construct(Auth $auth)
29
    {
30
        $this->auth = $auth;
31
    }
32
33
    /**
34
     * Handle an incoming request.
35
     *
36
     * @param  \Illuminate\Http\Request $request
37
     * @param  \Closure $next
38
     *
39
     * @return mixed
40
     *
41
     */
42
    public function handle($request, Closure $next)
43
    {
44
        try {
45
            $this->auth->setRequest($request);
46
            if ($user = $this->auth->authenticate()) {
47
                $request->setUserResolver(function () use ($user) {
48
                    return $user;
49
                });
50
            }
51
        } catch (AuthenticationException $e) {
52
            // do nothing
53
        }
54
55
        return $next($request);
56
    }
57
}
58