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

ManagerGlobalTokenTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 16
c 1
b 0
f 0
dl 0
loc 108
ccs 0
cts 19
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prolongationGlobalToken() 0 3 1
A deactivationGlobalToken() 0 3 1
A deleteGlobalTokenById() 0 3 1
A prolongationGlobalTokenById() 0 3 1
A deleteGlobalToken() 0 3 1
A createGlobalToken() 0 13 3
A deactivationGlobalTokenById() 0 3 1
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\Interfaces\GlobalTokenRepositoryInterface;
10
use Garbuzivan\Laraveltokens\Models\GlobalToken;
11
12
trait ManagerGlobalTokenTrait
13
{
14
    /**
15
     * @var Config $config
16
     */
17
    protected Config $config;
18
19
    /**
20
     * @var GlobalTokenRepositoryInterface
21
     */
22
    protected GlobalTokenRepositoryInterface $GlobalTokenRepository;
23
24
    /**
25
     * Создать токен
26
     *
27
     * @param string        $title      - заголовок токена
28
     * @param DateTime|null $expiration - до когда действует токен, null - бессрочно
29
     * @param string|null   $token      - токен, null == автоматическая генерация токена
30
     *
31
     * @return GlobalToken
32
     */
33
    public function createGlobalToken(
34
        string    $title,
35
        ?DateTime $expiration = null,
36
        ?string   $token = null
37
    ): GlobalToken {
38
        $token = is_null($token) || mb_strlen($token) < 32 ? $this->generateGlobalToken() : $token;
0 ignored issues
show
Bug introduced by
It seems like generateGlobalToken() 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

38
        $token = is_null($token) || mb_strlen($token) < 32 ? $this->/** @scrutinizer ignore-call */ generateGlobalToken() : $token;
Loading history...
39
        $tokenDB = $this->GlobalTokenRepository->createGlobalToken(
40
            $title,
41
            $expiration,
42
            $this->getGlobalTokenDb($token)
0 ignored issues
show
Bug introduced by
It seems like getGlobalTokenDb() 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

42
            $this->/** @scrutinizer ignore-call */ 
43
                   getGlobalTokenDb($token)
Loading history...
43
        );
44
        $tokenDB->token = $token;
45
        return $tokenDB;
46
    }
47
48
    /**
49
     * Удалить токен по ID токена
50
     *
51
     * @param int $token_id - ID токена
52
     *
53
     * @return bool
54
     */
55
    public function deleteGlobalTokenById(int $token_id): bool
56
    {
57
        return $this->GlobalTokenRepository->deleteGlobalTokenById($token_id);
58
    }
59
60
    /**
61
     * Удалить токен
62
     *
63
     * @param string $token
64
     *
65
     * @return bool
66
     */
67
    public function deleteGlobalToken(string $token): bool
68
    {
69
        return $this->GlobalTokenRepository->deleteGlobalToken($token);
70
    }
71
72
    /**
73
     * Деактивировать токен (прекратить срок действия токена) по ID токена
74
     *
75
     * @param int $token_id - ID токена
76
     *
77
     * @return bool
78
     */
79
    public function deactivationGlobalTokenById(int $token_id): bool
80
    {
81
        return $this->GlobalTokenRepository->deactivationGlobalTokenById($token_id);
82
    }
83
84
    /**
85
     * Деактивировать токен (прекратить срок действия токена) по токену
86
     *
87
     * @param string $token
88
     *
89
     * @return bool
90
     */
91
    public function deactivationGlobalToken(string $token): bool
92
    {
93
        return $this->GlobalTokenRepository->deactivationGlobalToken($token);
94
    }
95
96
    /**
97
     * Продлить срок действия токена по id токена
98
     *
99
     * @param int           $token_id
100
     * @param DateTime|null $expiration
101
     *
102
     * @return bool
103
     */
104
    public function prolongationGlobalTokenById(int $token_id, ?DateTime $expiration = null): bool
105
    {
106
        return $this->GlobalTokenRepository->prolongationGlobalTokenById($token_id, $expiration);
0 ignored issues
show
Bug introduced by
It seems like $expiration can also be of type null; however, parameter $expiration of Garbuzivan\Laraveltokens...gationGlobalTokenById() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

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

106
        return $this->GlobalTokenRepository->prolongationGlobalTokenById($token_id, /** @scrutinizer ignore-type */ $expiration);
Loading history...
107
    }
108
109
    /**
110
     * Продлить срок действия токена по токену
111
     *
112
     * @param string        $token
113
     * @param DateTime|null $expiration
114
     *
115
     * @return bool
116
     */
117
    public function prolongationGlobalToken(string $token, ?DateTime $expiration = null): bool
118
    {
119
        return $this->GlobalTokenRepository->prolongationGlobalToken($token, $expiration);
0 ignored issues
show
Bug introduced by
It seems like $expiration can also be of type null; however, parameter $expiration of Garbuzivan\Laraveltokens...olongationGlobalToken() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

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

119
        return $this->GlobalTokenRepository->prolongationGlobalToken($token, /** @scrutinizer ignore-type */ $expiration);
Loading history...
120
    }
121
}
122