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

UserTrait::tokensDelete()   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 0
crap 2
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