Passed
Push — master ( e9cdac...1d7587 )
by Gabriel
02:26
created

UserTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 28%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 21
c 1
b 0
f 1
dl 0
loc 51
rs 10
ccs 7
cts 25
cp 0.28

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 6 2
A checkAccessToken() 0 6 2
A doAuthentication() 0 7 1
A initIdentifier() 0 3 1
A generateToken() 0 7 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
        $token = $this->token();
40
        $token->setPrivateKey(app('hello.keys.private'));
41
        $this->access_token = $token->getIdentifier();
42
        $this->access_jwt = $token->__toString();
43
        $this->doAuthenticationTrait();
44
    }
45
46
    public function checkAccessToken()
47
    {
48
        if (empty($this->access_token)) {
49
            $token = $this->token();
50
            $this->access_token = $token->getIdentifier();
51
            $this->access_jwt = $token->convertToJWT(app('hello.keys.private'))->__toString();
52
        }
53
    }
54
55 1
    protected function initIdentifier()
56
    {
57 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

57
        $this->setIdentifier(UsersResolvers::identifier(/** @scrutinizer ignore-type */ $this));
Loading history...
58 1
    }
59
60
    /**
61
     * @return \ByTIC\Hello\Models\AccessTokens\Token|null
62
     */
63
    protected function generateToken()
64
    {
65
        $userTokens = ModelsHelper::accessTokens()->getValidUserTokens($this, ClientsManager::get());
66
        if (count($userTokens) < 1) {
67
            return $this->createToken($this->generateTokenName());
68
        }
69
        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...
70
    }
71
}
72