LinkedInAuthManager.php in Open Social 8.5
File
modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php
View source
<?php
namespace Drupal\social_auth_linkedin;
use Drupal\social_auth_extra\AuthManager;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Happyr\LinkedIn\LinkedIn;
use Happyr\LinkedIn\Exception\LinkedInException;
use Drupal\social_auth_linkedin\Settings\LinkedInAuthSettings;
class LinkedInAuthManager extends AuthManager {
public function getSocialNetworkKey() {
return LinkedInAuthSettings::getSocialNetworkKey();
}
public function setSdk($sdk) {
if (!$sdk instanceof LinkedIn) {
throw new InvalidArgumentException('SDK object should be instance of \\Happyr\\LinkedIn\\LinkedIn class');
}
$this->sdk = $sdk;
}
public function getAuthenticationUrl($type, array $scope = [
'r_basicprofile',
'r_emailaddress',
]) {
return $this->sdk
->getLoginUrl([
'redirect_uri' => $this
->getRedirectUrl($type),
'scope' => implode(',', $scope),
]);
}
public function getAccessToken($type = '') {
try {
$access_token = $this->sdk
->getAccessToken();
return $access_token;
} catch (LinkedInException $e) {
$this->loggerFactory
->get('social_auth_linkedin')
->error('Could not get LinkedIn access token. LinkedInException: @message', [
'@message' => $e
->getMessage(),
]);
return NULL;
}
if ($access_token) {
$this->sdk
->setAccessToken($access_token);
return $access_token;
}
$this->loggerFactory
->get('social_auth_linkedin')
->error('Could not get LinkedIn access token. User cancelled the dialog in Facebook or return URL was not valid.');
return NULL;
}
public function getProfile() {
if (!$this->profile) {
if (($profile = $this->sdk
->get('v1/people/~:(id,firstName,lastName,email-address,formattedName,pictureUrls::(original))')) && !isset($profile['errorCode'])) {
$this->profile = $profile;
}
}
return $this->profile;
}
public function getProfilePicture() {
if (!empty($this->profile['pictureUrls']['_total'])) {
return end($this->profile['pictureUrls']['values']);
}
}
public function setAccessToken($access_token) {
$this->sdk
->setAccessToken($access_token);
}
public function getAccountId() {
return isset($this->profile['id']) ? $this->profile['id'] : NULL;
}
public function getFirstName() {
return isset($this->profile['firstName']) ? $this->profile['firstName'] : NULL;
}
public function getLastName() {
return isset($this->profile['lastName']) ? $this->profile['lastName'] : NULL;
}
}