Completed
Push — master ( 7eb1a4...254269 )
by LEUNG
03:41
created

Jwt   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 17
c 7
b 1
f 0
dl 0
loc 34
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace xiaodi\JWTAuth\Middleware;
6
7
use think\App;
8
use xiaodi\JWTAuth\BearerToken;
0 ignored issues
show
Bug introduced by
The type xiaodi\JWTAuth\BearerToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use xiaodi\JWTAuth\Exception\JWTException;
10
use xiaodi\JWTAuth\Jwt as Ac;
11
12
/**
13
 * 中间件.
14
 */
15
class Jwt
16
{
17
    private $jwt;
18
    private $app;
19
    private $bearerToken;
20
21
    public function __construct(App $app, BearerToken $bearerToken, Ac $jwt)
22
    {
23
        $this->jwt = $jwt;
24
        $this->app = $app;
25
        $this->bearerToken = $bearerToken;
26
    }
27
28
    public function handle($request, \Closure $next)
29
    {
30
        $token = $this->bearerToken->getToken();
31
        if (true === $this->jwt->verify($token)) {
0 ignored issues
show
introduced by
The condition true === $this->jwt->verify($token) is always true.
Loading history...
32
            // 自动注入用户模型
33
            if ($this->jwt->injectUser()) {
34
                $user = $this->jwt->user();
35
                // 路由注入
36
                $request->user = $user;
37
38
                // 依赖注入
39
                $model = $this->jwt->userModel();
0 ignored issues
show
Bug introduced by
The method userModel() does not exist on xiaodi\JWTAuth\Jwt. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
                /** @scrutinizer ignore-call */ 
40
                $model = $this->jwt->userModel();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
                $this->app->bind($model, $user);
41
            }
42
43
            $request->jwt = $this->jwt;
44
45
            return $next($request);
46
        }
47
48
        throw new JWTException('Token 验证不通过', 401);
49
    }
50
}
51