UserController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actions() 0 6 1
A actionLogin() 0 10 1
1
<?php
2
3
namespace app\modules\v1\controllers;
4
5
use app\core\exceptions\InternalException;
6
use app\core\exceptions\InvalidArgumentException;
7
use app\core\models\User;
8
use app\core\requests\JoinRequest;
9
use app\core\requests\LoginRequest;
10
use app\core\traits\ServiceTrait;
11
use Yii;
12
13
/**
14
 * User controller for the `v1` module
15
 */
16
class UserController extends ActiveController
17
{
18
    use ServiceTrait;
19
20
    public $modelClass = User::class;
21
    public $noAuthActions = ['join', 'login'];
22
23
    public function actions()
24
    {
25
        $actions = parent::actions();
26
        // 注销系统自带的实现方法
27
        unset($actions['update'], $actions['index'], $actions['delete'], $actions['create']);
28
        return $actions;
29
    }
30
31
    /**
32
     * @return User
33
     * @throws InternalException
34
     * @throws InvalidArgumentException
35
     */
36
    public function actionJoin()
37
    {
38
        $params = Yii::$app->request->bodyParams;
39
        $data = $this->validate(new JoinRequest(), $params);
40
41
        /** @var JoinRequest $data */
42
        return $this->userService->createUser($data);
43
    }
44
45
    /**
46
     * @return string[]
47
     * @throws InvalidArgumentException|\Throwable
48
     */
49
    public function actionLogin()
50
    {
51
        $params = Yii::$app->request->bodyParams;
52
        $this->validate(new LoginRequest(), $params);
53
        $token = $this->userService->getToken();
54
        $user = Yii::$app->user->identity;
55
56
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('user' => $...ken' => (string)$token) returns an array which contains values of type yii\web\IdentityInterface which are incompatible with the documented value type string.
Loading history...
57
            'user' => $user,
58
            'token' => (string)$token,
59
        ];
60
    }
61
62
    public function actionRefreshToken()
63
    {
64
        $user = Yii::$app->user->identity;
65
        $token = $this->userService->getToken();
66
        return [
67
            'user' => $user,
68
            'token' => (string)$token,
69
        ];
70
    }
71
}
72