|
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(); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getClient() |
|
62
|
|
|
{ |
|
63
|
2 |
|
$client = $this->getClientTrait(); |
|
64
|
2 |
|
if ($client instanceof ClientEntityInterface) { |
|
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(); |
|
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') : ''; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.