diff --git a/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php b/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php index 3703b72ce..b7edf928c 100644 --- a/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php +++ b/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php @@ -32,7 +32,7 @@ use EasyWeChat\OpenPlatform\AccessToken; use EasyWeChat\OpenPlatform\Api\BaseApi; use EasyWeChat\OpenPlatform\Api\PreAuthorization; -use EasyWeChat\OpenPlatform\Authorization; +use EasyWeChat\OpenPlatform\Authorizer; use EasyWeChat\OpenPlatform\AuthorizerAccessToken; use EasyWeChat\OpenPlatform\EventHandlers; use EasyWeChat\OpenPlatform\Guard; @@ -112,8 +112,8 @@ public function register(Container $pimple) ); }; - $pimple['open_platform.authorization'] = function ($pimple) { - return new Authorization( + $pimple['open_platform.authorizer'] = function ($pimple) { + return new Authorizer( $pimple['open_platform.api'], $pimple['config']['open_platform']['app_id'], $pimple['cache'] @@ -123,7 +123,7 @@ public function register(Container $pimple) $pimple['open_platform.authorizer_access_token'] = function ($pimple) { return new AuthorizerAccessToken( $pimple['config']['open_platform']['app_id'], - $pimple['open_platform.authorization'] + $pimple['open_platform.authorizer'] ); }; @@ -151,7 +151,7 @@ public function register(Container $pimple) $scopes = $pimple['config']->get('open_platform.oauth.scopes', []); $socialite = (new Socialite([ 'wechat_open' => [ - 'client_id' => $pimple['open_platform.authorization']->getAuthorizerAppId(), + 'client_id' => $pimple['open_platform.authorizer']->getAppId(), 'client_secret' => [ $pimple['open_platform.access_token']->getAppId(), $pimple['open_platform.access_token']->getToken(), diff --git a/src/OpenPlatform/Authorization.php b/src/OpenPlatform/Authorizer.php similarity index 57% rename from src/OpenPlatform/Authorization.php rename to src/OpenPlatform/Authorizer.php index 8ef2d23c5..a573df9ec 100644 --- a/src/OpenPlatform/Authorization.php +++ b/src/OpenPlatform/Authorizer.php @@ -10,7 +10,7 @@ */ /** - * Authorization.php. + * Authorizer.php. * * Part of Overtrue\WeChat. * @@ -31,7 +31,7 @@ use EasyWeChat\Core\Exception; use EasyWeChat\OpenPlatform\Api\BaseApi; -class Authorization +class Authorizer { const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token'; const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token'; @@ -51,30 +51,30 @@ class Authorization protected $api; /** - * Open Platform App Id, aka, Component App Id. + * Authorizer AppId. * * @var string */ protected $appId; /** - * Authorizer App Id. + * OpenPlatform AppId. * * @var string */ - protected $authorizerAppId; + protected $openPlatformAppId; /** - * Authorization Constructor. + * Authorizer Constructor. * * @param \EasyWeChat\OpenPlatform\Api\BaseApi $api - * @param string $appId + * @param string $openPlatformAppId OpenPlatform AppId * @param \Doctrine\Common\Cache\Cache $cache */ - public function __construct(BaseApi $api, $appId, Cache $cache) + public function __construct(BaseApi $api, $openPlatformAppId, Cache $cache) { $this->api = $api; - $this->appId = $appId; + $this->openPlatformAppId = $openPlatformAppId; $this->cache = $cache; } @@ -91,13 +91,13 @@ public function getApi() /** * Sets the authorizer app id. * - * @param string $authorizerAppId + * @param string $appId * * @return $this */ - public function setAuthorizerAppId($authorizerAppId) + public function setAppId($appId) { - $this->authorizerAppId = $authorizerAppId; + $this->appId = $appId; return $this; } @@ -109,27 +109,30 @@ public function setAuthorizerAppId($authorizerAppId) * * @throws \EasyWeChat\Core\Exception */ - public function getAuthorizerAppId() + public function getAppId() { - if (!$this->authorizerAppId) { + if (!$this->appId) { throw new Exception( - 'Authorizer App Id is not present, you may not make the authorization yet.' + 'Authorizer App Id is not present, you may not make the authorizer yet.' ); } - return $this->authorizerAppId; + return $this->appId; } /** * Saves the authorizer access token in cache. * * @param string $token + * @param int $expires * - * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise + * @return $this */ - public function setAuthorizerAccessToken($token, $expires = 7200) + public function setAccessToken($token, $expires = 7200) { - return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires); + $this->cache->save($this->getAccessTokenCacheKey(), $token, $expires); + + return $this; } /** @@ -137,9 +140,9 @@ public function setAuthorizerAccessToken($token, $expires = 7200) * * @return string */ - public function getAuthorizerAccessToken() + public function getAccessToken() { - return $this->cache->fetch($this->getAuthorizerAccessTokenKey()); + return $this->cache->fetch($this->getAccessTokenCacheKey()); } /** @@ -147,11 +150,13 @@ public function getAuthorizerAccessToken() * * @param string $refreshToken * - * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise + * @return $this */ - public function setAuthorizerRefreshToken($refreshToken) + public function setRefreshToken($refreshToken) { - return $this->cache->save($this->getAuthorizerRefreshTokenKey(), $refreshToken); + $this->cache->save($this->getRefreshTokenCacheKey(), $refreshToken); + + return $this; } /** @@ -161,47 +166,25 @@ public function setAuthorizerRefreshToken($refreshToken) * * @throws \EasyWeChat\Core\Exception when refresh token is not present */ - public function getAuthorizerRefreshToken() + public function getRefreshToken() { - if ($token = $this->cache->fetch($this->getAuthorizerRefreshTokenKey())) { + if ($token = $this->cache->fetch($this->getRefreshTokenCacheKey())) { return $token; } throw new Exception( - 'Authorizer Refresh Token is not present, you may not make the authorization yet.' + 'Authorizer Refresh Token is not present, you may not make the authorizer yet.' ); } - /** - * Removes the authorizer access token from cache. - * - * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. - * Deleting a non-existing entry is considered successful - */ - public function removeAuthorizerAccessToken() - { - return $this->cache->delete($this->getAuthorizerAccessTokenKey()); - } - - /** - * Removes the authorizer refresh token from cache. - * - * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. - * Deleting a non-existing entry is considered successful - */ - public function removeAuthorizerRefreshToken() - { - return $this->cache->delete($this->getAuthorizerRefreshTokenKey()); - } - /** * Gets the authorizer access token cache key. * * @return string */ - public function getAuthorizerAccessTokenKey() + public function getAccessTokenCacheKey() { - return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAuthorizerAppId(); + return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAppId(); } /** @@ -209,8 +192,8 @@ public function getAuthorizerAccessTokenKey() * * @return string */ - public function getAuthorizerRefreshTokenKey() + public function getRefreshTokenCacheKey() { - return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAuthorizerAppId(); + return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAppId(); } } diff --git a/src/OpenPlatform/AuthorizerAccessToken.php b/src/OpenPlatform/AuthorizerAccessToken.php index f97d8f3c9..28d6c8a50 100644 --- a/src/OpenPlatform/AuthorizerAccessToken.php +++ b/src/OpenPlatform/AuthorizerAccessToken.php @@ -36,29 +36,28 @@ * * AuthorizerAccessToken is responsible for the access token of the authorizer, * the complexity is that this access token also requires the refresh token - * of the authorizer which is acquired by the open platform authorization - * process. + * of the authorizer which is acquired by the open platform authorizer process. * * This completely overrides the original AccessToken. */ class AuthorizerAccessToken extends BaseAccessToken { /** - * @var \EasyWeChat\OpenPlatform\Authorization + * @var \EasyWeChat\OpenPlatform\Authorizer */ - protected $authorization; + protected $authorizer; /** * AuthorizerAccessToken constructor. * - * @param string $appId - * @param \EasyWeChat\OpenPlatform\Authorization $authorization + * @param string $appId + * @param \EasyWeChat\OpenPlatform\Authorizer $authorizer */ - public function __construct($appId, Authorization $authorization) + public function __construct($appId, Authorizer $authorizer) { parent::__construct($appId, null); - $this->authorization = $authorization; + $this->authorizer = $authorizer; } /** @@ -70,7 +69,7 @@ public function __construct($appId, Authorization $authorization) */ public function getToken($forceRefresh = false) { - $cached = $this->authorization->getAuthorizerAccessToken(); + $cached = $this->authorizer->getAccessToken(); if ($forceRefresh || empty($cached)) { return $this->refreshToken(); @@ -86,13 +85,13 @@ public function getToken($forceRefresh = false) */ protected function refreshToken() { - $token = $this->authorization->getApi() + $token = $this->authorizer->getApi() ->getAuthorizerToken( - $this->authorization->getAuthorizerAppId(), - $this->authorization->getAuthorizerRefreshToken() + $this->authorizer->getAppId(), + $this->authorizer->getRefreshToken() ); - $this->authorization->setAuthorizerAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500); + $this->authorizer->setAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500); return $token['authorizer_access_token']; } @@ -104,6 +103,6 @@ protected function refreshToken() */ public function getAppId() { - return $this->authorization->getAuthorizerAppId(); + return $this->authorizer->getAppId(); } } diff --git a/src/OpenPlatform/Guard.php b/src/OpenPlatform/Guard.php index 374e8852b..ee931c370 100644 --- a/src/OpenPlatform/Guard.php +++ b/src/OpenPlatform/Guard.php @@ -132,8 +132,8 @@ protected function handleEventMessage(array $message) Log::notice("No existing handler for '{$infoType}'."); } - if ($customHandler = $this->getMessageHandler()) { - $customHandler($message); + if ($messageHandler = $this->getMessageHandler()) { + call_user_func_array($messageHandler, [$message]); } } } diff --git a/src/OpenPlatform/OpenPlatform.php b/src/OpenPlatform/OpenPlatform.php index 9d8a20aa5..0e82726b0 100644 --- a/src/OpenPlatform/OpenPlatform.php +++ b/src/OpenPlatform/OpenPlatform.php @@ -55,15 +55,16 @@ class OpenPlatform */ public function createAuthorizerApplication($appId, $refreshToken) { - $this->fetch('authorization') - ->setAuthorizerAppId($appId) - ->setAuthorizerRefreshToken($refreshToken); + $this->fetch('authorizer', function ($authorizer) use ($appId, $refreshToken) { + $authorizer->setAppId($appId); + $authorizer->setRefreshToken($refreshToken); + }); - $application = $this->fetch('app'); - $application['access_token'] = $this->fetch('authorizer_access_token'); - $application['oauth'] = $this->fetch('oauth'); - - return $application; + return $this->fetch('app', function ($app) { + $app['access_token'] = $this->fetch('authorizer_access_token'); + $app['oauth'] = $this->fetch('oauth'); + $app['server'] = $this->fetch('server'); + }); } /** diff --git a/src/Support/Traits/PrefixedContainer.php b/src/Support/Traits/PrefixedContainer.php index 83c6993c9..70ef547af 100644 --- a/src/Support/Traits/PrefixedContainer.php +++ b/src/Support/Traits/PrefixedContainer.php @@ -54,13 +54,20 @@ public function __construct(Container $container) /** * Fetches from pimple container. * - * @param string $key + * @param string $key + * @param callable|null $callable * * @return mixed */ - public function fetch($key) + public function fetch($key, callable $callable = null) { - return $this->$key; + $instance = $this->$key; + + if (!is_null($callable)) { + $callable($instance); + } + + return $instance; } /** diff --git a/tests/OpenPlatform/AuthorizerAccessTokenTest.php b/tests/OpenPlatform/AuthorizerAccessTokenTest.php index d99356984..9ee34b0c5 100644 --- a/tests/OpenPlatform/AuthorizerAccessTokenTest.php +++ b/tests/OpenPlatform/AuthorizerAccessTokenTest.php @@ -16,43 +16,33 @@ class AuthorizerAccessTokenTest extends TestCase { public function testGetToken() { - $appId = 'appid@123'; - $cachedToken = 'token@123'; - $auth = $this->make($appId, $cachedToken, null); + $auth = $this->make('appid@123', 'token@123', null); - $token = $auth->getToken(); - $this->assertEquals($cachedToken, $token); + $this->assertEquals('token@123', $auth->getToken()); } public function testGetTokenExpired() { - $appId = 'appid@123'; - $newToken = 'token@456'; - $auth = $this->make($appId, null, $newToken); + $auth = $this->make('appid@123', null, 'token@456'); - $token = $auth->getToken(); - $this->assertEquals($newToken, $token); + $this->assertEquals('token@456', $auth->getToken()); } public function testGetTokenForced() { - $appId = 'appid@123'; - $cachedToken = 'token@123'; - $newToken = 'token@456'; - $auth = $this->make($appId, $cachedToken, $newToken); + $auth = $this->make('appid@123', 'token@123', 'token@456'); - $token = $auth->getToken(true); - $this->assertEquals($newToken, $token); + $this->assertEquals('token@456', $auth->getToken(true)); } private function make($appId, $cachedToken, $newToken) { - /** @var Authorization|\Mockery\MockInterface $mock */ - $mock = \Mockery::mock('EasyWeChat\OpenPlatform\Authorization'); - $mock->shouldReceive('getAuthorizerAppId')->andReturn($appId); - $mock->shouldReceive('getAuthorizerRefreshToken')->andReturn($newToken); - $mock->shouldReceive('setAuthorizerAccessToken')->andReturn(true); - $mock->shouldReceive('getAuthorizerAccessToken') + /** @var \EasyWeChat\OpenPlatform\Authorizer|\Mockery\MockInterface $mock */ + $mock = \Mockery::mock('EasyWeChat\OpenPlatform\Authorizer'); + $mock->shouldReceive('getAppId')->andReturn($appId); + $mock->shouldReceive('getRefreshToken')->andReturn($newToken); + $mock->shouldReceive('setAccessToken')->andReturn(true); + $mock->shouldReceive('getAccessToken') ->andReturn($cachedToken); $mock->shouldReceive('getApi') ->andReturn(\Mockery::mock('EasyWeChat\OpenPlatform\Api\BaseApi', function ($mock) use ($newToken) { diff --git a/tests/OpenPlatform/AuthorizationTest.php b/tests/OpenPlatform/AuthorizerTest.php similarity index 78% rename from tests/OpenPlatform/AuthorizationTest.php rename to tests/OpenPlatform/AuthorizerTest.php index e809b529a..bcc910f2c 100644 --- a/tests/OpenPlatform/AuthorizationTest.php +++ b/tests/OpenPlatform/AuthorizerTest.php @@ -1,7 +1,7 @@ */ @@ -9,26 +9,26 @@ namespace EasyWeChat\Tests\OpenPlatform; use Doctrine\Common\Cache\ArrayCache; -use EasyWeChat\OpenPlatform\Authorization; +use EasyWeChat\OpenPlatform\Authorizer; use EasyWeChat\Support\Collection; use EasyWeChat\Tests\TestCase; -class AuthorizationTest extends TestCase +class AuthorizerTest extends TestCase { public function testGetApi() { - $authorization = $this->make('appid', 'authorizer-appid'); + $authorizer = $this->make('appid', 'authorizer-appid'); - $this->assertInstanceOf('EasyWeChat\OpenPlatform\Api\BaseApi', $authorization->getApi()); + $this->assertInstanceOf('EasyWeChat\OpenPlatform\Api\BaseApi', $authorizer->getApi()); } public function testGetAuthorizationInfo() { $appId = 'appid@123'; $authorizerAppId = 'appid@456'; - $authorization = $this->make($appId, $authorizerAppId); + $authorizer = $this->make($appId, $authorizerAppId); - $result = $authorization->getApi()->getAuthorizationInfo(); + $result = $authorizer->getApi()->getAuthorizationInfo(); $this->assertEquals($this->stubAuthorizationInfo($authorizerAppId), $result); } @@ -36,43 +36,45 @@ public function testGetAuthorizerInfo() { $appId = 'appid@123'; $authorizerAppId = 'appid@456'; - $authorization = $this->make($appId, $authorizerAppId); + $authorizer = $this->make($appId, $authorizerAppId); - $result = $authorization->getApi()->getAuthorizerInfo('appid@123'); + $result = $authorizer->getApi()->getAuthorizerInfo('appid@123'); $this->assertEquals($this->stubAuthorizerInfo($authorizerAppId), $result); } - public function testSetAndGetAuthorizerAccessToken() + public function testSetAndGetAccessToken() { - $authorization = $this->make('appid@123', 'authorizer-appid@456'); + $authorizer = $this->make('appid@123', 'authorizer-appid@456'); $stub = $this->stubAuthorizationInfo('authorizer-appid@456', 'authorizer-access@123'); - $this->assertTrue( - $authorization->setAuthorizerAccessToken($stub['authorization_info']['authorizer_access_token']) + $this->assertInstanceOf( + 'EasyWeChat\OpenPlatform\Authorizer', + $authorizer->setAccessToken($stub['authorization_info']['authorizer_access_token']) ); - $this->assertEquals('authorizer-access@123', $authorization->getAuthorizerAccessToken()); + $this->assertEquals('authorizer-access@123', $authorizer->getAccessToken()); } - public function testSetAndGetAuthorizerRefreshToken() + public function testSetAndGetRefreshToken() { - $authorization = $this->make('appid@123', 'appid@456'); + $authorizer = $this->make('appid@123', 'appid@456'); $stub = $this->stubAuthorizationInfo('appid@456', 'access@123', 'refresh@123'); - $this->assertTrue( - $authorization->setAuthorizerRefreshToken($stub['authorization_info']['authorizer_refresh_token']) + $this->assertInstanceOf( + 'EasyWeChat\OpenPlatform\Authorizer', + $authorizer->setRefreshToken($stub['authorization_info']['authorizer_refresh_token']) ); - $this->assertEquals('refresh@123', $authorization->getAuthorizerRefreshToken()); + $this->assertEquals('refresh@123', $authorizer->getRefreshToken()); } /** - * Authorization mock. + * Authorizer mock. * * @param string $appId * @param string $authorizerAppId * @param string $authorizerAccessToken * @param string $authorizerRefreshToken * - * @return Authorization + * @return Authorizer */ protected function make($appId, $authorizerAppId, $authorizerAccessToken = null, @@ -108,10 +110,10 @@ function ($appId, $authorizerRefreshToken) use ($stub) { ); $cache = new ArrayCache(); - $authorization = new Authorization($mockAuthorizer, $appId, $cache); - $authorization->setAuthorizerAppId($authorizerAppId); + $authorizer = new Authorizer($mockAuthorizer, $appId, $cache); + $authorizer->setAppId($authorizerAppId); - return $authorization; + return $authorizer; } protected function stubAuthorizationInfo($authorizerAppId,