1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace Garbuzivan\Laraveltokens\Traits; |
||||||
6 | |||||||
7 | use DateTime; |
||||||
8 | use Exception; |
||||||
9 | use Garbuzivan\Laraveltokens\Config; |
||||||
10 | use Garbuzivan\Laraveltokens\Exceptions\UserNotExistsException; |
||||||
11 | use Garbuzivan\Laraveltokens\Interfaces\AccessTokenRepositoryInterface; |
||||||
12 | use Garbuzivan\Laraveltokens\Models\AccessToken; |
||||||
13 | |||||||
14 | trait ManagerAccessTokenTrait |
||||||
15 | { |
||||||
16 | /** |
||||||
17 | * @var Config $config |
||||||
18 | */ |
||||||
19 | protected Config $config; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * @var AccessTokenRepositoryInterface |
||||||
23 | */ |
||||||
24 | protected AccessTokenRepositoryInterface $accessTokenRepository; |
||||||
25 | |||||||
26 | /** |
||||||
27 | * Создать токен |
||||||
28 | * |
||||||
29 | * @param string $title - заголовок токена |
||||||
30 | * @param int $user_id - ID клиента |
||||||
31 | * @param DateTime|null $expiration - до когда действует токен, null - бессрочно |
||||||
32 | * @param string|null $user_type - класс полиморфной связи, null == App\Models\User |
||||||
33 | * @param string|null $token - токен, null == автоматическая генерация токена |
||||||
34 | * |
||||||
35 | * @return AccessToken |
||||||
36 | * @throws UserNotExistsException |
||||||
37 | * @throws Exception |
||||||
38 | */ |
||||||
39 | public function createAccessToken( |
||||||
40 | string $title, |
||||||
41 | ?DateTime $expiration = null, |
||||||
42 | int $user_id, |
||||||
43 | ?string $user_type = null, |
||||||
44 | ?string $token = null |
||||||
45 | ): AccessToken { |
||||||
46 | $user_type = !is_null($user_type) ? $user_type : $this->/** @scrutinizer ignore-call */ getDefaultMorph(); |
||||||
47 | $array = explode('\\', $user_type); |
||||||
48 | $type = end($array); |
||||||
49 | $token = is_null($token) || mb_strlen($token) < 32 ? |
||||||
50 | $this->generateToken( |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
51 | ['uid' => $user_id, 'type' => $type], |
||||||
52 | ['exp' => $expiration] |
||||||
53 | ) : $token; |
||||||
54 | $user_type = is_null($user_type) ? $this->/** @scrutinizer ignore-call */ getDefaultMorph() : $user_type; |
||||||
55 | $this->isMorph($user_id, $user_type); |
||||||
0 ignored issues
–
show
It seems like
isMorph() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
56 | $tokenDB = $this->accessTokenRepository->createAccessToken( |
||||||
57 | $title, |
||||||
58 | $expiration, |
||||||
59 | $user_id, |
||||||
60 | $user_type, |
||||||
61 | $this->/** @scrutinizer ignore-call */ getTokenDb($token) |
||||||
62 | ); |
||||||
63 | $tokenDB->token = $token; |
||||||
64 | return $tokenDB; |
||||||
65 | } |
||||||
66 | |||||||
67 | /** |
||||||
68 | * Удалить токен по ID токена |
||||||
69 | * |
||||||
70 | * @param int $token_id - ID токена |
||||||
71 | * |
||||||
72 | * @return bool |
||||||
73 | */ |
||||||
74 | public function deleteAccessTokenById(int $token_id): bool |
||||||
75 | { |
||||||
76 | return $this->accessTokenRepository->deleteAccessTokenById($token_id); |
||||||
77 | } |
||||||
78 | |||||||
79 | /** |
||||||
80 | * Удалить токен |
||||||
81 | * |
||||||
82 | * @param string $token |
||||||
83 | * |
||||||
84 | * @return bool |
||||||
85 | */ |
||||||
86 | public function deleteAccessToken(string $token): bool |
||||||
87 | { |
||||||
88 | return $this->accessTokenRepository->deleteAccessToken($token); |
||||||
89 | } |
||||||
90 | |||||||
91 | /** |
||||||
92 | * Удалить все токены пользователя по id пользователя |
||||||
93 | * |
||||||
94 | * @param int $user_id |
||||||
95 | * @param string $user_type |
||||||
96 | * |
||||||
97 | * @return bool |
||||||
98 | */ |
||||||
99 | public function deleteAccessTokenByUser(int $user_id, string $user_type): bool |
||||||
100 | { |
||||||
101 | return $this->accessTokenRepository->deleteAccessTokenByUser($user_id, $user_type); |
||||||
102 | } |
||||||
103 | |||||||
104 | /** |
||||||
105 | * Деактивировать токен (прекратить срок действия токена) по ID токена |
||||||
106 | * |
||||||
107 | * @param int $token_id - ID токена |
||||||
108 | * |
||||||
109 | * @return bool |
||||||
110 | */ |
||||||
111 | public function deactivationAccessTokenById(int $token_id): bool |
||||||
112 | { |
||||||
113 | return $this->accessTokenRepository->deactivationAccessTokenById($token_id); |
||||||
114 | } |
||||||
115 | |||||||
116 | /** |
||||||
117 | * Деактивировать токен (прекратить срок действия токена) по токену |
||||||
118 | * |
||||||
119 | * @param string $token |
||||||
120 | * |
||||||
121 | * @return bool |
||||||
122 | */ |
||||||
123 | public function deactivationAccessToken(string $token): bool |
||||||
124 | { |
||||||
125 | return $this->accessTokenRepository->deactivationAccessToken($token); |
||||||
126 | } |
||||||
127 | |||||||
128 | /** |
||||||
129 | * Деактивировать токен (прекратить срок действия токена) по id пользователя |
||||||
130 | * |
||||||
131 | * @param int $user_id |
||||||
132 | * @param string $user_type |
||||||
133 | * |
||||||
134 | * @return bool |
||||||
135 | */ |
||||||
136 | public function deactivationAccessTokenByUser(int $user_id, string $user_type): bool |
||||||
137 | { |
||||||
138 | return $this->accessTokenRepository->deactivationAccessTokenByUser($user_id, $user_type); |
||||||
139 | } |
||||||
140 | |||||||
141 | /** |
||||||
142 | * Продлить срок действия токена по id токена |
||||||
143 | * |
||||||
144 | * @param int $token_id |
||||||
145 | * @param DateTime|null $expiration |
||||||
146 | * |
||||||
147 | * @return bool |
||||||
148 | */ |
||||||
149 | public function prolongationAccessTokenById(int $token_id, ?DateTime $expiration = null): bool |
||||||
150 | { |
||||||
151 | return $this->accessTokenRepository->prolongationAccessTokenById($token_id, $expiration); |
||||||
152 | } |
||||||
153 | |||||||
154 | /** |
||||||
155 | * Продлить срок действия всех токенов по id пользователя |
||||||
156 | * |
||||||
157 | * @param int $user_id |
||||||
158 | * @param string $user_type |
||||||
159 | * @param DateTime|null $expiration |
||||||
160 | * |
||||||
161 | * @return bool |
||||||
162 | */ |
||||||
163 | public function prolongationAccessTokenByUser(int $user_id, string $user_type, ?DateTime $expiration = null): bool |
||||||
164 | { |
||||||
165 | return $this->accessTokenRepository->prolongationAccessTokenByUser($user_id, $user_type, $expiration); |
||||||
166 | } |
||||||
167 | |||||||
168 | /** |
||||||
169 | * Продлить срок действия токена по токену |
||||||
170 | * |
||||||
171 | * @param string $token |
||||||
172 | * @param DateTime|null $expiration |
||||||
173 | * |
||||||
174 | * @return bool |
||||||
175 | */ |
||||||
176 | public function prolongationAccessToken(string $token, ?DateTime $expiration = null): bool |
||||||
177 | { |
||||||
178 | return $this->accessTokenRepository->prolongationAccessToken($token, $expiration); |
||||||
179 | } |
||||||
180 | } |
||||||
181 |