Passed
Push — main ( a8595a...1afaaf )
by Garbuz
03:24
created

AccessTokenRepository::isMorph()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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|null $expiration
142
     *
143
     * @return bool
144
     */
145
    public function prolongationAccessTokenById(int $token_id, ?DateTime $expiration = null): 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|null $expiration
155
     *
156
     * @return bool
157
     */
158
    public function prolongationAccessToken(string $token, ?DateTime $expiration = null): 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|null $expiration
169
     *
170
     * @return bool
171
     */
172
    public function prolongationAccessTokenByUser(int $user_id, string $user_type, ?DateTime $expiration = null): 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
     * Получить список токенов по ID пользователя
181
     *
182
     * @param int    $user_id
183
     * @param string $user_type
184
     *
185
     * @return Collection|null
186
     */
187
    public function getAccessTokenByUser(int $user_id, string $user_type): ?Collection
188
    {
189
        return AccessToken::where('user_id', $user_id)
190
            ->where('user_type', $user_type)
191
            ->orderByDesc('expiration')->get();
192
    }
193
194
    /**
195
     * Получить токен по ID
196
     *
197
     * @param int $token_id
198
     *
199
     * @return AccessToken|null
200
     */
201
    public function getAccessTokenById(int $token_id): ?AccessToken
202
    {
203
        return AccessToken::where('id', $token_id)->first();
204
    }
205
206
    /**
207
     * Получить данные о токене
208
     *
209
     * @param string $token
210
     *
211
     * @return AccessToken|null
212
     */
213
    public function getAccessToken(string $token): ?AccessToken
214
    {
215
        return AccessToken::where('token', $token)->first();
216
    }
217
218
    /**
219
     * Фиксация последней активности токена
220
     *
221
     * @param int $token_id
222
     *
223
     * @return bool
224
     */
225
    public function setLastUseAccessToken(int $token_id): bool
226
    {
227
        return (bool)AccessToken::where('id', $token_id)->update([
228
            'last_use' => Carbon::now()->subMinutes(),
229
        ]);
230
    }
231
}
232