Completed
Push — master ( 4b04e0...ecec2c )
by Gabriel
10:34 queued 05:21
created

Token::setUserIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ByTIC\Hello\Models\AccessTokens;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use League\OAuth2\Server\Entities\Traits\EntityTrait;
7
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
8
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
9
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
10
11
/**
12
 * Class Token
13
 * @package ByTIC\Auth\Models\AccessTokens
14
 *
15
 * @property int $user_id
16
 * @property int $client_id
17
 * @property string $revoked
18
 * @property string $expires_at
19
 */
20
class Token extends \Nip\Records\Record implements AccessTokenEntityInterface
21
{
22
    use EntityTrait {
23
        setIdentifier as setIdentifierTrait;
24
    }
25
    use TokenEntityTrait {
26
        getClient as getClientTrait;
27
        setUserIdentifier as setUserIdentifierTrait;
28
    }
29
    use AccessTokenTrait;
30
31
    /**
32
     * @inheritDoc
33
     */
34 4
    public function writeData($data = false)
35
    {
36 4
        parent::writeData($data);
37 4
        if (isset($data['expires_at'])) {
38
            $date = new \DateTime($data['expires_at']);
39
            $this->setExpiryDateTime($date);
40
        }
41 4
        if (isset($data['identifier'])) {
42
            $this->setIdentifier($data['identifier']);
43
        }
44 4
        if (isset($data['user_id'])) {
45 1
            $this->setUserIdentifier($data['user_id']);
46
        }
47 4
    }
48
49
    /**
50
     * @param ClientEntityInterface $clientEntity
51
     */
52 1
    public function populateFromClient(ClientEntityInterface $clientEntity)
53
    {
54 1
        $this->setClient($clientEntity);
55 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...
56 1
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 2
    public function getClient()
62
    {
63 2
        $client = $this->getClientTrait();
64 2
        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...
65 1
            return $client;
66
        }
67
        /** @var ClientEntityInterface $relationClient */
68 1
        $relationClient = $this->getRelation('Client')->getResults();
69 1
        $this->setClient($relationClient);
70 1
        return $relationClient;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function setIdentifier($value)
77
    {
78 1
        $this->_data['identifier'] = $value;
79 1
        $this->setIdentifierTrait($value);
80 1
    }
81
82
    /**
83
     * @param int|string|null $identifier
84
     */
85 2
    public function setUserIdentifier($identifier)
86
    {
87 2
        $this->setUserIdentifierTrait($identifier);
88 2
        $this->user_id = $this->getUserIdentifier();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getUserIdentifier() can also be of type string. However, the property $user_id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
89 2
    }
90
91
    /**
92
     * @param $scopes
93
     */
94 1
    public function addScopes($scopes)
95
    {
96 1
        foreach ($scopes as $scope) {
97
            $this->addScope($scope);
98
        }
99 1
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function insert()
105
    {
106
        $this->castExpireDateTime();
107
        return parent::insert();
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function update()
114
    {
115
        $this->castExpireDateTime();
116
        return parent::insert();
117
    }
118
119
    protected function castExpireDateTime()
120
    {
121
        $date = $this->getExpiryDateTime();
122
        $this->expires_at = ($date) ? $date->format('Y-m-d') : '';
0 ignored issues
show
introduced by
$date is of type DateTime, thus it always evaluated to true.
Loading history...
123
    }
124
}
125