AccessTokenRepository::setLastUseAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens\Repositories;
6
7
use Carbon\Carbon;
8
use DateTime;
9
use Garbuzivan\Laraveltokens\Interfaces\AccessTokenRepositoryInterface;
10
use Garbuzivan\Laraveltokens\Models\AccessToken;
11
use Illuminate\Database\Eloquent\Collection;
12
use Illuminate\Support\Facades\DB;
13
14
class AccessTokenRepository implements AccessTokenRepositoryInterface
15
{
16
    /**
17
     * Создать токен
18
     *
19
     * @param string        $title      - заголовок токена
20
     * @param DateTime|null $expiration - до когда действует токен, null - бессрочно
21
     * @param int           $user_id    - ID клиента
22
     * @param string        $user_type  - класс полиморфной связи
23
     * @param string        $token      - токен
24
     *
25
     * @return AccessToken
26
     */
27
    public function createAccessToken(
28
        string    $title,
29
        ?DateTime $expiration = null,
30
        int       $user_id,
31
        string    $user_type,
32
        string    $token
33
    ): AccessToken {
34
        return AccessToken::create([
35
            'token'      => $token,
36
            'user_id'    => $user_id,
37
            'user_type'  => $user_type,
38
            'title'      => $title,
39
            'expiration' => $expiration,
40
        ]);
41
    }
42
43
    /**
44
     * Удалить токен по ID токена
45
     *
46
     * @param int $token_id - ID токена
47
     *
48
     * @return bool
49
     */
50
    public function deleteAccessTokenById(int $token_id): bool
51
    {
52
        return (bool)AccessToken::where('id', $token_id)->delete();
53
    }
54
55
    /**
56
     * Удалить токен
57
     *
58
     * @param string $token
59
     *
60
     * @return bool
61
     */
62
    public function deleteAccessToken(string $token): bool
63
    {
64
        return (bool)AccessToken::where('token', $token)->delete();
65
    }
66
67
    /**
68
     * Удалить все токены пользователя по id пользователя
69
     *
70
     * @param int    $user_id
71
     * @param string $user_type
72
     *
73
     * @return bool
74
     */
75
    public function deleteAccessTokenByUser(int $user_id, string $user_type): bool
76
    {
77
        return (bool)AccessToken::where('user_id', $user_id)->where('user_type', $user_type)->delete();
78
    }
79
80
    /**
81
     * Очистить таблицу токенов
82
     *
83
     * @return bool
84
     */
85
    public function deleteAllAccessToken(): bool
86
    {
87
        DB::table('access_tokens')->truncate();
88
        return true;
89
    }
90
91
    /**
92
     * Деактивировать токен (прекратить срок действия токена) по ID токена
93
     *
94
     * @param int $token_id - ID токена
95
     *
96
     * @return bool
97
     */
98
    public function deactivationAccessTokenById(int $token_id): bool
99
    {
100
        return (bool)AccessToken::where('id', $token_id)->update([
101
            'expiration' => Carbon::now()->subMinutes(),
102
        ]);
103
    }
104
105
    /**
106
     * Деактивировать токен (прекратить срок действия токена) по токену
107
     *
108
     * @param string $token
109
     *
110
     * @return bool
111
     */
112
    public function deactivationAccessToken(string $token): bool
113
    {
114
        return (bool)AccessToken::where('token', $token)->update([
115
            'expiration' => Carbon::now()->subMinutes(),
116
        ]);
117
    }
118
119
    /**
120
     * Деактивировать токен (прекратить срок действия токена) по id пользователя
121
     *
122
     * @param int    $user_id
123
     * @param string $user_type
124
     *
125
     * @return bool
126
     */
127
    public function deactivationAccessTokenByUser(int $user_id, string $user_type): bool
128
    {
129
        return (bool)AccessToken::where('user_id', $user_id)
130
            ->where('user_type', $user_type)
131
            ->update([
132
                'expiration' => Carbon::now()->subMinutes(),
133
            ]);
134
    }
135
136
    /**
137
     * Продлить срок действия токена по id токена
138
     *
139
     * @param int           $token_id
140
     * @param DateTime|null $expiration
141
     *
142
     * @return bool
143
     */
144
    public function prolongationAccessTokenById(int $token_id, ?DateTime $expiration = null): bool
145
    {
146
        return (bool)AccessToken::where('id', $token_id)->update(['expiration' => $expiration]);
147
    }
148
149
    /**
150
     * Продлить срок действия токена
151
     *
152
     * @param string        $token
153
     * @param DateTime|null $expiration
154
     *
155
     * @return bool
156
     */
157
    public function prolongationAccessToken(string $token, ?DateTime $expiration = null): bool
158
    {
159
        return (bool)AccessToken::where('token', $token)->update(['expiration' => $expiration]);
160
    }
161
162
    /**
163
     * Продлить срок действия всех токенов по id пользователя
164
     *
165
     * @param int           $user_id
166
     * @param string        $user_type
167
     * @param DateTime|null $expiration
168
     *
169
     * @return bool
170
     */
171
    public function prolongationAccessTokenByUser(int $user_id, string $user_type, ?DateTime $expiration = null): bool
172
    {
173
        return (bool)AccessToken::where('user_id', $user_id)
174
            ->where('user_type', $user_type)
175
            ->update(['expiration' => $expiration]);
176
    }
177
178
    /**
179
     * Получить список токенов по ID пользователя
180
     *
181
     * @param int    $user_id
182
     * @param string $user_type
183
     *
184
     * @return Collection|null
185
     */
186
    public function getAccessTokenByUser(int $user_id, string $user_type): ?Collection
187
    {
188
        return AccessToken::where('user_id', $user_id)
189
            ->where('user_type', $user_type)
190
            ->orderByDesc('expiration')->get();
191
    }
192
193
    /**
194
     * Получить токен по ID
195
     *
196
     * @param int $token_id
197
     *
198
     * @return AccessToken|null
199
     */
200
    public function getAccessTokenById(int $token_id): ?AccessToken
201
    {
202
        return AccessToken::where('id', $token_id)->first();
203
    }
204
205
    /**
206
     * Получить данные о токене
207
     *
208
     * @param string $token
209
     *
210
     * @return AccessToken|null
211
     */
212
    public function getAccessToken(string $token): ?AccessToken
213
    {
214
        return AccessToken::where('token', $token)->first();
215
    }
216
217
    /**
218
     * Фиксация последней активности токена
219
     *
220
     * @param int $token_id
221
     *
222
     * @return bool
223
     */
224
    public function setLastUseAccessToken(int $token_id): bool
225
    {
226
        return (bool)AccessToken::where('id', $token_id)->update([
227
            'last_use' => Carbon::now()->subMinutes(),
228
        ]);
229
    }
230
}
231