From 7f09c32e4322ad5ee91d1f003678fa7588f4bd72 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Wed, 15 Feb 2017 21:25:16 +0000 Subject: [PATCH 1/3] Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer --- src/Console/BazaarApiClearCacheCommand.php | 24 +++--- src/Console/BazaarApiRefreshTokenCommand.php | 37 ++++----- .../BazaarApiLaravel/BazaarApiFacade.php | 2 +- .../BazaarApiLaravel/BazaarApiFactory.php | 71 +++++++--------- .../BazaarApiLaravelServiceProvider.php | 80 +++++++++---------- src/TokenManager/CacheTokenManager.php | 30 ++++--- src/config/config.php | 2 +- 7 files changed, 118 insertions(+), 128 deletions(-) diff --git a/src/Console/BazaarApiClearCacheCommand.php b/src/Console/BazaarApiClearCacheCommand.php index 2ecdf85..c3854bf 100644 --- a/src/Console/BazaarApiClearCacheCommand.php +++ b/src/Console/BazaarApiClearCacheCommand.php @@ -4,7 +4,8 @@ use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; -class BazaarApiClearCacheCommand extends Command { +class BazaarApiClearCacheCommand extends Command +{ /** * The console command name. @@ -31,7 +32,8 @@ class BazaarApiClearCacheCommand extends Command { * @param ConfigRepository $config * @return \Nikapps\BazaarApiLaravel\Console\BazaarApiClearCacheCommand */ - public function __construct(ConfigRepository $config) { + public function __construct(ConfigRepository $config) + { $this->config = $config; @@ -44,24 +46,21 @@ public function __construct(ConfigRepository $config) { * * @return mixed */ - public function fire() { + public function fire() + { $cacheDriver = $this->option('driver'); $cacheName = $this->option('name'); - if(\Cache::driver($cacheDriver)->has($cacheName)){ - + if (\Cache::driver($cacheDriver)->has($cacheName)) { $this->comment('Value: ' . \Cache::driver($cacheDriver)->get($cacheName)); \Cache::driver($cacheDriver)->forget($cacheName); $this->info("$cacheName has been successfully removed from cache $cacheDriver"); - - }else{ - + } else { $this->error("$cacheName is already cleared from cache $cacheDriver"); } $this->info('Done!'); - } /** @@ -69,7 +68,8 @@ public function fire() { * * @return array */ - protected function getOptions() { + protected function getOptions() + { return [ [ "driver", @@ -89,6 +89,4 @@ protected function getOptions() { ]; } - - -} \ No newline at end of file +} diff --git a/src/Console/BazaarApiRefreshTokenCommand.php b/src/Console/BazaarApiRefreshTokenCommand.php index ef10d8e..b3663d3 100644 --- a/src/Console/BazaarApiRefreshTokenCommand.php +++ b/src/Console/BazaarApiRefreshTokenCommand.php @@ -9,8 +9,8 @@ use Illuminate\Console\Command; use Illuminate\Config\Repository as ConfigRepository; - -class BazaarApiRefreshTokenCommand extends Command { +class BazaarApiRefreshTokenCommand extends Command +{ /** * The console command name. @@ -39,7 +39,8 @@ class BazaarApiRefreshTokenCommand extends Command { * @param ConfigRepository $config * @return \Nikapps\BazaarApiLaravel\Console\BazaarApiRefreshTokenCommand */ - public function __construct(ConfigRepository $config) { + public function __construct(ConfigRepository $config) + { $this->config = $config; @@ -53,7 +54,8 @@ public function __construct(ConfigRepository $config) { * * @return mixed */ - public function fire() { + public function fire() + { $code = $this->option('code'); @@ -66,7 +68,8 @@ public function fire() { $bazaarApi->setApiConfig($apiConfig); $bazaarApi->setAccountConfig($accountConfig); - $this->line(sprintf($this->generateCodeUri, + $this->line(sprintf( + $this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId() )); @@ -84,26 +87,22 @@ public function fire() { $this->comment('Save refresh token in your config file'); $this->table($headers, $rows); - } catch (NetworkErrorException $e) { - $this->error('Error: ' . $e->getClientException()->getRequest()->getUrl()); $this->error('Response Status: ' . $e->getClientException()->getResponse()->getStatusCode()); - if($e->getClientException()->getResponse()->getStatusCode() == 401) { + if ($e->getClientException()->getResponse()->getStatusCode() == 401) { $this->comment('Your credentials is invalid or you should generate new code from this url: '); - $this->line(sprintf($this->generateCodeUri, + $this->line(sprintf( + $this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId() )); } - - } catch (\Exception $e){ + } catch (\Exception $e) { $this->error("Unknown Error: " . $e->getMessage()); } - - } /** @@ -111,7 +110,8 @@ public function fire() { * * @return array */ - protected function getOptions() { + protected function getOptions() + { return [ ["code", "c", InputOption::VALUE_REQUIRED, "returned code from cafebazaar", null], [ @@ -130,7 +130,8 @@ protected function getOptions() { * * @return ApiConfig */ - protected function getApiConfig(){ + protected function getApiConfig() + { $apiConfig = new ApiConfig(); $apiConfig->setAuthorizationGrantType($this->config->get('bazaar-api-laravel::api.authorization.grant_type')); @@ -148,7 +149,8 @@ protected function getApiConfig(){ * @param $redirectUrl * @return \Nikapps\BazaarApiPhp\Configs\AccountConfig */ - protected function getAccountConfig($code, $redirectUrl){ + protected function getAccountConfig($code, $redirectUrl) + { $accountConfig = new AccountConfig(); $accountConfig->setClientId($this->config->get('bazaar-api-laravel::credentials.client_id')); @@ -158,5 +160,4 @@ protected function getAccountConfig($code, $redirectUrl){ return $accountConfig; } - -} \ No newline at end of file +} diff --git a/src/Nikapps/BazaarApiLaravel/BazaarApiFacade.php b/src/Nikapps/BazaarApiLaravel/BazaarApiFacade.php index 2d316b7..7261ec7 100644 --- a/src/Nikapps/BazaarApiLaravel/BazaarApiFacade.php +++ b/src/Nikapps/BazaarApiLaravel/BazaarApiFacade.php @@ -13,4 +13,4 @@ protected static function getFacadeAccessor() { return 'BazaarApi'; } -} \ No newline at end of file +} diff --git a/src/Nikapps/BazaarApiLaravel/BazaarApiFactory.php b/src/Nikapps/BazaarApiLaravel/BazaarApiFactory.php index 23b9ef8..2355143 100644 --- a/src/Nikapps/BazaarApiLaravel/BazaarApiFactory.php +++ b/src/Nikapps/BazaarApiLaravel/BazaarApiFactory.php @@ -10,7 +10,8 @@ use Nikapps\BazaarApiPhp\Models\Requests\RefreshTokenRequest; use Nikapps\BazaarApiPhp\Models\Requests\SubscriptionStatusRequest; -class BazaarApiFactory { +class BazaarApiFactory +{ /** * @var ConfigRepository @@ -38,7 +39,8 @@ class BazaarApiFactory { * * @param ConfigRepository $config */ - public function __construct(ConfigRepository $config) { + public function __construct(ConfigRepository $config) + { $this->config = $config; @@ -54,7 +56,6 @@ public function __construct(ConfigRepository $config) { $tokenManager->setCacheName($this->config->get('bazaar-api-laravel::cache.cache_name')); $this->bazaarApi->setTokenManager($tokenManager); - } /** @@ -65,17 +66,19 @@ public function __construct(ConfigRepository $config) { * @throws \Nikapps\BazaarApiPhp\Exceptions\NotFoundException * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidJsonException */ - public function refreshToken($shouldStore = true) { + public function refreshToken($shouldStore = true) + { $refreshTokenRequest = new RefreshTokenRequest(); $refreshToken = $this->bazaarApi->refreshToken($refreshTokenRequest); if ($shouldStore) { - $this->bazaarApi->getTokenManager()->storeToken($refreshToken->getAccessToken(), - $refreshToken->getExpireIn()); + $this->bazaarApi->getTokenManager()->storeToken( + $refreshToken->getAccessToken(), + $refreshToken->getExpireIn() + ); } return $refreshToken; - } /** @@ -92,15 +95,14 @@ public function refreshToken($shouldStore = true) { * @throws \Nikapps\BazaarApiPhp\Exceptions\NotFoundException * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidJsonException */ - public function purchase($packageNameOrRequestOrArray, $productId = null, $purchaseToken = null) { + public function purchase($packageNameOrRequestOrArray, $productId = null, $purchaseToken = null) + { $purchaseRequest = $this->makePurchaseRequest($packageNameOrRequestOrArray, $productId, $purchaseToken); try { return $this->bazaarApi->getPurchase($purchaseRequest); - } catch (ExpiredAccessTokenException $e) { - if (!$this->config->get('bazaar-api-laravel::api.auto_refresh_token')) { //auto refresh token is disabled throw new ExpiredAccessTokenException; @@ -112,7 +114,6 @@ public function purchase($packageNameOrRequestOrArray, $productId = null, $purch return $this->purchase($purchaseRequest); } } - } /** @@ -129,15 +130,14 @@ public function purchase($packageNameOrRequestOrArray, $productId = null, $purch * @throws \Nikapps\BazaarApiPhp\Exceptions\NotFoundException * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidJsonException */ - public function subscription($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) { + public function subscription($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) + { $subscriptionRequest = $this->makeSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId, $purchaseToken); try { return $this->bazaarApi->getSubscription($subscriptionRequest); - } catch (ExpiredAccessTokenException $e) { - if (!$this->config->get('bazaar-api-laravel::api.auto_refresh_token')) { //auto refresh token is disabled throw new ExpiredAccessTokenException; @@ -149,7 +149,6 @@ public function subscription($packageNameOrRequestOrArray, $subscriptionId = nul return $this->subscription($subscriptionRequest); } } - } /** @@ -166,15 +165,14 @@ public function subscription($packageNameOrRequestOrArray, $subscriptionId = nul * @throws \Nikapps\BazaarApiPhp\Exceptions\NotFoundException * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidJsonException */ - public function cancelSubscription($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) { + public function cancelSubscription($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) + { $cancelSubscriptionRequest = $this->makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId, $purchaseToken); try { return $this->bazaarApi->cancelSubscription($cancelSubscriptionRequest); - } catch (ExpiredAccessTokenException $e) { - if (!$this->config->get('bazaar-api-laravel::api.auto_refresh_token')) { //auto refresh token is disabled throw new ExpiredAccessTokenException; @@ -186,7 +184,6 @@ public function cancelSubscription($packageNameOrRequestOrArray, $subscriptionId return $this->cancelSubscription($cancelSubscriptionRequest); } } - } @@ -198,7 +195,8 @@ public function cancelSubscription($packageNameOrRequestOrArray, $subscriptionId * @param string|null $purchaseToken * @return PurchaseStatusRequest */ - protected function makePurchaseRequest($packageNameOrRequestOrArray, $productId = null, $purchaseToken = null) { + protected function makePurchaseRequest($packageNameOrRequestOrArray, $productId = null, $purchaseToken = null) + { if ($packageNameOrRequestOrArray instanceof PurchaseStatusRequest) { return $packageNameOrRequestOrArray; @@ -207,21 +205,17 @@ protected function makePurchaseRequest($packageNameOrRequestOrArray, $productId $purchaseRequest = new PurchaseStatusRequest(); if (is_array($packageNameOrRequestOrArray)) { - $array = $packageNameOrRequestOrArray; $purchaseRequest->setPackage($array['package']); $purchaseRequest->setProductId($array['product_id']); $purchaseRequest->setPurchaseToken($array['purchase_token']); - } else { - $purchaseRequest->setPackage($packageNameOrRequestOrArray); $purchaseRequest->setProductId($productId); $purchaseRequest->setPurchaseToken($purchaseToken); } return $purchaseRequest; - } /** @@ -232,7 +226,8 @@ protected function makePurchaseRequest($packageNameOrRequestOrArray, $productId * @param string|null $purchaseToken * @return SubscriptionStatusRequest */ - protected function makeSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) { + protected function makeSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) + { if ($packageNameOrRequestOrArray instanceof SubscriptionStatusRequest) { return $packageNameOrRequestOrArray; @@ -241,21 +236,17 @@ protected function makeSubscriptionRequest($packageNameOrRequestOrArray, $subscr $subscriptionRequest = new SubscriptionStatusRequest(); if (is_array($packageNameOrRequestOrArray)) { - $array = $packageNameOrRequestOrArray; $subscriptionRequest->setPackage($array['package']); $subscriptionRequest->setSubscriptionId($array['subscription_id']); $subscriptionRequest->setPurchaseToken($array['purchase_token']); - } else { - $subscriptionRequest->setPackage($packageNameOrRequestOrArray); $subscriptionRequest->setSubscriptionId($subscriptionId); $subscriptionRequest->setPurchaseToken($purchaseToken); } return $subscriptionRequest; - } /** @@ -266,7 +257,8 @@ protected function makeSubscriptionRequest($packageNameOrRequestOrArray, $subscr * @param string|null $purchaseToken * @return CancelSubscriptionRequest */ - protected function makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) { + protected function makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null) + { if ($packageNameOrRequestOrArray instanceof CancelSubscriptionRequest) { return $packageNameOrRequestOrArray; @@ -275,21 +267,17 @@ protected function makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $ $cancelSubscriptionRequest = new CancelSubscriptionRequest(); if (is_array($packageNameOrRequestOrArray)) { - $array = $packageNameOrRequestOrArray; $cancelSubscriptionRequest->setPackage($array['package']); $cancelSubscriptionRequest->setSubscriptionId($array['subscription_id']); $cancelSubscriptionRequest->setPurchaseToken($array['purchase_token']); - } else { - $cancelSubscriptionRequest->setPackage($packageNameOrRequestOrArray); $cancelSubscriptionRequest->setSubscriptionId($subscriptionId); $cancelSubscriptionRequest->setPurchaseToken($purchaseToken); } return $cancelSubscriptionRequest; - } @@ -298,10 +286,10 @@ protected function makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $ * * @return ApiConfig */ - public function getApiConfig() { + public function getApiConfig() + { if (!$this->apiConfig) { - $apiConfig = new ApiConfig(); $apiConfig->setAuthorizationGrantType($this->config->get('bazaar-api-laravel::api.authorization.grant_type')); $apiConfig->setAuthorizationPath($this->config->get('bazaar-api-laravel::api.authorization.path')); @@ -324,10 +312,10 @@ public function getApiConfig() { * * @return \Nikapps\BazaarApiPhp\Configs\AccountConfig */ - public function getAccountConfig() { + public function getAccountConfig() + { if (!$this->accountConfig) { - $accountConfig = new AccountConfig(); $accountConfig->setClientId($this->config->get('bazaar-api-laravel::credentials.client_id')); $accountConfig->setClientSecret($this->config->get('bazaar-api-laravel::credentials.client_secret')); @@ -343,9 +331,8 @@ public function getAccountConfig() { /** * @return \Nikapps\BazaarApiPhp\BazaarApi */ - public function getBazaarApi() { + public function getBazaarApi() + { return $this->bazaarApi; } - - -} \ No newline at end of file +} diff --git a/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php b/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php index 5be9091..c7b0772 100644 --- a/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php +++ b/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php @@ -5,71 +5,69 @@ use Nikapps\BazaarApiLaravel\Console\BazaarApiClearCacheCommand; use Nikapps\BazaarApiLaravel\Console\BazaarApiRefreshTokenCommand; -class BazaarApiLaravelServiceProvider extends ServiceProvider { +class BazaarApiLaravelServiceProvider extends ServiceProvider +{ - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - $this->package('nikapps/bazaar-api-laravel'); + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $this->package('nikapps/bazaar-api-laravel'); AliasLoader::getInstance()->alias( 'BazaarApi', 'Nikapps\BazaarApiLaravel\BazaarApiFacade' ); - } + } - /** - * Register the service provider. - * - * @return void - */ - public function register() - { + /** + * Register the service provider. + * + * @return void + */ + public function register() + { $this->commands(['refresh-token']); $this->commands(['clear-cache']); $this->registerCommands(); - $this->app->bind('BazaarApi', function($app){ + $this->app->bind('BazaarApi', function ($app) { $config = $app['config']; return new BazaarApiFactory($config); }); - } + } - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array(); - } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array(); + } - public function registerCommands(){ + public function registerCommands() + { - $this->app['refresh-token'] = $this->app->share(function($app) - { + $this->app['refresh-token'] = $this->app->share(function ($app) { $config = $app['config']; return new BazaarApiRefreshTokenCommand($config); }); - $this->app['clear-cache'] = $this->app->share(function($app) - { + $this->app['clear-cache'] = $this->app->share(function ($app) { $config = $app['config']; return new BazaarApiClearCacheCommand($config); }); - } - } diff --git a/src/TokenManager/CacheTokenManager.php b/src/TokenManager/CacheTokenManager.php index ab9e90c..f9f38d5 100644 --- a/src/TokenManager/CacheTokenManager.php +++ b/src/TokenManager/CacheTokenManager.php @@ -3,7 +3,8 @@ use Illuminate\Support\Facades\Cache; use Nikapps\BazaarApiPhp\TokenManagers\TokenManagerInterface; -class CacheTokenManager implements TokenManagerInterface { +class CacheTokenManager implements TokenManagerInterface +{ /** * cache driver @@ -22,11 +23,12 @@ class CacheTokenManager implements TokenManagerInterface { * @param int $ttl number of seconds remaining until the token expires * @return mixed */ - public function storeToken($accessToken, $ttl) { + public function storeToken($accessToken, $ttl) + { $ttlInMin = intval($ttl / 60); - if(\Cache::driver($this->getCacheDriver())->has($this->cacheName)){ + if (\Cache::driver($this->getCacheDriver())->has($this->cacheName)) { \Cache::driver($this->getCacheDriver())->forget($this->cacheName); } @@ -38,7 +40,8 @@ public function storeToken($accessToken, $ttl) { * * @return string */ - public function loadToken() { + public function loadToken() + { return \Cache::driver($this->getCacheDriver())->get($this->cacheName, ''); } @@ -48,7 +51,8 @@ public function loadToken() { * * @return bool */ - public function isTokenExpired() { + public function isTokenExpired() + { return !\Cache::driver($this->getCacheDriver())->has($this->cacheName); } @@ -56,30 +60,32 @@ public function isTokenExpired() { /** * @return mixed */ - public function getCacheDriver() { + public function getCacheDriver() + { return $this->cacheDriver; } /** * @param mixed $cacheDriver */ - public function setCacheDriver($cacheDriver) { + public function setCacheDriver($cacheDriver) + { $this->cacheDriver = $cacheDriver; } /** * @return mixed */ - public function getCacheName() { + public function getCacheName() + { return $this->cacheName; } /** * @param mixed $cacheName */ - public function setCacheName($cacheName) { + public function setCacheName($cacheName) + { $this->cacheName = $cacheName; } - - -} \ No newline at end of file +} diff --git a/src/config/config.php b/src/config/config.php index 47599a8..0407049 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -115,4 +115,4 @@ 'cache_name' => 'bazaar-api-laravel::access_token' ] -]; \ No newline at end of file +]; From f5bcc2f16cabea0dfe7ad8f50416c62f2748a748 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Wed, 15 Feb 2017 21:25:16 +0000 Subject: [PATCH 2/3] Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. --- .../BazaarApiLaravel/BazaarApiLaravelServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php b/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php index c7b0772..9e18d39 100644 --- a/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php +++ b/src/Nikapps/BazaarApiLaravel/BazaarApiLaravelServiceProvider.php @@ -54,7 +54,7 @@ public function register() */ public function provides() { - return array(); + return []; } public function registerCommands() From da350d653a60ffb4f960abc0d5c76d2b4a4e7c51 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Wed, 15 Feb 2017 21:25:17 +0000 Subject: [PATCH 3/3] Shift Laravel dependencies --- composer.json | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index b1fc6cb..1580933 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,15 @@ { "name": "nikapps/bazaar-api-laravel", "description": "An API wrapper for cafebazaar based on popular Laravel Framework", - "keywords": ["CafeBazaar", "API", "Laravel", "Wrapper", "Cafebazaar Api", "In App Purchase", "iap"], + "keywords": [ + "CafeBazaar", + "API", + "Laravel", + "Wrapper", + "Cafebazaar Api", + "In App Purchase", + "iap" + ], "license": "MIT", "type": "project", "authors": [ @@ -20,16 +28,13 @@ "nikapps/bazaar-api-php": "1.*" }, "autoload": { - "classmap": [ "src/Console", "src/TokenManager" ], - "psr-0": { "Nikapps\\BazaarApiLaravel\\": "src/" } - }, "minimum-stability": "stable" -} +} \ No newline at end of file