From 58b72a667da292a1d0a0b1e6e9aeda4053617030 Mon Sep 17 00:00:00 2001 From: a-komarev Date: Tue, 15 Jan 2019 18:36:41 +0300 Subject: [PATCH] Fixed SocialiteWasCalled event --- .../Helpers/ConfigRetrieverInterface.php | 4 +- src/Exception/InvalidArgumentException.php | 4 +- src/Exception/MissingConfigException.php | 4 +- src/Helpers/ConfigRetriever.php | 24 +++---- src/SocialiteWasCalled.php | 70 +++++++++++-------- 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/Contracts/Helpers/ConfigRetrieverInterface.php b/src/Contracts/Helpers/ConfigRetrieverInterface.php index dcfaf99..47c7cb5 100644 --- a/src/Contracts/Helpers/ConfigRetrieverInterface.php +++ b/src/Contracts/Helpers/ConfigRetrieverInterface.php @@ -8,9 +8,9 @@ interface ConfigRetrieverInterface * @param string $providerName * @param array $additionalConfigKeys * - * @throws \SocialiteProviders\Manager\Exception\MissingConfigException - * * @return \SocialiteProviders\Manager\Contracts\ConfigInterface + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ public function fromServices($providerName, array $additionalConfigKeys = []); } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 4a00951..ce04812 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -2,6 +2,8 @@ namespace SocialiteProviders\Manager\Exception; -class InvalidArgumentException extends \InvalidArgumentException +use InvalidArgumentException as BaseInvalidArgumentException; + +class InvalidArgumentException extends BaseInvalidArgumentException { } diff --git a/src/Exception/MissingConfigException.php b/src/Exception/MissingConfigException.php index 77d4eae..3d8d0b6 100644 --- a/src/Exception/MissingConfigException.php +++ b/src/Exception/MissingConfigException.php @@ -2,6 +2,8 @@ namespace SocialiteProviders\Manager\Exception; -class MissingConfigException extends \Exception +use Exception; + +class MissingConfigException extends Exception { } diff --git a/src/Helpers/ConfigRetriever.php b/src/Helpers/ConfigRetriever.php index eb1431e..1163e19 100644 --- a/src/Helpers/ConfigRetriever.php +++ b/src/Helpers/ConfigRetriever.php @@ -2,6 +2,7 @@ namespace SocialiteProviders\Manager\Helpers; +use Closure; use SocialiteProviders\Manager\Config; use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface; use SocialiteProviders\Manager\Exception\MissingConfigException; @@ -32,9 +33,9 @@ class ConfigRetriever implements ConfigRetrieverInterface * @param string $providerName * @param array $additionalConfigKeys * - * @throws \SocialiteProviders\Manager\Exception\MissingConfigException - * * @return \SocialiteProviders\Manager\Contracts\ConfigInterface + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ public function fromServices($providerName, array $additionalConfigKeys = []) { @@ -59,7 +60,7 @@ public function fromServices($providerName, array $additionalConfigKeys = []) * * @return array */ - protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) + protected function getConfigItems(array $configKeys, Closure $keyRetrievalClosure) { if (count($configKeys) < 1) { return []; @@ -74,7 +75,7 @@ protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosu * * @return array */ - protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) + protected function retrieveItemsFromConfig(array $keys, Closure $keyRetrievalClosure) { $out = []; @@ -88,9 +89,9 @@ protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalCl /** * @param string $key * - * @throws \SocialiteProviders\Manager\Exception\MissingConfigException - * * @return string + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function getFromServices($key) { @@ -112,22 +113,21 @@ protected function getFromServices($key) /** * @param string $providerName * - * @throws \SocialiteProviders\Manager\Exception\MissingConfigException - * * @return array + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function getConfigFromServicesArray($providerName) { - /** @var array $configArray */ - $configArray = config("services.$providerName"); + $configArray = config("services.{$providerName}"); if (empty($configArray)) { // If we are running in console we should spoof values to make Socialite happy... if (app()->runningInConsole()) { $configArray = [ - 'client_id' => "{$this->providerIdentifier}_KEY", + 'client_id' => "{$this->providerIdentifier}_KEY", 'client_secret' => "{$this->providerIdentifier}_SECRET", - 'redirect' => "{$this->providerIdentifier}_REDIRECT_URI", + 'redirect' => "{$this->providerIdentifier}_REDIRECT_URI", ]; } else { throw new MissingConfigException("There is no services entry for $providerName"); diff --git a/src/SocialiteWasCalled.php b/src/SocialiteWasCalled.php index 6cbd8a4..60d07ae 100644 --- a/src/SocialiteWasCalled.php +++ b/src/SocialiteWasCalled.php @@ -2,18 +2,21 @@ namespace SocialiteProviders\Manager; -use Illuminate\Container\Container as Application; +use Illuminate\Contracts\Container\Container as Application; +use Laravel\Socialite\Contracts\Factory as SocialiteFactory; +use Laravel\Socialite\One\AbstractProvider as SocialiteOAuth1AbstractProvider; use Laravel\Socialite\SocialiteManager; +use Laravel\Socialite\Two\AbstractProvider as SocialiteOAuth2AbstractProvider; +use League\OAuth1\Client\Server\Server as OAuth1Server; use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface; use SocialiteProviders\Manager\Exception\InvalidArgumentException; -use SocialiteProviders\Manager\Exception\MissingConfigException; class SocialiteWasCalled { const SERVICE_CONTAINER_PREFIX = 'SocialiteProviders.config.'; /** - * @var \Illuminate\Container\Container + * @var \Illuminate\Contracts\Container\Container */ protected $app; @@ -23,20 +26,22 @@ class SocialiteWasCalled private $configRetriever; /** - * @var bool + * @var array */ private $spoofedConfig = [ - 'client_id' => 'spoofed_client_id', + 'client_id' => 'spoofed_client_id', 'client_secret' => 'spoofed_client_secret', - 'redirect' => 'spoofed_redirect', + 'redirect' => 'spoofed_redirect', ]; /** - * @param \Illuminate\Container\Container $app + * @param \Illuminate\Contracts\Container\Container $app + * @param \SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface $configRetriever */ - public function __construct(Application $app) + public function __construct(Application $app, ConfigRetrieverInterface $configRetriever) { $this->app = $app; + $this->configRetriever = $configRetriever; } /** @@ -46,17 +51,19 @@ public function __construct(Application $app) * Laravel\Socialite\One\AbstractProvider * @param string $oauth1Server 'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server * - * @throws \InvalidArgumentException + * @return void + * + * @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException */ public function extendSocialite($providerName, $providerClass, $oauth1Server = null) { /** @var SocialiteManager $socialite */ - $socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class); + $socialite = $this->app->make(SocialiteFactory::class); $this->classExists($providerClass); if ($this->isOAuth1($oauth1Server)) { $this->classExists($oauth1Server); - $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class); + $this->classExtends($providerClass, SocialiteOAuth1AbstractProvider::class); } $socialite->extend( @@ -74,11 +81,13 @@ function () use ($socialite, $providerName, $providerClass, $oauth1Server) { /** * @param \Laravel\Socialite\SocialiteManager $socialite - * @param $providerName + * @param string $providerName * @param string $providerClass * @param null|string $oauth1Server * * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function buildProvider(SocialiteManager $socialite, $providerName, $providerClass, $oauth1Server) { @@ -92,16 +101,18 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr /** * Build an OAuth 1 provider instance. * + * @param \Laravel\Socialite\SocialiteManager $socialite * @param string $providerClass must extend Laravel\Socialite\One\AbstractProvider + * @param string $providerName * @param string $oauth1Server must extend League\OAuth1\Client\Server\Server - * @param array $config - * @param mixed $providerName * * @return \Laravel\Socialite\One\AbstractProvider + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function buildOAuth1Provider(SocialiteManager $socialite, $providerClass, $providerName, $oauth1Server) { - $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class); + $this->classExtends($oauth1Server, OAuth1Server::class); $config = $this->getConfig($providerClass, $providerName); @@ -121,14 +132,15 @@ protected function buildOAuth1Provider(SocialiteManager $socialite, $providerCla * * @param SocialiteManager $socialite * @param string $providerClass must extend Laravel\Socialite\Two\AbstractProvider - * @param array $config - * @param mixed $providerName + * @param string $providerName * * @return \Laravel\Socialite\Two\AbstractProvider + * + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, $providerName) { - $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class); + $this->classExtends($providerClass, SocialiteOAuth2AbstractProvider::class); $config = $this->getConfig($providerClass, $providerName); @@ -143,19 +155,15 @@ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerCla * @param string $providerClass * @param string $providerName * - * @throws \SocialiteProviders\Manager\Exception\MissingConfigException + * @return \SocialiteProviders\Manager\Contracts\ConfigInterface * - * @return array + * @throws \SocialiteProviders\Manager\Exception\MissingConfigException */ protected function getConfig($providerClass, $providerName) { - try { - return resolve(ConfigRetrieverInterface::class)->fromServices( - $providerName, $providerClass::additionalConfigKeys() - ); - } catch (MissingConfigException $e) { - throw new MissingConfigException($e->getMessage()); - } + return $this->configRetriever->fromServices( + $providerName, $providerClass::additionalConfigKeys() + ); return new Config( $this->spoofedConfig['client_id'], @@ -180,7 +188,9 @@ private function isOAuth1($oauth1Server) * @param string $class * @param string $baseClass * - * @throws \InvalidArgumentException + * @return void + * + * @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException */ private function classExtends($class, $baseClass) { @@ -192,7 +202,9 @@ private function classExtends($class, $baseClass) /** * @param string $providerClass * - * @throws \InvalidArgumentException + * @return void + * + * @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException */ private function classExists($providerClass) {