Skip to content

Commit f4def48

Browse files
committed
:octocat: reduce storage exception classes
1 parent 9aeed31 commit f4def48

11 files changed

+31
-60
lines changed

src/Storage/FileStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getAccessToken(string $provider):AccessToken{
8080
$tokenData = $this->loadFile($this::KEY_TOKEN, $provider);
8181

8282
if($tokenData === null){
83-
throw new TokenNotFoundException;
83+
throw new ItemNotFoundException($this::KEY_TOKEN);
8484
}
8585

8686
return $this->fromStorage($tokenData);
@@ -137,7 +137,7 @@ public function getCSRFState(string $provider):string{
137137
$state = $this->loadFile($this::KEY_STATE, $provider);
138138

139139
if($state === null){
140-
throw new StateNotFoundException;
140+
throw new ItemNotFoundException($this::KEY_STATE);
141141
}
142142

143143
if($this->options->useStorageEncryption === true){
@@ -198,7 +198,7 @@ public function getCodeVerifier(string $provider):string{
198198
$verifier = $this->loadFile($this::KEY_VERIFIER, $provider);
199199

200200
if($verifier === null){
201-
throw new VerifierNotFoundException;
201+
throw new ItemNotFoundException($this::KEY_VERIFIER);
202202
}
203203

204204
if($this->options->useStorageEncryption === true){
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Class StateNotFoundException
3+
* Class ItemNotFoundException
44
*
55
* @created 04.04.2024
66
* @author smiley <[email protected]>
@@ -11,8 +11,8 @@
1111
namespace chillerlan\OAuth\Storage;
1212

1313
/**
14-
*
14+
* Thrown when an item cannot be found in the storage
1515
*/
16-
final class StateNotFoundException extends OAuthStorageException{
16+
final class ItemNotFoundException extends OAuthStorageException{
1717

1818
}

src/Storage/MemoryStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getAccessToken(string $provider):AccessToken{
5252
return $this->storage[$this::KEY_TOKEN][$this->getProviderName($provider)];
5353
}
5454

55-
throw new TokenNotFoundException;
55+
throw new ItemNotFoundException($this::KEY_TOKEN);
5656
}
5757

5858
/**
@@ -103,7 +103,7 @@ public function getCSRFState(string $provider):string{
103103
return $this->storage[$this::KEY_STATE][$this->getProviderName($provider)];
104104
}
105105

106-
throw new StateNotFoundException;
106+
throw new ItemNotFoundException($this::KEY_STATE);
107107
}
108108

109109
/**
@@ -154,7 +154,7 @@ public function getCodeVerifier(string $provider):string{
154154
return $this->storage[$this::KEY_VERIFIER][$this->getProviderName($provider)];
155155
}
156156

157-
throw new VerifierNotFoundException;
157+
throw new ItemNotFoundException($this::KEY_VERIFIER);
158158
}
159159

160160
/**

src/Storage/OAuthStorageException.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use chillerlan\OAuth\OAuthException;
1515

16+
/**
17+
* Thrown on general storage errors
18+
*/
1619
class OAuthStorageException extends OAuthException{
1720

1821
}

src/Storage/OAuthStorageInterface.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function storeAccessToken(AccessToken $token, string $provider):static;
6363
/**
6464
* Retrieves an AccessToken for the given $provider
6565
*
66-
* This method *must* throw a TokenNotFoundException if a token is not found
66+
* This method *must* throw a ItemNotFoundException if a token is not found
6767
*
68-
* @throws \chillerlan\OAuth\Storage\TokenNotFoundException
68+
* @throws \chillerlan\OAuth\Storage\ItemNotFoundException
6969
*/
7070
public function getAccessToken(string $provider):AccessToken;
7171

@@ -103,9 +103,9 @@ public function storeCSRFState(string $state, string $provider):static;
103103
/**
104104
* Retrieves a CSRF <state> value for the given $provider
105105
*
106-
* This method *must* throw a StateNotFoundException if a state is not found
106+
* This method *must* throw a ItemNotFoundException if a state is not found
107107
*
108-
* @throws \chillerlan\OAuth\Storage\StateNotFoundException
108+
* @throws \chillerlan\OAuth\Storage\ItemNotFoundException
109109
*/
110110
public function getCSRFState(string $provider):string;
111111

@@ -140,6 +140,10 @@ public function storeCodeVerifier(string $verifier, string $provider):static;
140140

141141
/**
142142
* Retrieves a PKCE verifier
143+
*
144+
* This method *must* throw a ItemNotFoundException if a verifier is not found
145+
*
146+
* @throws \chillerlan\OAuth\Storage\ItemNotFoundException
143147
*/
144148
public function getCodeVerifier(string $provider):string;
145149

src/Storage/SessionStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getAccessToken(string $provider):AccessToken{
9292
return $this->fromStorage($_SESSION[$this->storageVar][$this::KEY_TOKEN][$this->getProviderName($provider)]);
9393
}
9494

95-
throw new TokenNotFoundException;
95+
throw new ItemNotFoundException($this::KEY_TOKEN);
9696
}
9797

9898
/**
@@ -145,7 +145,7 @@ public function storeCSRFState(string $state, string $provider):static{
145145
public function getCSRFState(string $provider):string{
146146

147147
if(!$this->hasCSRFState($provider)){
148-
throw new StateNotFoundException;
148+
throw new ItemNotFoundException($this::KEY_STATE);
149149
}
150150

151151
$state = $_SESSION[$this->storageVar][$this::KEY_STATE][$this->getProviderName($provider)];
@@ -207,7 +207,7 @@ public function storeCodeVerifier(string $verifier, string $provider):static{
207207
public function getCodeVerifier(string $provider):string{
208208

209209
if(!$this->hasCodeVerifier($provider)){
210-
throw new VerifierNotFoundException;
210+
throw new ItemNotFoundException($this::KEY_VERIFIER);
211211
}
212212

213213
$verifier = $_SESSION[$this->storageVar][$this::KEY_VERIFIER][$this->getProviderName($provider)];

src/Storage/TokenNotFoundException.php

-18
This file was deleted.

src/Storage/VerifierNotFoundException.php

-18
This file was deleted.

tests/Providers/Unit/OAuth2ProviderUnitTestAbstract.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
2121
use chillerlan\OAuth\OAuthException;
2222
use chillerlan\OAuth\Providers\ProviderException;
23-
use chillerlan\OAuth\Storage\StateNotFoundException;
23+
use chillerlan\OAuth\Storage\ItemNotFoundException;
2424
use PHPUnit\Framework\Attributes\DataProvider;
2525
use function base64_encode, implode, json_decode, json_encode;
2626

@@ -432,7 +432,7 @@ public function testCheckCSRFStateNotFoundException():void{
432432
$this->markTestSkipped('CSRFToken N/A');
433433
}
434434

435-
$this->expectException(StateNotFoundException::class);
435+
$this->expectException(ItemNotFoundException::class);
436436

437437
$this->provider->checkState('invalid_test_state');
438438
}

tests/Providers/Unit/OAuthProviderUnitTestAbstract.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use chillerlan\OAuth\Core\UnauthorizedAccessException;
2222
use chillerlan\OAuth\Core\UserInfo;
2323
use chillerlan\OAuth\Providers\ProviderException;
24-
use chillerlan\OAuth\Storage\TokenNotFoundException;
24+
use chillerlan\OAuth\Storage\ItemNotFoundException;
2525
use chillerlan\OAuthTest\Providers\ProviderUnitTestAbstract;
2626
use PHPUnit\Framework\Attributes\DataProvider;
2727
use function sprintf;
@@ -226,7 +226,7 @@ public function testTokenInvalidateNoTokenException():void{
226226
$this::markTestSkipped('TokenInvalidate N/A');
227227
}
228228

229-
$this->expectException(TokenNotFoundException::class);
229+
$this->expectException(ItemNotFoundException::class);
230230

231231
$this->provider->invalidateAccessToken();
232232
}

tests/Storage/StorageTestAbstract.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\OAuth\OAuthOptions;
1515
use chillerlan\OAuth\Core\AccessToken;
1616
use chillerlan\OAuth\Storage\{
17-
OAuthStorageException, OAuthStorageInterface, StateNotFoundException, TokenNotFoundException, VerifierNotFoundException
17+
OAuthStorageException, OAuthStorageInterface, ItemNotFoundException
1818
};
1919
use PHPUnit\Framework\TestCase;
2020
use ReflectionMethod;
@@ -68,7 +68,7 @@ public function testClearAllAccessTokens():void{
6868
}
6969

7070
public function testGetAccessTokenNotFoundException():void{
71-
$this->expectException(TokenNotFoundException::class);
71+
$this->expectException(ItemNotFoundException::class);
7272

7373
$this->storage->getAccessToken('LOLNOPE');
7474
}
@@ -103,7 +103,7 @@ public function testClearAllCSRFStates():void{
103103
}
104104

105105
public function testGetCSRFStateNotFoundException():void{
106-
$this->expectException(StateNotFoundException::class);
106+
$this->expectException(ItemNotFoundException::class);
107107

108108
$this->storage->getCSRFState('LOLNOPE');
109109
}
@@ -138,7 +138,7 @@ public function testClearAllPKCEVerifiers():void{
138138
}
139139

140140
public function testGetPKCEVerifierNotFoundException():void{
141-
$this->expectException(VerifierNotFoundException::class);
141+
$this->expectException(ItemNotFoundException::class);
142142

143143
$this->storage->getCodeVerifier('LOLNOPE');
144144
}

0 commit comments

Comments
 (0)