User   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
eloc 20
c 3
b 1
f 0
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A hasInject() 0 3 1
A get() 0 19 3
A getConfig() 0 3 1
A getModel() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace xiaodi\JWTAuth;
6
7
use think\App;
8
use think\Model;
9
use xiaodi\JWTAuth\Exception\JWTException;
10
11
class User
12
{
13
    private $inject = false;
14
    private $model;
15
16
    public function __construct(App $app)
17
    {
18
        $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
20
        $config = $this->getConfig();
21
        foreach ($config as $key => $v) {
22
            $this->$key = $v;
23
        }
24
    }
25
26
    public function getConfig()
27
    {
28
        return $this->app->config->get('jwt.user', []);
29
    }
30
31
    /**
32
     * 是否开启注入.
33
     *
34
     * @return bool
35
     */
36
    public function hasInject()
37
    {
38
        return $this->inject;
39
    }
40
41
    /**
42
     * 获取 用户模型文件.
43
     *
44
     * @return string
45
     */
46
    public function getModel()
47
    {
48
        return $this->model;
49
    }
50
51
    /**
52
     * 获取 具用登录信息的用户模型.
53
     *
54
     * @return Model
55
     */
56
    public function get(): Model
57
    {
58
        $token = $this->app->jwt->getToken();
59
60
        if (!$token) {
61
            throw new JWTException('未登录.', 500);
62
        }
63
64
        if (!$this->hasInject()) {
65
            throw new JWTException('未开启注入功能.', 500);
66
        }
67
68
        $uid = $token->getClaim($this->app->jwt->getUniqidKey());
69
70
        $namespace = $this->getModel();
71
        $model = new $namespace();
72
        $user = $model->findOrFail($uid);
73
74
        return $user;
75
    }
76
}
77