UserTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 26.92%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 9
eloc 21
c 2
b 1
f 1
dl 0
loc 55
ccs 7
cts 26
cp 0.2692
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doAuthentication() 0 4 1
A initIdentifier() 0 3 1
A initAccessToken() 0 6 1
A checkAccessToken() 0 4 2
A getIdentifier() 0 6 2
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;
25
    use EntityTrait;
26
27
    /**
28
     * @return string
29
     */
30 3
    public function getIdentifier()
31
    {
32 3
        if (empty($this->identifier)) {
33 1
            $this->initIdentifier();
34
        }
35 3
        return $this->identifier;
36
    }
37
38
    public function doAuthentication()
39
    {
40
        $this->initAccessToken();
41
        $this->doAuthenticationTrait();
42
    }
43
44
    public function checkAccessToken()
45
    {
46
        if (empty($this->access_token)) {
47
            $this->initAccessToken();
48
        }
49
    }
50
51
    protected function initAccessToken()
52
    {
53
        $token = $this->token();
54
        $token->setPrivateKey(app('hello.keys.private'));
55
        $this->access_token = $token->getIdentifier();
56
        $this->access_jwt = $token->__toString();
57
    }
58
59 1
    protected function initIdentifier()
60
    {
61 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

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