Skip to content

Commit

Permalink
optimize the signatures and drop the last one `getBody()->getContents…
Browse files Browse the repository at this point in the history
…()` usage (#245)
  • Loading branch information
TheNorthMemory committed Jul 15, 2022
1 parent 725ad08 commit 45f4931
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/Contracts/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@

interface FactoryInterface
{
public function create(string $driver): ProviderInterface;
public function config(\Overtrue\Socialite\Config $config): self;

public function create(string $name): ProviderInterface;

public function getResolvedProviders(): array;

public function buildProvider(string $provider, array $config): ProviderInterface;
}
10 changes: 10 additions & 0 deletions src/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ public function userFromCode(string $code): UserInterface;

public function userFromToken(string $token): UserInterface;

public function withRedirectUrl(string $redirectUrl): self;

public function withState(string $state): self;

/**
* @param string[] $scopes
*/
public function scopes(array $scopes): self;

public function with(array $parameters): self;

public function withScopeSeparator(string $scopeSeparator): self;

public function getClientId(): ?string;

public function getClientSecret(): ?string;
}
4 changes: 4 additions & 0 deletions src/Contracts/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public function setExpiresIn(int $expiresIn): self;

public function setTokenResponse(array $response): self;

public function getTokenResponse(): mixed;

public function setProvider(ProviderInterface $provider): self;

public function getRaw(): array;

public function setRaw(array $user): self;

public function setAccessToken(string $token): self;
Expand Down
8 changes: 4 additions & 4 deletions src/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function tokenFromCode(string $code): array
]
);

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

/**
Expand Down Expand Up @@ -216,7 +216,7 @@ protected function buildAuthUrlFromBase(string $url): string

protected function getCodeFields(): array
{
$fields = array_merge(
$fields = \array_merge(
[
Contracts\RFC6749_ABNF_CLIENT_ID => $this->getClientId(),
Contracts\RFC6749_ABNF_REDIRECT_URI => $this->redirectUrl,
Expand Down Expand Up @@ -244,15 +244,15 @@ protected function normalizeAccessTokenResponse(mixed $response): array
}

if (\is_string($response)) {
$response = json_decode($response, true) ?? [];
$response = Utils::jsonDecode($response, true);
}

if (!\is_array($response)) {
throw new Exceptions\AuthorizeFailedException('Invalid token response', [$response]);
}

if (empty($response[$this->accessTokenKey])) {
throw new Exceptions\AuthorizeFailedException('Authorize Failed: ' . json_encode($response, JSON_UNESCAPED_UNICODE), $response);
throw new Exceptions\AuthorizeFailedException('Authorize Failed: ' . Utils::jsonEncode($response, \JSON_UNESCAPED_UNICODE), $response);
}

return $response + [
Expand Down
4 changes: 0 additions & 4 deletions src/Providers/Figma.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ protected function getTokenUrl(): string
return 'https://www.figma.com/api/oauth/token';
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*/
public function tokenFromCode(string $code): array
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
Expand Down
3 changes: 0 additions & 3 deletions src/Providers/Linkedin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected function getUserByToken(string $token, ?array $query = []): array
return \array_merge($basicProfile, $emailAddress);
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getBasicProfile(string $token): array
{
$url = 'https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))';
Expand Down
2 changes: 1 addition & 1 deletion src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(array $config)
$this->config = new Config($config);
}

public function config(Config $config): static
public function config(Config $config): self
{
$this->config = $config;

Expand Down
8 changes: 4 additions & 4 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public function test_it_can_get_token()
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);

$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('getContents')->andReturn([
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]);
]));

$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
Expand Down Expand Up @@ -129,11 +129,11 @@ public function test_it_can_get_user_by_code()

$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('getContents')->andReturn([
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]);
]));

$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
Expand Down

0 comments on commit 45f4931

Please sign in to comment.