Skip to content

Commit 7ae49ce

Browse files
committed
AbstractService code style and refactoring
1 parent 8dbd83e commit 7ae49ce

25 files changed

+47
-47
lines changed

src/OAuth2/Service/AbstractService.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use OAuth\Common\Http\Url;
1010
use OAuth\Common\Service\AbstractService as BaseAbstractService;
1111
use OAuth\Common\Storage\TokenStorageInterface;
12+
use OAuth\Common\Token\AbstractToken;
1213
use OAuth\Common\Token\Exception\ExpiredTokenException;
1314
use OAuth\Common\Token\TokenInterface;
1415
use OAuth\OAuth2\Service\Exception\InvalidAuthorizationStateException;
@@ -102,12 +103,12 @@ abstract protected function parseAccessTokenResponse($responseBody);
102103
public function getAuthorizationUri(array $additionalParameters = [])
103104
{
104105
$parameters = array_merge(
105-
array(
106+
[
106107
'type' => 'web_server',
107108
'client_id' => $this->credentials->getConsumerId(),
108109
'redirect_uri' => $this->credentials->getCallbackUrl(),
109110
'response_type' => 'code',
110-
),
111+
],
111112
$additionalParameters
112113
);
113114

@@ -137,12 +138,10 @@ public function getAuthorizationUri(array $additionalParameters = [])
137138
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
138139
{
139140
$uri = $this->determineRequestUriFromPath($path);
141+
/** @var AbstractToken $token */
140142
$token = $this->storage->retrieveAccessToken($this->service());
141143

142-
if ($token->getEndOfLife() !== TokenInterface::EOL_NEVER_EXPIRES
143-
&& $token->getEndOfLife() !== TokenInterface::EOL_UNKNOWN
144-
&& time() > $token->getEndOfLife()
145-
)
144+
if ($token->isExpired())
146145
{
147146
throw new ExpiredTokenException(
148147
sprintf(
@@ -156,23 +155,23 @@ public function request($path, array $body = [], $method = 'GET', array $extraHe
156155
// add the token where it may be needed
157156
if (static::AUTHORIZATION_METHOD_HEADER_OAUTH === $this->getAuthorizationMethod())
158157
{
159-
$extraHeaders = array_merge(array('Authorization' => 'OAuth ' . $token->getAccessToken()), $extraHeaders);
158+
$extraHeaders = array_merge(['Authorization' => 'OAuth ' . $token->getAccessToken()], $extraHeaders);
160159
}
161160
elseif (static::AUTHORIZATION_METHOD_QUERY_STRING === $this->getAuthorizationMethod())
162161
{
163-
$uri->getQuery()->modify(array('access_token' => $token->getAccessToken()));
162+
$uri->getQuery()->modify(['access_token' => $token->getAccessToken()]);
164163
}
165164
elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V2 === $this->getAuthorizationMethod())
166165
{
167-
$uri->getQuery()->modify(array('oauth2_access_token' => $token->getAccessToken()));
166+
$uri->getQuery()->modify(['oauth2_access_token' => $token->getAccessToken()]);
168167
}
169168
elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V3 === $this->getAuthorizationMethod())
170169
{
171-
$uri->getQuery()->modify(array('apikey' => $token->getAccessToken()));
170+
$uri->getQuery()->modify(['apikey' => $token->getAccessToken()]);
172171
}
173172
elseif (static::AUTHORIZATION_METHOD_HEADER_BEARER === $this->getAuthorizationMethod())
174173
{
175-
$extraHeaders = array_merge(array('Authorization' => 'Bearer ' . $token->getAccessToken()), $extraHeaders);
174+
$extraHeaders = array_merge(['Authorization' => 'Bearer ' . $token->getAccessToken()], $extraHeaders);
176175
}
177176

178177
$extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
@@ -200,13 +199,13 @@ public function requestAccessToken($code, $state = NULL)
200199
$this->validateAuthorizationState($state);
201200
}
202201

203-
$bodyParams = array(
202+
$bodyParams = [
204203
'code' => $code,
205204
'client_id' => $this->credentials->getConsumerId(),
206205
'client_secret' => $this->credentials->getConsumerSecret(),
207206
'redirect_uri' => $this->credentials->getCallbackUrl(),
208207
'grant_type' => 'authorization_code',
209-
);
208+
];
210209

211210
$responseBody = $this->httpRequest(
212211
$this->getAccessTokenEndpoint(),
@@ -236,13 +235,13 @@ public function refreshAccessToken(TokenInterface $token)
236235
throw new MissingRefreshTokenException();
237236
}
238237

239-
$parameters = array(
238+
$parameters = [
240239
'grant_type' => 'refresh_token',
241240
'type' => 'web_server',
242241
'client_id' => $this->credentials->getConsumerId(),
243242
'client_secret' => $this->credentials->getConsumerSecret(),
244243
'refresh_token' => $refreshToken,
245-
);
244+
];
246245

247246
$responseBody = $this->httpRequest(
248247
$this->getAccessTokenEndpoint(),

tests/Unit/OAuth2/Service/AbstractServiceTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,13 @@ public function testGetAccessTokenUriWithInjectedVersion()
250250
* @covers OAuth\OAuth2\Service\AbstractService::__construct
251251
* @covers OAuth\OAuth2\Service\AbstractService::request
252252
* @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
253+
* @group active
253254
*/
254255
public function testRequestThrowsExceptionWhenTokenIsExpired()
255256
{
256257
$tokenExpiration = new \DateTime('26-03-1984 00:00:00');
257258

258-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
259+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife']);
259260
$token->expects($this->any())->method('getEndOfLife')->will($this->returnValue($tokenExpiration->format('U')));
260261

261262
$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
@@ -283,7 +284,7 @@ public function testRequestThrowsExceptionWhenTokenIsExpired()
283284
*/
284285
public function testRequestOauthAuthorizationMethod()
285286
{
286-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
287+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
287288
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
288289
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
289290

@@ -316,7 +317,7 @@ public function testRequestOauthAuthorizationMethod()
316317
*/
317318
public function testRequestQueryStringMethod()
318319
{
319-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
320+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
320321
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
321322
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
322323

@@ -348,7 +349,7 @@ public function testRequestQueryStringMethod()
348349
*/
349350
public function testRequestQueryStringTwoMethod()
350351
{
351-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
352+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
352353
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
353354
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
354355

@@ -381,7 +382,7 @@ public function testRequestQueryStringTwoMethod()
381382
*/
382383
public function testRequestBearerMethod()
383384
{
384-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
385+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
385386
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
386387
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
387388

tests/Unit/OAuth2/Service/AmazonTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetAccessTokenEndpoint()
8787
*/
8888
public function testGetAuthorizationMethod()
8989
{
90-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
90+
$token = $token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);;
9191
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9292
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9393

tests/Unit/OAuth2/Service/BitlyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetAccessTokenEndpoint()
8787
*/
8888
public function testGetAuthorizationMethod()
8989
{
90-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
90+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
9191
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9292
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9393

tests/Unit/OAuth2/Service/BoxTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetAccessTokenEndpoint()
8787
*/
8888
public function testGetAuthorizationMethod()
8989
{
90-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
90+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
9191
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9292
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9393

tests/Unit/OAuth2/Service/BufferTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetAccessTokenEndpoint()
8787
*/
8888
public function testGetAuthorizationMethod()
8989
{
90-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
90+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
9191
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9292
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9393

tests/Unit/OAuth2/Service/DailymotionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetAccessTokenEndpoint()
8787
*/
8888
public function testGetAuthorizationMethod()
8989
{
90-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
90+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
9191
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9292
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9393

tests/Unit/OAuth2/Service/DeviantArtTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testConstructCorrectInstanceWithCustomUri()
5151

5252
public function testBaseApiUriIsCorrect()
5353
{
54-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
54+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
5555
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
5656
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
5757

@@ -103,7 +103,7 @@ public function testGetAccessTokenEndpoint()
103103
*/
104104
public function testGetAuthorizationMethod()
105105
{
106-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
106+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
107107
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
108108
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
109109

tests/Unit/OAuth2/Service/DropboxTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function testGetAccessTokenEndpoint()
131131
*/
132132
public function testGetAuthorizationMethod()
133133
{
134-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
134+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
135135
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
136136
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
137137

tests/Unit/OAuth2/Service/EveOnlineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testGetAccessTokenEndpoint()
8989
*/
9090
public function testGetAuthorizationMethod()
9191
{
92-
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
92+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', ['getEndOfLife', 'getAccessToken']);
9393
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
9494
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
9595

0 commit comments

Comments
 (0)