Passed
Push — main ( 12e7e2...8f14aa )
by Garbuz
03:18
created

ManagerAccessTokenTrait::prolongationAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens\Traits;
6
7
use DateTime;
8
use Garbuzivan\Laraveltokens\Config;
9
use Garbuzivan\Laraveltokens\Exceptions\UserNotExistsException;
10
use Garbuzivan\Laraveltokens\Interfaces\AccessTokenRepositoryInterface;
11
use Garbuzivan\Laraveltokens\Models\AccessToken;
12
13
trait ManagerAccessTokenTrait
14
{
15
    /**
16
     * @var Config $config
17
     */
18
    protected Config $config;
19
20
    /**
21
     * @var AccessTokenRepositoryInterface
22
     */
23
    protected AccessTokenRepositoryInterface $accessTokenRepository;
24
25
    /**
26
     * Создать токен
27
     *
28
     * @param string        $title      - заголовок токена
29
     * @param int           $user_id    - ID клиента
30
     * @param DateTime|null $expiration - до когда действует токен, null - бессрочно
31
     * @param string|null   $user_type  - класс полиморфной связи, null == App\Models\User
32
     * @param string|null   $token      - токен, null == автоматическая генерация токена
33
     *
34
     * @return AccessToken
35
     * @throws UserNotExistsException
36
     */
37
    public function createAccessToken(
38
        string    $title,
39
        ?DateTime $expiration = null,
40
        int       $user_id,
41
        ?string   $user_type = null,
42
        ?string   $token = null
43
    ): AccessToken {
44
        $token = is_null($token) || mb_strlen($token) < 32 ? $this->generateAccessToken() : $token;
0 ignored issues
show
Bug introduced by
It seems like generateAccessToken() 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 ignore-call  annotation

44
        $token = is_null($token) || mb_strlen($token) < 32 ? $this->/** @scrutinizer ignore-call */ generateAccessToken() : $token;
Loading history...
45
        $user_type = is_null($user_type) ? $this->getDefaultMorph() : $user_type;
0 ignored issues
show
Bug introduced by
It seems like getDefaultMorph() 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 ignore-call  annotation

45
        $user_type = is_null($user_type) ? $this->/** @scrutinizer ignore-call */ getDefaultMorph() : $user_type;
Loading history...
46
        $this->accessTokenRepository->isMorph($user_id, $user_type);
47
        $tokenDB = $this->accessTokenRepository->createAccessToken(
48
            $title,
49
            $expiration,
50
            $user_id,
51
            $user_type,
52
            $this->getAccessTokenDb($token)
0 ignored issues
show
Bug introduced by
It seems like getAccessTokenDb() 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 ignore-call  annotation

52
            $this->/** @scrutinizer ignore-call */ 
53
                   getAccessTokenDb($token)
Loading history...
53
        );
54
        $tokenDB->token = $token;
55
        return $tokenDB;
56
    }
57
58
    /**
59
     * Удалить токен по ID токена
60
     *
61
     * @param int $token_id - ID токена
62
     *
63
     * @return bool
64
     */
65
    public function deleteAccessTokenById(int $token_id): bool
66
    {
67
        return $this->accessTokenRepository->deleteAccessTokenById($token_id);
68
    }
69
70
    /**
71
     * Удалить токен
72
     *
73
     * @param string $token
74
     *
75
     * @return bool
76
     */
77
    public function deleteAccessToken(string $token): bool
78
    {
79
        return $this->accessTokenRepository->deleteAccessToken($token);
80
    }
81
82
    /**
83
     * Удалить все токены пользователя по id пользователя
84
     *
85
     * @param int    $user_id
86
     * @param string $user_type
87
     *
88
     * @return bool
89
     */
90
    public function deleteAccessTokenByUser(int $user_id, string $user_type): bool
91
    {
92
        return $this->accessTokenRepository->deleteAccessTokenByUser($user_id, $user_type);
93
    }
94
95
    /**
96
     * Деактивировать токен (прекратить срок действия токена) по ID токена
97
     *
98
     * @param int $token_id - ID токена
99
     *
100
     * @return bool
101
     */
102
    public function deactivationAccessTokenById(int $token_id): bool
103
    {
104
        return $this->accessTokenRepository->deactivationAccessTokenById($token_id);
105
    }
106
107
    /**
108
     * Деактивировать токен (прекратить срок действия токена) по токену
109
     *
110
     * @param string $token
111
     *
112
     * @return bool
113
     */
114
    public function deactivationAccessToken(string $token): bool
115
    {
116
        return $this->accessTokenRepository->deactivationAccessToken($token);
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 $this->accessTokenRepository->deactivationAccessTokenByUser($user_id, $user_type);
130
    }
131
132
    /**
133
     * Продлить срок действия токена по id токена
134
     *
135
     * @param int           $token_id
136
     * @param DateTime|null $expiration
137
     *
138
     * @return bool
139
     */
140
    public function prolongationAccessTokenById(int $token_id, ?DateTime $expiration = null): bool
141
    {
142
        return $this->accessTokenRepository->prolongationAccessTokenById($token_id, $expiration);
143
    }
144
145
    /**
146
     * Продлить срок действия всех токенов по id пользователя
147
     *
148
     * @param int           $user_id
149
     * @param string        $user_type
150
     * @param DateTime|null $expiration
151
     *
152
     * @return bool
153
     */
154
    public function prolongationAccessTokenByUser(int $user_id, string $user_type, ?DateTime $expiration = null): bool
155
    {
156
        return $this->accessTokenRepository->prolongationAccessTokenByUser($user_id, $user_type, $expiration);
157
    }
158
159
    /**
160
     * Продлить срок действия токена по токену
161
     *
162
     * @param string        $token
163
     * @param DateTime|null $expiration
164
     *
165
     * @return bool
166
     */
167
    public function prolongationAccessToken(string $token, ?DateTime $expiration = null): bool
168
    {
169
        return $this->accessTokenRepository->prolongationAccessToken($token, $expiration);
170
    }
171
}
172