Passed
Push — master ( 744dd3...6da628 )
by Gabriel
02:56
created

Token   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 17
eloc 39
c 0
b 0
f 0
dl 0
loc 129
rs 10
ccs 30
cts 42
cp 0.7143

12 Methods

Rating   Name   Duplication   Size   Complexity  
A populateFromClient() 0 4 1
A addScopesFromString() 0 7 2
A setExpiresAt() 0 6 1
A getClient() 0 10 2
A setUserId() 0 3 1
A setUserIdentifier() 0 4 1
A castExpireDateTime() 0 4 2
A setIdentifier() 0 4 1
A setScopes() 0 7 2
A insert() 0 4 1
A addScopes() 0 4 2
A update() 0 4 1
1
<?php
2
3
namespace ByTIC\Hello\Models\AccessTokens;
4
5
use ByTIC\Hello\Utility\ModelsHelper;
6
use DateTimeImmutable;
7
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
8
use League\OAuth2\Server\Entities\ClientEntityInterface;
9
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
10
use League\OAuth2\Server\Entities\Traits\EntityTrait;
11
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
12
13
/**
14
 * Class Token
15
 * @package ByTIC\Auth\Models\AccessTokens
16
 *
17
 * @property int $user_id
18
 * @property int $client_id
19
 * @property string $revoked
20
 * @property string $expires_at
21
 */
22
class Token extends \Nip\Records\Record implements AccessTokenEntityInterface
23
{
24
    use EntityTrait {
25
        setIdentifier as setIdentifierTrait;
26
    }
27
    use TokenEntityTrait {
28
        TokenEntityTrait::getClient as getClientTrait;
29
        TokenEntityTrait::setUserIdentifier as setUserIdentifierTrait;
30
    }
31
    use AccessTokenTrait;
32
33
    /**
34
     * @param ClientEntityInterface $clientEntity
35 1
     */
36
    public function populateFromClient(ClientEntityInterface $clientEntity)
37 1
    {
38 1
        $this->setClient($clientEntity);
39 1
        $this->client_id = $clientEntity->getIdentifier();
0 ignored issues
show
Documentation Bug introduced by
The property $client_id was declared of type integer, but $clientEntity->getIdentifier() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
40
    }
41
42
    /**
43
     * @inheritDoc
44 2
     */
45
    public function getClient(): ClientEntityInterface
46 2
    {
47 2
        $client = $this->getClientTrait();
48 1
        if ($client instanceof ClientEntityInterface) {
0 ignored issues
show
introduced by
$client is always a sub-type of League\OAuth2\Server\Ent...s\ClientEntityInterface.
Loading history...
49
            return $client;
50
        }
51 1
        /** @var ClientEntityInterface $relationClient */
52 1
        $relationClient = $this->getRelation('Client')->getResults();
53 1
        $this->setClient($relationClient);
54
        return $relationClient;
55
    }
56
57
    /**
58
     * @inheritDoc
59 1
     */
60
    public function setIdentifier($identifier)
61 1
    {
62 1
        $this->setDataValue('identifier', $identifier);
63 1
        $this->setIdentifierTrait($identifier);
64
    }
65
66
    /**
67
     * @inheritDoc
68 2
     */
69
    public function setExpiresAt($value)
70 2
    {
71 2
        /** @noinspection PhpUnhandledExceptionInspection */
72 2
        $date = new DateTimeImmutable($value);
73 2
        $this->setExpiryDateTime($date);
74
        $this->setDataValue('expires_at', $value);
75
    }
76
77
    /**
78 2
     * @param $identifier
79
     */
80 2
    public function setUserId($identifier)
81 2
    {
82
        $this->setUserIdentifier($identifier);
83
    }
84
85
86
    /**
87 3
     * @param int|string|null $identifier
88
     */
89 3
    public function setUserIdentifier($identifier)
90 3
    {
91 3
        $this->setUserIdentifierTrait($identifier);
92
        $this->setDataValue('user_id', $this->getUserIdentifier());
93
    }
94
95
    /**
96 1
     * @param $scopes
97
     */
98 1
    public function setScopes($scopes)
99
    {
100
        if (is_string($scopes)) {
101 1
            $this->addScopesFromString($scopes);
102
            return;
103
        }
104
        $this->scopes = $scopes;
105
    }
106
107
    /**
108
     * @param $scopes
109
     */
110
    public function addScopesFromString($scopes)
111
    {
112
        $scopes = array_filter(explode(',', $scopes));
113
        foreach ($scopes as $key => $scope) {
114
            $scopes[$key] = ModelsHelper::scopes()->getScopeEntityByIdentifier($scope);
115
        }
116
        $this->addScopes($scopes);
117
    }
118
119
    /**
120
     * @param $scopes
121
     */
122
    public function addScopes($scopes)
123
    {
124
        foreach ($scopes as $scope) {
125
            $this->addScope($scope);
126
        }
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function insert()
133
    {
134
        $this->castExpireDateTime();
135
        return parent::insert();
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function update()
142
    {
143
        $this->castExpireDateTime();
144
        return parent::insert();
145
    }
146
147
    protected function castExpireDateTime()
148
    {
149
        $date = $this->getExpiryDateTime();
150
        $this->expires_at = ($date) ? $date->format('Y-m-d') : '';
0 ignored issues
show
introduced by
$date is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
151
    }
152
}
153