Passed
Push — main ( e53ca0...5e6012 )
by Garbuz
02:52
created

UserTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 4
c 2
b 0
f 0
dl 0
loc 29
ccs 0
cts 6
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A tokensDelete() 0 3 1
A tokens() 0 3 1
A tokenCreate() 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\Exceptions\UserNotExistsException;
9
use Garbuzivan\Laraveltokens\Models\Token;
10
use Garbuzivan\Laraveltokens\TokenManager;
11
use Illuminate\Database\Eloquent\Relations\HasMany;
12
13
trait UserTrait
14
{
15
    /**
16
     * @return HasMany
17
     */
18
    public function tokens()
19
    {
20
        return $this->/** @scrutinizer ignore-call */ hasMany(Token::class);
21
    }
22
23
    /**
24
     * Создать новый токен пользователя
25
     * @param string $title
26
     * @param DateTime|null $expiration
27
     * @return Token
28
     * @throws UserNotExistsException
29
     */
30
    public function tokenCreate(string $title = '', ?DateTime $expiration = null): Token
31
    {
32
        return app(TokenManager::class)->create($title, $expiration, $this->id);
33
    }
34
35
    /**
36
     * Удалить все токены пользователя
37
     * @return bool
38
     */
39
    public function tokensDelete(): bool
40
    {
41
        return app(TokenManager::class)->deleteByUser($this->id);
42
    }
43
}
44