1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Common\Storage; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; |
6
|
|
|
use OAuth\Common\Storage\Exception\TokenNotFoundException; |
7
|
|
|
use OAuth\Common\Token\TokenInterface; |
8
|
|
|
|
9
|
|
|
// Stores a token in-memory only (destroyed at end of script execution). |
10
|
|
|
class Memory implements TokenStorageInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var object|TokenInterface |
14
|
|
|
*/ |
15
|
|
|
protected $tokens; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $states; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->tokens = []; |
|
|
|
|
25
|
|
|
$this->states = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function retrieveAccessToken($service) |
32
|
|
|
{ |
33
|
|
|
if ($this->hasAccessToken($service)) { |
34
|
|
|
return $this->tokens[$service]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new TokenNotFoundException('Token not stored'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function storeAccessToken($service, TokenInterface $token) |
44
|
|
|
{ |
45
|
|
|
$this->tokens[$service] = $token; |
46
|
|
|
|
47
|
|
|
// allow chaining |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function hasAccessToken($service) |
55
|
|
|
{ |
56
|
|
|
return isset($this->tokens[$service]) && $this->tokens[$service] instanceof TokenInterface; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function clearToken($service) |
63
|
|
|
{ |
64
|
|
|
if (array_key_exists($service, $this->tokens)) { |
65
|
|
|
unset($this->tokens[$service]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// allow chaining |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function clearAllTokens() |
76
|
|
|
{ |
77
|
|
|
$this->tokens = []; |
|
|
|
|
78
|
|
|
|
79
|
|
|
// allow chaining |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function retrieveAuthorizationState($service) |
87
|
|
|
{ |
88
|
|
|
if ($this->hasAuthorizationState($service)) { |
89
|
|
|
return $this->states[$service]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new AuthorizationStateNotFoundException('State not stored'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function storeAuthorizationState($service, $state) |
99
|
|
|
{ |
100
|
|
|
$this->states[$service] = $state; |
101
|
|
|
|
102
|
|
|
// allow chaining |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function hasAuthorizationState($service) |
110
|
|
|
{ |
111
|
|
|
return isset($this->states[$service]) && null !== $this->states[$service]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function clearAuthorizationState($service) |
118
|
|
|
{ |
119
|
|
|
if (array_key_exists($service, $this->states)) { |
120
|
|
|
unset($this->states[$service]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// allow chaining |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function clearAllAuthorizationStates() |
131
|
|
|
{ |
132
|
|
|
$this->states = []; |
133
|
|
|
|
134
|
|
|
// allow chaining |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..