Skip to content

Commit

Permalink
Add User::getProviderName()
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jul 3, 2017
1 parent c8e013f commit 306d6df
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PHP >= 5.4
# Installation

```shell
$ composer require "overtrue/socialite:~1.0"
$ composer require "overtrue/socialite:~1.1"
```

# Usage
Expand Down Expand Up @@ -66,6 +66,7 @@ $user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$user->getName(); // "安正超"
$user->getEmail(); // "[email protected]"
$user->getProviderName(); // GitHub
...
```

Expand Down Expand Up @@ -185,6 +186,7 @@ $user->getEmail();
$user->getAvatar();
$user->getOriginal();
$user->getToken();// or $user->getAccessToken()
$user->getProviderName(); // GitHub/Google/Facebook...
```

#### Get original response from OAuth API
Expand Down
20 changes: 19 additions & 1 deletion src/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
abstract class AbstractProvider implements ProviderInterface
{
/** Provider name
*
* @var string
*/
protected $name;

/**
* The HTTP request instance.
*
Expand Down Expand Up @@ -176,7 +182,7 @@ public function user(AccessTokenInterface $token = null)

$user = $this->mapUserToObject($user)->merge(['original' => $user]);

return $user->setToken($token);
return $user->setToken($token)->setProviderName($this->getName());
}

/**
Expand Down Expand Up @@ -300,6 +306,18 @@ public function with(array $parameters)
return $this;
}

/**
* @return string
*/
public function getName()
{
if (empty($this->name)) {
$this->name = strstr((new \ReflectionClass(get_class($this)))->getShortName(), 'Provider', true);
}

return $this->name;
}

/**
* Get the authentication URL for the provider.
*
Expand Down
22 changes: 21 additions & 1 deletion src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ public function setToken(AccessTokenInterface $token)
return $this;
}

/**
* @param string $provider
*
* @return $this
*/
public function setProviderName($provider)
{
$this->setAttribute('provider', $provider);

return $this;
}

/**
* @return string
*/
public function getProviderName()
{
return $this->getAttribute('provider');
}

/**
* Get the authorized token.
*
Expand All @@ -122,7 +142,7 @@ public function getToken()
*/
public function getAccessToken()
{
return $this->token;
return $this->getToken();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ public function testExceptionIsThrownIfStateIsNotSet()
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
$user = $provider->user();
}

public function testDriverName()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');

$this->assertSame('OAuthTwoTest', $provider->getName());
}
}

class OAuthTwoTestProviderStub extends AbstractProvider
Expand Down

0 comments on commit 306d6df

Please sign in to comment.