Passed
Push — master ( 7747fb...9e97d7 )
by hugh
08:11
created

PersonalAccessToken::saveInstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 18
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 26
rs 9.6666
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hugh.li
5
 * Date: 2022/6/14
6
 * Time: 17:41
7
 */
8
9
namespace HughCube\Laravel\OTS\Sanctum;
10
11
use Aliyun\OTS\Consts\ColumnTypeConst;
12
use Aliyun\OTS\Consts\RowExistenceExpectationConst;
13
use Aliyun\OTS\OTSClientException;
14
use Aliyun\OTS\OTSServerException;
15
use Carbon\Carbon;
16
use Exception;
17
use HughCube\Base\Base;
18
use HughCube\Laravel\OTS\Connection;
19
use HughCube\Laravel\OTS\Ots;
20
21
/**
22
 * @property string $tokenable_type
23
 * @property int $tokenable_id
24
 * @property string $name
25
 * @property string $token
26
 * @property array $abilities
27
 * @property Carbon|null $last_used_at
28
 * @property Carbon|null $created_at
29
 * @property Carbon|null $updated_at
30
 */
31
class PersonalAccessToken extends \Laravel\Sanctum\PersonalAccessToken
32
{
33
    /**
34
     * @throws Exception
35
     */
36
    protected static function getOts(): Connection
37
    {
38
        return Ots::connection();
39
    }
40
41
    public static function getOtsTable(): string
42
    {
43
        return 'personal_access_tokens';
44
    }
45
46
    public static function getApp(): string
47
    {
48
        return config('app.name');
49
    }
50
51
    /**
52
     * 最后一次使用时间在15天之前
53
     *
54
     * @param  bool  $isValid  验证的当前值
55
     * @return bool
56
     */
57
    public function isValidAccessToken(bool $isValid = true): bool
0 ignored issues
show
Unused Code introduced by
The parameter $isValid is not used and could be removed. ( Ignorable by Annotation )

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

57
    public function isValidAccessToken(/** @scrutinizer ignore-unused */ bool $isValid = true): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        if (!$this->last_used_at instanceof Carbon) {
60
            return true;
61
        }
62
63
        return $this->last_used_at->gt(Carbon::now()->subSeconds(3600 * 24 * 15));
64
    }
65
66
    /**
67
     * @throws OTSServerException
68
     * @throws OTSClientException
69
     * @throws Exception
70
     */
71
    public static function findToken($token): ?PersonalAccessToken
72
    {
73
        $request = [
74
            'table_name' => static::getOtsTable(),
75
            'primary_key' => [
76
                ['token', hash('sha256', $token)],
77
                ['app', static::getApp()]
78
            ],
79
            'max_versions' => 1
80
        ];
81
82
        $row = Ots::parseRow(static::getOts()->getRow($request));
0 ignored issues
show
Bug introduced by
The method getRow() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

82
        $row = Ots::parseRow(static::getOts()->/** @scrutinizer ignore-call */ getRow($request));
Loading history...
83
84
        /** @var static $model */
85
        $model = static::query()->newModelInstance();
86
        $model->forceFill($row);
87
        $model->exists = true;
88
89
        if (is_string($model->abilities)) {
0 ignored issues
show
introduced by
The condition is_string($model->abilities) is always false.
Loading history...
90
            $abilities = json_decode($model->abilities, true);
91
            if (JSON_ERROR_NONE !== json_last_error()) {
92
                $abilities = [];
93
            }
94
            $model->abilities = $abilities;
95
        }
96
97
        return $model;
98
    }
99
100
    /**
101
     * @throws OTSServerException
102
     * @throws OTSClientException
103
     * @throws Exception
104
     */
105
    public function save(array $options = []): bool
106
    {
107
        $this->created_at = $this->created_at ?? Carbon::now();
108
        $this->updated_at = $this->updated_at ?? Carbon::now();
109
110
        if (!$this->exists) {
111
            return $this->saveInstall();
112
        }
113
114
        $this->updated_at = Carbon::now();
115
        $request = [
116
            'table_name' => $this->table,
117
            'condition' => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
118
            'primary_key' => [
119
                ['token', $this->token],
120
                ['app', $this->getApp()]
121
            ],
122
            'update_of_attribute_columns' => [
123
                'PUT' => [
124
                    ['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING],
125
                    ['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING],
126
                    ['name', $this->name, ColumnTypeConst::CONST_STRING],
127
                    ['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING],
128
                    ['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not exist on null. ( Ignorable by Annotation )

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

128
                    ['last_used_at', $this->last_used_at->/** @scrutinizer ignore-call */ getTimestamp(), ColumnTypeConst::CONST_DOUBLE],

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...
129
                    ['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
130
                    ['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
131
                ]
132
            ],
133
        ];
134
135
        static::getOts()->updateRow($request);
0 ignored issues
show
Bug introduced by
The method updateRow() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

135
        static::getOts()->/** @scrutinizer ignore-call */ updateRow($request);
Loading history...
136
137
        return true;
138
    }
139
140
    /**
141
     * @throws OTSServerException
142
     * @throws OTSClientException
143
     * @throws Exception
144
     */
145
    protected function saveInstall(): bool
146
    {
147
        $this->last_used_at = $this->last_used_at ?? Carbon::now();
148
149
        $request = [
150
            'table_name' => $this->table,
151
            'condition' => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST,
152
            'primary_key' => [
153
                ['token', $this->token],
154
                ['app', $this->getApp()]
155
            ],
156
            'attribute_columns' => [
157
                ['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING],
158
                ['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING],
159
                ['name', $this->name, ColumnTypeConst::CONST_STRING],
160
                ['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING],
161
                ['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
162
                ['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not exist on null. ( Ignorable by Annotation )

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

162
                ['created_at', $this->created_at->/** @scrutinizer ignore-call */ getTimestamp(), ColumnTypeConst::CONST_DOUBLE],

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...
163
                ['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE],
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not exist on null. ( Ignorable by Annotation )

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

163
                ['updated_at', $this->updated_at->/** @scrutinizer ignore-call */ getTimestamp(), ColumnTypeConst::CONST_DOUBLE],

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...
164
            ]
165
        ];
166
167
        static::getOts()->putRow($request);
0 ignored issues
show
Bug introduced by
The method putRow() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

167
        static::getOts()->/** @scrutinizer ignore-call */ putRow($request);
Loading history...
168
        $this->exists = true;
169
170
        return true;
171
    }
172
}
173