LinkedInAuthManager.php in Open Social 8.8
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 LinkedIn\Client;
use LinkedIn\Scope;
use Drupal\social_auth_linkedin\Settings\LinkedInAuthSettings;
class LinkedInAuthManager extends AuthManager {
public function getSocialNetworkKey() {
return LinkedInAuthSettings::getSocialNetworkKey();
}
public function setSdk($sdk) {
if (!$sdk instanceof Client) {
throw new InvalidArgumentException('SDK object should be instance of \\LinkedIn\\Client class');
}
$this->sdk = $sdk;
}
public function getAuthenticationUrl($type, array $scope = [
Scope::READ_LITE_PROFILE,
Scope::READ_EMAIL_ADDRESS,
]) {
$redirect_url = $this
->getRedirectUrl($type);
$this->sdk
->setRedirectUrl($redirect_url);
return $this->sdk
->getLoginUrl([
'scope' => implode(',', $scope),
]);
}
public function getAccessToken($type = '') {
try {
$redirect_url = $this
->getRedirectUrl($type);
$this->sdk
->setRedirectUrl($redirect_url);
$access_token = $this->sdk
->getAccessToken($_GET['code']);
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 LinkedIn or return URL was not valid.');
return NULL;
}
public function getProfile() {
if (!$this->profile) {
if (($profile = $this->sdk
->get('me', [
'fields' => 'id,firstName,lastName',
])) && !isset($profile['errorCode'])) {
$this->profile['id'] = $profile['id'];
$this->profile['basic_information'] = $profile;
}
if (($profile = $this->sdk
->get('me', [
'projection' => '(id,profilePicture(displayImage~:playableStreams))',
])) && !isset($profile['errorCode'])) {
$this->profile['profile_picture'] = $profile;
}
if (($profile = $this->sdk
->get('emailAddress', [
'q' => 'members',
'projection' => '(elements*(handle~))',
])) && !isset($profile['errorCode'])) {
$this->profile['email_information'] = $profile;
}
}
return $this->profile;
}
public function getProfilePicture() {
if (!empty($this->profile['profile_picture']['profilePicture']['displayImage~']['elements'])) {
$profile_picture = end($this->profile['profile_picture']['profilePicture']['displayImage~']['elements']);
return $profile_picture['identifiers'][0]['identifier'];
}
}
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() {
if (!empty($this->profile['basic_information']['firstName'])) {
return array_values($this->profile['basic_information']['firstName']['localized'])[0];
}
}
public function getLastName() {
if (!empty($this->profile['basic_information']['lastName'])) {
return array_values($this->profile['basic_information']['lastName']['localized'])[0];
}
}
public function getEmailAddress() {
if (!empty($this->profile['email_information']['elements'])) {
return $this->profile['email_information']['elements'][0]['handle~']['emailAddress'];
}
}
}