Passed
Push — main ( 69451c...874877 )
by Garbuz
03:25
created

AccessTokenRepository::setLastUseAccessToken()   A

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\Exceptions\UserNotExistsException;
10
use Garbuzivan\Laraveltokens\Interfaces\AccessTokenRepositoryInterface;
11
use Garbuzivan\Laraveltokens\Models\AccessToken;
12
use Illuminate\Database\Eloquent\Collection;
13
use Illuminate\Support\Facades\DB;
14
15
class AccessTokenRepository implements AccessTokenRepositoryInterface
16
{
17
    /**
18
     * Создать токен
19
     *
20
     * @param string        $title      - заголовок токена
21
     * @param DateTime|null $expiration - до когда действует токен, null - бессрочно
22
     * @param int           $user_id    - ID клиента
23
     * @param string        $user_type  - класс полиморфной связи
24
     * @param string        $token      - токен
25
     *
26
     * @return AccessToken
27
     */
28
    public function createAccessToken(
29
        string    $title,
30
        ?DateTime $expiration = null,
31
        int       $user_id,
32
        string    $user_type,
33
        string    $token
34
    ): AccessToken {
35
        return AccessToken::create([
36
            'token'      => $token,
37
            'user_id'    => $user_id,
38
            'user_type'  => $user_type,
39
            'title'      => $title,
40
            'expiration' => $expiration,
41
        ]);
42
    }
43
44
    /**
45
     * Удалить токен по ID токена
46
     *
47
     * @param int $token_id - ID токена
48
     *
49
     * @return bool
50
     */
51
    public function deleteAccessTokenById(int $token_id): bool
52
    {
53
        return (bool)AccessToken::where('id', $token_id)->delete();
54
    }
55
56
    /**
57
     * Удалить токен
58
     *
59
     * @param string $token
60
     *
61
     * @return bool
62
     */
63
    public function deleteAccessToken(string $token): bool
64
    {
65
        return (bool)AccessToken::where('token', $token)->delete();
66
    }
67
68
    /**
69
     * Удалить все токены пользователя по id пользователя
70
     *
71
     * @param int    $user_id
72
     * @param string $user_type
73
     *
74
     * @return bool
75
     */
76
    public function deleteAccessTokenByUser(int $user_id, string $user_type): bool
77
    {
78
        return (bool)AccessToken::where('user_id', $user_id)->where('user_type', $user_type)->delete();
79
    }
80
81
    /**
82
     * Очистить таблицу токенов
83
     *
84
     * @return bool
85
     */
86
    public function deleteAllAccessToken(): bool
87
    {
88
        DB::table('access_tokens')->truncate();
89
        return true;
90
    }
91
92
    /**
93
     * Деактивировать токен (прекратить срок действия токена) по ID токена
94
     *
95
     * @param int $token_id - ID токена
96
     *
97
     * @return bool
98
     */
99
    public function deactivationAccessTokenById(int $token_id): bool
100
    {
101
        return (bool)AccessToken::where('id', $token_id)->update([
102
            'expiration' => Carbon::now()->subMinutes(),
103
        ]);
104
    }
105
106
    /**
107
     * Деактивировать токен (прекратить срок действия токена) по токену
108
     *
109
     * @param string $token
110
     *
111
     * @return bool
112
     */
113
    public function deactivationAccessToken(string $token): bool
114
    {
115
        return (bool)AccessToken::where('token', $token)->update([
116
            'expiration' => Carbon::now()->subMinutes(),
117
        ]);
118
    }
119
120
    /**
121
     * Деактивировать токен (прекратить срок действия токена) по id пользователя
122
     *
123
     * @param int    $user_id
124
     * @param string $user_type
125
     *
126
     * @return bool
127
     */
128
    public function deactivationAccessTokenByUser(int $user_id, string $user_type): bool
129
    {
130
        return (bool)AccessToken::where('user_id', $user_id)
131
            ->where('user_type', $user_type)
132
            ->update([
133
                'expiration' => Carbon::now()->subMinutes(),
134
            ]);
135
    }
136
137
    /**
138
     * Продлить срок действия токена по id токена
139
     *
140
     * @param int      $token_id
141
     * @param DateTime $expiration
142
     *
143
     * @return bool
144
     */
145
    public function prolongationAccessTokenById(int $token_id, DateTime $expiration): bool
146
    {
147
        return (bool)AccessToken::where('id', $token_id)->update(['expiration' => $expiration]);
148
    }
149
150
    /**
151
     * Продлить срок действия токена
152
     *
153
     * @param string   $token
154
     * @param DateTime $expiration
155
     *
156
     * @return bool
157
     */
158
    public function prolongationAccessToken(string $token, DateTime $expiration): bool
159
    {
160
        return (bool)AccessToken::where('token', $token)->update(['expiration' => $expiration]);
161
    }
162
163
    /**
164
     * Продлить срок действия всех токенов по id пользователя
165
     *
166
     * @param int      $user_id
167
     * @param string   $user_type
168
     * @param DateTime $expiration
169
     *
170
     * @return bool
171
     */
172
    public function prolongationAccessTokenByUser(int $user_id, string $user_type, DateTime $expiration): bool
173
    {
174
        return (bool)AccessToken::where('user_id', $user_id)
175
            ->where('user_type', $user_type)
176
            ->update(['expiration' => $expiration]);
177
    }
178
179
    /**
180
     * Редактировать токен
181
     *
182
     * @param int      $token_id   - ID токена
183
     * @param string   $title      - заголовок токена
184
     * @param DateTime $expiration - до когда действует токен
185
     * @param int      $user_id    - ID пользователя
186
     * @param string   $user_type  - полиморфная связь
187
     * @param string   $token      - токен
188
     *
189
     * @return bool
190
     */
191
    public function editAccessToken(
192
        int      $token_id,
193
        string   $title,
194
        DateTime $expiration,
195
        int      $user_id,
196
        string   $user_type,
197
        string   $token
198
    ): bool {
199
        return (bool)AccessToken::where('id', $token_id)->update([
200
            'token'      => $token,
201
            'user_id'    => $user_id,
202
            'user_type'  => $user_type,
203
            'title'      => $title,
204
            'expiration' => $expiration,
205
        ]);
206
    }
207
208
    /**
209
     * Получить список токенов по ID пользователя
210
     *
211
     * @param int    $user_id
212
     * @param string $user_type
213
     *
214
     * @return Collection|null
215
     */
216
    public function getAccessTokenByUser(int $user_id, string $user_type): ?Collection
217
    {
218
        return AccessToken::where('user_id', $user_id)
219
            ->where('user_type', $user_type)
220
            ->orderByDesc('expiration')->get();
221
    }
222
223
    /**
224
     * Получить токен по ID
225
     *
226
     * @param int $token_id
227
     *
228
     * @return AccessToken|null
229
     */
230
    public function getAccessTokenById(int $token_id): ?AccessToken
231
    {
232
        return AccessToken::where('id', $token_id)->first();
233
    }
234
235
    /**
236
     * Получить данные о токене
237
     *
238
     * @param string $token
239
     *
240
     * @return AccessToken|null
241
     */
242
    public function getAccessToken(string $token): ?AccessToken
243
    {
244
        return AccessToken::where('token', $token)->first();
245
    }
246
247
    /**
248
     * Фиксация последней активности токена
249
     *
250
     * @param string $token
251
     *
252
     * @return bool
253
     */
254
    public function setLastUseAccessToken(string $token): bool
255
    {
256
        return (bool)AccessToken::where('token', $token)->update([
257
            'last_use' => Carbon::now()->subMinutes(),
258
        ]);
259
    }
260
261
    /**
262
     * Проверка полиморфной связи
263
     *
264
     * @param int    $user_id
265
     * @param string $user_type
266
     *
267
     * @return void
268
     * @throws UserNotExistsException
269
     */
270
    public function isMorph(int $user_id, string $user_type): void
271
    {
272
        $user = app($user_type)->where('id', $user_id)->first();
0 ignored issues
show
Bug introduced by
The method where() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

272
        $user = app($user_type)->/** @scrutinizer ignore-call */ where('id', $user_id)->first();

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...
273
        if (is_null($user)) {
274
            throw new UserNotExistsException;
275
        }
276
    }
277
}
278