Skip to content

Commit f01e147

Browse files
committed
🚿 OAuthProvider: remove magic properties
1 parent ea86ace commit f01e147

19 files changed

+121
-90
lines changed

examples/create-description.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
/** @var \OAuthExampleProviderFactory $factory */
3838
$provider = $factory->getProvider($p['fqcn'], OAuthExampleProviderFactory::STORAGE_MEMORY);
3939

40-
$oauth = match(true){
40+
$oauthVersion = match(true){
4141
$provider instanceof OAuth2Interface => '2',
4242
$provider instanceof OAuth1Interface => '1',
4343
default => '-',
4444
};
4545

46-
$table[] = '| ['.$p['name'].']('.$provider->apiDocs.')'.
47-
' | [link]('.$provider->applicationURL.')'.
48-
' | '.($provider->userRevokeURL !== null ? '[link]('.$provider->userRevokeURL.')' : '').
49-
' | '.$oauth.
46+
$table[] = '| ['.$provider->getName().']('.$provider->getApiDocURL().')'.
47+
' | [link]('.$provider->getApplicationURL().')'.
48+
' | '.($provider->getUserRevokeURL() !== null ? '[link]('.$provider->getUserRevokeURL().')' : '').
49+
' | '.$oauthVersion.
5050
' | '.(($provider instanceof UserInfo) ? '' : '').
5151
' | '.(($provider instanceof CSRFToken) ? '' : '').
5252
' | '.(($provider instanceof PKCE) ? '' : '').

examples/get-token/LastFM.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
$provider = $factory->getProvider(LastFM::class);
22-
$name = $provider->name;
22+
$name = $provider->getName();
2323

2424
// step 2: redirect to the provider's login screen
2525
if(isset($_GET['login']) && $_GET['login'] === $name){

examples/get-token/Steam.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @var \chillerlan\OAuth\Providers\Steam $provider
1919
*/
2020
$provider = $factory->getProvider(Steam::class);
21-
$name = $provider->name;
21+
$name = $provider->getName();
2222

2323
// step 2: redirect to the provider's login screen
2424
if(isset($_GET['login']) && $_GET['login'] === $name){

examples/get-token/_flow-oauth1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @var array|null $PARAMS
1818
*/
1919

20-
$name = $provider->name;
20+
$name = $provider->getName();
2121

2222
// step 2: redirect to the provider's login screen
2323
if(isset($_GET['login']) && $_GET['login'] === $name){

examples/get-token/_flow-oauth2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @var array|null $SCOPES
1919
*/
2020

21-
$name = $provider->name;
21+
$name = $provider->getName();
2222

2323
// step 2: redirect to the provider's login screen
2424
if(isset($_GET['login']) && $_GET['login'] === $name){

src/Core/OAuthInterface.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121

2222
/**
2323
* Specifies the basic methods for an OAuth provider.
24-
*
25-
* @property string $name (magic) The name of the provider/class
26-
* @property string $apiURL (magic) The API base URL
27-
* @property string|null $apiDocs (magic) An optional link to the provider's API docs
28-
* @property string|null $applicationURL (magic) An optional URL to the provider's credential registration/application page
29-
* @property string|null $userRevokeURL (magic) An optional link to the page where a user can revoke access tokens
3024
*/
3125
interface OAuthInterface extends ClientInterface{
3226

@@ -76,6 +70,26 @@ interface OAuthInterface extends ClientInterface{
7670
*/
7771
public const SCOPES_DELIMITER = ' ';
7872

73+
/**
74+
* Returns the name of the provider/class
75+
*/
76+
public function getName():string;
77+
78+
/**
79+
* Returns the link to the provider's API docs, or null if the value is not set
80+
*/
81+
public function getApiDocURL():string|null;
82+
83+
/**
84+
* Returns the link to the provider's credential registration/application page, or null if the value is not set
85+
*/
86+
public function getApplicationURL():string|null;
87+
88+
/**
89+
* Returns the link to the page where a user can revoke access tokens, or null if the value is not set
90+
*/
91+
public function getUserRevokeURL():string|null;
92+
7993
/**
8094
* Prepares the URL with optional $params which redirects to the provider's authorization prompt
8195
* and returns a PSR-7 UriInterface with all necessary parameters set.

src/Core/OAuthProvider.php

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
*/
3737
abstract class OAuthProvider implements OAuthInterface{
3838

39-
/**
40-
* The options instance
41-
*/
42-
protected OAuthOptions|SettingsContainerInterface $options;
43-
4439
/**
4540
* The PSR-18 HTTP client
4641
*/
@@ -62,50 +57,61 @@ abstract class OAuthProvider implements OAuthInterface{
6257
protected UriFactoryInterface $uriFactory;
6358

6459
/**
65-
* A storage instance
60+
* A PSR-3 logger
6661
*/
67-
protected OAuthStorageInterface $storage;
62+
protected LoggerInterface $logger;
6863

6964
/**
70-
* A PSR-3 logger
65+
* The options instance
7166
*/
72-
protected LoggerInterface $logger;
67+
protected OAuthOptions|SettingsContainerInterface $options;
68+
69+
/**
70+
* A storage instance
71+
*/
72+
protected OAuthStorageInterface $storage;
7373

7474
/**
75-
* the authorization URL
75+
* The authorization URL
7676
*/
7777
protected string $authorizationURL = '';
7878

7979
/**
80-
* the provider's access token exchange URL
80+
* The access token exchange URL
8181
*/
8282
protected string $accessTokenURL = '';
8383

8484
/**
85-
* an optional URL for application side token revocation
85+
* An optional URL for application side token revocation
8686
*
8787
* @see \chillerlan\OAuth\Core\TokenInvalidate
8888
*/
8989
protected string $revokeURL = '';
9090

9191
/**
92-
* magic properties
93-
*
94-
* @var string[]
92+
* The API base URL
93+
*/
94+
protected string $apiURL = '';
95+
96+
/**
97+
* The name of the provider/class
9598
*/
96-
protected const MAGIC_PROPERTIES = [
97-
'apiDocs', 'apiURL', 'applicationURL', 'name', 'userRevokeURL',
98-
];
99+
protected string $name = '';
99100

100-
/*
101-
* magic properties (doc in interface docblock)
101+
/**
102+
* An optional link to the provider's API docs
102103
*/
104+
protected string|null $apiDocs = null;
103105

104-
protected string $name = '';
105-
protected string $apiURL = '';
106-
protected string|null $apiDocs = null;
106+
/**
107+
* An optional URL to the provider's credential registration/application page
108+
*/
107109
protected string|null $applicationURL = null;
108-
protected string|null $userRevokeURL = null;
110+
111+
/**
112+
* An optional link to the page where a user can revoke access tokens
113+
*/
114+
protected string|null $userRevokeURL = null;
109115

110116
/**
111117
* OAuthProvider constructor.
@@ -141,15 +147,35 @@ protected function construct():void{
141147
}
142148

143149
/**
144-
* Magic getter for the properties specified in self::ALLOWED_PROPERTIES
150+
* @inheritDoc
151+
* @codeCoverageIgnore
145152
*/
146-
final public function __get(string $name):string|null{
153+
final public function getName():string{
154+
return $this->name;
155+
}
147156

148-
if(in_array($name, $this::MAGIC_PROPERTIES, true)){
149-
return $this->{$name};
150-
}
157+
/**
158+
* @inheritDoc
159+
* @codeCoverageIgnore
160+
*/
161+
final public function getApiDocURL():string|null{
162+
return $this->apiDocs;
163+
}
151164

152-
return null;
165+
/**
166+
* @inheritDoc
167+
* @codeCoverageIgnore
168+
*/
169+
final public function getApplicationURL():string|null{
170+
return $this->applicationURL;
171+
}
172+
173+
/**
174+
* @inheritDoc
175+
* @codeCoverageIgnore
176+
*/
177+
final public function getUserRevokeURL():string|null{
178+
return $this->userRevokeURL;
153179
}
154180

155181
/**

tests/Providers/Live/AmazonAPITest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ protected function assertMeResponse(AuthenticatedUser $user):void{
2929
}
3030

3131
public function testMeUnauthorizedAccessException():void{
32-
$token = $this->storage->getAccessToken($this->provider->name);
32+
$token = $this->storage->getAccessToken($this->provider->getName());
3333
// avoid refresh
3434
$token->expires = AccessToken::NEVER_EXPIRES;
3535
$token->refreshToken = null;
3636
// invalidate token
3737
$token->accessToken = 'Atza|nope'; // amazon tokens are prefixed
3838

3939
// using a temp storage here so that the local tokens won't be overwritten
40-
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->name);
40+
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->getName());
4141

4242
$this->provider->setStorage($tempStorage);
4343

tests/Providers/Live/MailChimpAPITest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
final class MailChimpAPITest extends OAuth2ProviderLiveTestAbstract{
2424

2525
public function testGetTokenMetadata():void{
26-
$token = $this->storage->getAccessToken($this->provider->name);
26+
$token = $this->storage->getAccessToken($this->provider->getName());
2727
$token = $this->provider->getTokenMetadata($token);
2828

2929
$this::assertSame($this->TEST_USER, $token->extraParams['accountname']);

tests/Providers/Live/OAuthProviderLiveTestAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testMeUnauthorizedAccessException():void{
6767
$this::markTestSkipped('AuthenticatedUser N/A');
6868
}
6969

70-
$token = $this->storage->getAccessToken($this->provider->name);
70+
$token = $this->storage->getAccessToken($this->provider->getName());
7171
// avoid refresh
7272
$token->expires = AccessToken::NEVER_EXPIRES;
7373
$token->refreshToken = null;
@@ -76,7 +76,7 @@ public function testMeUnauthorizedAccessException():void{
7676
$token->accessTokenSecret = 'what';
7777

7878
// using a temp storage here so that the local tokens won't be overwritten
79-
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->name);
79+
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->getName());
8080

8181
$this->provider->setStorage($tempStorage);
8282

0 commit comments

Comments
 (0)