Passed
Push — master ( 1d7587...a5dfb5 )
by Gabriel
02:49
created

UserTrait::initAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Models\Users\Traits;
4
5
use ByTIC\Auth\Models\Users\Traits\AbstractUserTrait;
6
use ByTIC\Hello\Models\Clients\PersonalAccess\ClientsManager;
7
use ByTIC\Hello\Models\Traits\HasApiTokensTrait;
8
use ByTIC\Hello\Models\Users\Resolvers\UsersResolvers;
9
use ByTIC\Hello\Utility\ModelsHelper;
10
use League\OAuth2\Server\Entities\Traits\EntityTrait;
11
12
/**
13
 * Trait UserTrait
14
 * @package ByTIC\Hello\Models\Users\
15
 *
16
 * @property string $access_token
17
 * @property string $access_jwt
18
 */
19
trait UserTrait
20
{
21
    use AbstractUserTrait {
22
        doAuthentication as doAuthenticationTrait;
23
    }
24
    use HasApiTokensTrait, EntityTrait;
25
26
    /**
27
     * @return string
28
     */
29 3
    public function getIdentifier()
30
    {
31 3
        if (empty($this->identifier)) {
32 1
            $this->initIdentifier();
33
        }
34 3
        return $this->identifier;
35
    }
36
37
    public function doAuthentication()
38
    {
39
        $this->initAccessToken();
40
        $this->doAuthenticationTrait();
41
    }
42
43
    public function checkAccessToken()
44
    {
45
        if (empty($this->access_token)) {
46
            $this->initAccessToken();
47
        }
48
    }
49
50
    protected function initAccessToken()
51
    {
52
        $token = $this->token();
53
        $token->setPrivateKey(app('hello.keys.private'));
54
        $this->access_token = $token->getIdentifier();
55
        $this->access_jwt = $token->__toString();
56
    }
57
58 1
    protected function initIdentifier()
59
    {
60 1
        $this->setIdentifier(UsersResolvers::identifier($this));
0 ignored issues
show
Bug introduced by
$this of type ByTIC\Hello\Models\Users\Traits\UserTrait is incompatible with the type Nip\Records\Record expected by parameter $entity of ByTIC\Hello\Models\Users...Resolvers::identifier(). ( Ignorable by Annotation )

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

60
        $this->setIdentifier(UsersResolvers::identifier(/** @scrutinizer ignore-type */ $this));
Loading history...
61 1
    }
62
63
    /**
64
     * @return \ByTIC\Hello\Models\AccessTokens\Token|null
65
     */
66
    protected function generateToken()
67
    {
68
        $userTokens = ModelsHelper::accessTokens()->getValidUserTokens($this, ClientsManager::get());
69
        if (count($userTokens) < 1) {
70
            return $this->createToken($this->generateTokenName());
71
        }
72
        return $userTokens->current();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $userTokens->current() also could return the type Nip\Records\AbstractMode...ections\Collection|true which is incompatible with the documented return type ByTIC\Hello\Models\AccessTokens\Token|null.
Loading history...
73
    }
74
}
75