Skip to content

Commit

Permalink
refactor: 移除不必要的依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jun 25, 2024
1 parent f7e6510 commit 2fe592a
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 78 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"php": ">=8.0.2",
"ext-json": "*",
"ext-openssl": "*",
"guzzlehttp/guzzle": "^7.0",
"symfony/psr-http-message-bridge": "^2.1|^6.0|^7.1"
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"mockery/mockery": "^1.3",
Expand Down
3 changes: 0 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ class Config implements ArrayAccess, JsonSerializable
{
protected array $config;

/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Overtrue\Socialite\Exceptions;

class BadRequestException extends Exception
{
}
class BadRequestException extends Exception {}
4 changes: 1 addition & 3 deletions src/Exceptions/FeiShu/InvalidTicketException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Overtrue\Socialite\Exceptions;

class InvalidTicketException extends Exceptions\Exception
{
}
class InvalidTicketException extends Exceptions\Exception {}
26 changes: 13 additions & 13 deletions src/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ abstract class Base implements Contracts\ProviderInterface
{
public const NAME = null;

protected ?string $state = null;
protected ?string $state = null;

protected Config $config;
protected Config $config;

protected ?string $redirectUrl;
protected ?string $redirectUrl;

protected array $parameters = [];
protected array $parameters = [];

protected array $scopes = [];
protected array $scopes = [];

protected string $scopeSeparator = ',';
protected string $scopeSeparator = ',';

protected GuzzleClient $httpClient;

protected array $guzzleOptions = [];
protected array $guzzleOptions = [];

protected int $encodingType = PHP_QUERY_RFC1738;
protected int $encodingType = PHP_QUERY_RFC1738;

protected string $expiresInKey = Contracts\RFC6749_ABNF_EXPIRES_IN;
protected string $expiresInKey = Contracts\RFC6749_ABNF_EXPIRES_IN;

protected string $accessTokenKey = Contracts\RFC6749_ABNF_ACCESS_TOKEN;
protected string $accessTokenKey = Contracts\RFC6749_ABNF_ACCESS_TOKEN;

protected string $refreshTokenKey = Contracts\RFC6749_ABNF_REFRESH_TOKEN;
protected string $refreshTokenKey = Contracts\RFC6749_ABNF_REFRESH_TOKEN;

public function __construct(array $config)
{
Expand All @@ -53,15 +53,15 @@ public function __construct(array $config)
// normalize Contracts\RFC6749_ABNF_CLIENT_ID
if (! $this->config->has(Contracts\RFC6749_ABNF_CLIENT_ID)) {
$id = $this->config->get(Contracts\ABNF_APP_ID);
if (null != $id) {
if ($id != null) {
$this->config->set(Contracts\RFC6749_ABNF_CLIENT_ID, $id);
}
}

// normalize Contracts\RFC6749_ABNF_CLIENT_SECRET
if (! $this->config->has(Contracts\RFC6749_ABNF_CLIENT_SECRET)) {
$secret = $this->config->get(Contracts\ABNF_APP_SECRET);
if (null != $secret) {
if ($secret != null) {
$this->config->set(Contracts\RFC6749_ABNF_CLIENT_SECRET, $secret);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Coding.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Coding extends Base
{
public const NAME = 'coding';
public const NAME = 'coding';

// example: https://{your-team}.coding.net
protected string $teamUrl;
Expand Down
1 change: 0 additions & 1 deletion src/Providers/Douban.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ protected function getTokenUrl(): string
}

/**
* @param string $token
* @param ?array $query
*/
protected function getUserByToken(string $token, ?array $query = []): array
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Facebook extends Base

protected string $version = 'v3.3';

protected array $fields = ['first_name', 'last_name', 'email', 'gender', 'verified','picture'];
protected array $fields = ['first_name', 'last_name', 'email', 'gender', 'verified', 'picture'];

protected array $scopes = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/FeiShu.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FeiShu extends Base

protected string $expiresInKey = 'refresh_expires_in';

protected bool $isInternalApp = false;
protected bool $isInternalApp = false;

public function __construct(array $config)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

class GitHub extends Base
{
public const NAME = 'github';
public const NAME = 'github';

protected array $scopes = ['read:user'];
protected array $scopes = ['read:user'];

protected string $scopeSeparator = ' ';

Expand Down
8 changes: 4 additions & 4 deletions src/Providers/Linkedin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function getBasicProfile(string $token): array

$response = $this->getHttpClient()->get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Authorization' => 'Bearer '.$token,
'X-RestLi-Protocol-Version' => '2.0.0',
],
]);
Expand All @@ -65,7 +65,7 @@ protected function getEmailAddress(string $token): array

$response = $this->getHttpClient()->get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Authorization' => 'Bearer '.$token,
'X-RestLi-Protocol-Version' => '2.0.0',
],
]);
Expand All @@ -75,11 +75,11 @@ protected function getEmailAddress(string $token): array

protected function mapUserToObject(array $user): Contracts\UserInterface
{
$preferredLocale = ($user['firstName']['preferredLocale']['language'] ?? null) . '_' .
$preferredLocale = ($user['firstName']['preferredLocale']['language'] ?? null).'_'.
($user['firstName']['preferredLocale']['country'] ?? null);
$firstName = $user['firstName']['localized'][$preferredLocale] ?? null;
$lastName = $user['lastName']['localized'][$preferredLocale] ?? null;
$name = $firstName . ' ' . $lastName;
$name = $firstName.' '.$lastName;

$images = $user['profilePicture']['displayImage~']['elements'] ?? [];
$avatars = \array_filter($images, static fn ($image) => ($image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] ?? 0) === 100);
Expand Down
85 changes: 46 additions & 39 deletions src/Providers/PayPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,57 @@

/**
* Class PayPal
*
* @author zhiqiang
* @package Overtrue\Socialite\Providers
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/
*/
class PayPal extends Base
{
public const NAME = 'paypal';

protected string $scopeSeparator = ' ';

/**
* @var string|null
* code or id_token
* code or id_token
*/
protected ?string $responseType = Contracts\RFC6749_ABNF_CODE;
protected string $flowEntry = 'static';

protected string $authUrl = 'https://www.paypal.com/signin/authorize';
protected string $tokenURL = "https://api.sandbox.paypal.com/v1/oauth2/token";
protected string $userinfoURL = "https://api.paypal.com/v1/identity/openidconnect/userinfo";
protected string $flowEntry = 'static';

protected string $authUrl = 'https://www.paypal.com/signin/authorize';

protected string $tokenURL = 'https://api.sandbox.paypal.com/v1/oauth2/token';

protected string $userinfoURL = 'https://api.paypal.com/v1/identity/openidconnect/userinfo';

protected array $scopes = [
'openid', 'profile', 'email', 'address'
'openid', 'profile', 'email', 'address',
];

protected bool $sandbox = true;

public function __construct(array $config)
{
parent::__construct($config);
$this->sandbox = (bool)$this->config->get('sandbox', false);
$this->sandbox = (bool) $this->config->get('sandbox', false);
if ($this->sandbox) {
$this->authUrl = 'https://www.sandbox.paypal.com/signin/authorize';
$this->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token';
$this->authUrl = 'https://www.sandbox.paypal.com/signin/authorize';
$this->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token';
$this->userinfoURL = 'https://api-m.sandbox.paypal.com/v1/identity/openidconnect/userinfo';
}
}

/**
* @param string|null $responseType
* @return $this
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/generate-button/
*/
public function withResponseType(?string $responseType)
{
$this->responseType = $responseType;

return $this;
}

Expand All @@ -65,32 +71,31 @@ protected function getCodeFields(): array
{
$fields = \array_merge(
[
'flowEntry' => $this->flowEntry,
Contracts\RFC6749_ABNF_CLIENT_ID => $this->getClientId(),
'flowEntry' => $this->flowEntry,
Contracts\RFC6749_ABNF_CLIENT_ID => $this->getClientId(),
Contracts\RFC6749_ABNF_RESPONSE_TYPE => $this->responseType,
Contracts\RFC6749_ABNF_SCOPE => $this->formatScopes($this->scopes, $this->scopeSeparator),
Contracts\RFC6749_ABNF_REDIRECT_URI => $this->redirectUrl,
Contracts\RFC6749_ABNF_SCOPE => $this->formatScopes($this->scopes, $this->scopeSeparator),
Contracts\RFC6749_ABNF_REDIRECT_URI => $this->redirectUrl,
],
$this->parameters
);

if ($this->state) {
$fields[Contracts\RFC6749_ABNF_STATE] = $this->state;
}

return $fields;
}


protected function getTokenUrl(): string
{
return $this->tokenURL;
}

/**
* @param string $code
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-getaccesstoken
*/
public function tokenFromCode(string $code): array
Expand All @@ -100,46 +105,46 @@ public function tokenFromCode(string $code): array
[
'form_params' => [
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_AUTHORATION_CODE,
Contracts\RFC6749_ABNF_CODE => $code,
Contracts\RFC6749_ABNF_CODE => $code,
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
],
]
);
return $this->normalizeAccessTokenResponse((string)$response->getBody());

return $this->normalizeAccessTokenResponse((string) $response->getBody());
}

/**
* @param string $refreshToken
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-exchangerefreshtokenforaccesstoken
*/
public function refreshToken(string $refreshToken): mixed
{
$response = $this->getHttpClient()->post(
$response = $this->getHttpClient()->post(
$this->getTokenUrl(),
[
'form_params' => [
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_REFRESH_TOKEN,
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_REFRESH_TOKEN,
Contracts\RFC6749_ABNF_REFRESH_TOKEN => $refreshToken,
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
],
]
);
return $this->normalizeAccessTokenResponse((string)$response->getBody());

return $this->normalizeAccessTokenResponse((string) $response->getBody());
}

/**
* @param string $token
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @see https://developer.paypal.com/docs/api/identity/v1/#userinfo_get
*/
protected function getUserByToken(string $token): array
Expand All @@ -148,22 +153,24 @@ protected function getUserByToken(string $token): array
$this->userinfoURL,
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
]
);

return $this->fromJsonBody($response);
}

#[Pure]
protected function mapUserToObject(array $user): Contracts\UserInterface
{
$user[Contracts\ABNF_ID] = $user['user_id'] ?? null;
$user[Contracts\ABNF_ID] = $user['user_id'] ?? null;
$user[Contracts\ABNF_NICKNAME] = $user['given_name'] ?? $user['family_name'] ?? $user['middle_name'] ?? null;
$user[Contracts\ABNF_NAME] = $user['name'] ?? '';
$user[Contracts\ABNF_EMAIL] = $user[Contracts\ABNF_EMAIL] ?? null;
$user[Contracts\ABNF_AVATAR] = $user['picture'] ?? null;
$user[Contracts\ABNF_NAME] = $user['name'] ?? '';
$user[Contracts\ABNF_EMAIL] = $user[Contracts\ABNF_EMAIL] ?? null;
$user[Contracts\ABNF_AVATAR] = $user['picture'] ?? null;

return new User($user);
}
}
4 changes: 2 additions & 2 deletions src/Providers/WeChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ protected function prepareForComponent(array $component): void
}
}

if (2 !== \count($config)) {
if (\count($config) !== 2) {
throw new Exceptions\InvalidArgumentException('Please check your config arguments were available.');
}

if (1 === \count($this->scopes) && \in_array('snsapi_login', $this->scopes)) {
if (\count($this->scopes) === 1 && \in_array('snsapi_login', $this->scopes)) {
$this->scopes = ['snsapi_base'];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function toJSON(): string
{
$result = \json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE);

false === $result && throw new Exceptions\Exception('Cannot Processing this instance as JSON stringify.');
$result === false && throw new Exceptions\Exception('Cannot Processing this instance as JSON stringify.');

return $result;
}
Expand Down
Loading

0 comments on commit 2fe592a

Please sign in to comment.