View source
<?php
namespace Drupal\simple_fb_connect;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;
use Facebook\GraphNodes\GraphNode;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class SimpleFbConnectFbManager {
protected $loggerFactory;
protected $eventDispatcher;
protected $entityFieldManager;
protected $urlGenerator;
protected $persistentDataHandler;
protected $facebook;
public function __construct(LoggerChannelFactoryInterface $logger_factory, EventDispatcherInterface $event_dispatcher, EntityFieldManagerInterface $entity_field_manager, UrlGeneratorInterface $url_generator, SimpleFbConnectPersistentDataHandler $persistent_data_handler) {
$this->loggerFactory = $logger_factory;
$this->eventDispatcher = $event_dispatcher;
$this->entityFieldManager = $entity_field_manager;
$this->urlGenerator = $url_generator;
$this->persistentDataHandler = $persistent_data_handler;
$this->facebook = NULL;
}
public function setFacebookService(Facebook $facebook) {
$this->facebook = $facebook;
}
public function getFbLoginUrl() {
$login_helper = $this->facebook
->getRedirectLoginHelper();
$return_url = $this->urlGenerator
->generateFromRoute('simple_fb_connect.return_from_fb', [], [
'absolute' => TRUE,
]);
$scope = [
'public_profile',
'email',
];
$e = new GenericEvent($scope, [
'scope' => $scope,
]);
$event = $this->eventDispatcher
->dispatch('simple_fb_connect.scope', $e);
$final_scope = $event
->getArgument('scope');
return $login_helper
->getLoginUrl($return_url, $final_scope);
}
public function getFbReRequestUrl() {
$login_helper = $this->facebook
->getRedirectLoginHelper();
$return_url = $this->urlGenerator
->generateFromRoute('simple_fb_connect.return_from_fb', [], [
'absolute' => TRUE,
]);
$scope = [
'public_profile',
'email',
];
return $login_helper
->getReRequestUrl($return_url, $scope);
}
public function getAccessTokenFromFb() {
$helper = $this->facebook
->getRedirectLoginHelper();
$return_url = $this->urlGenerator
->generateFromRoute('simple_fb_connect.return_from_fb', [], [
'absolute' => TRUE,
]);
try {
$access_token = $helper
->getAccessToken($return_url);
} catch (FacebookResponseException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not get Facebook access token. FacebookResponseException: @message', [
'@message' => json_encode($ex
->getMessage()),
]);
return FALSE;
} catch (FacebookSDKException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not get Facebook access token. Exception: @message', [
'@message' => $ex
->getMessage(),
]);
}
if (isset($access_token)) {
$this->facebook
->setDefaultAccessToken($access_token);
return $access_token;
}
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not get Facebook access token. User cancelled the dialog in Facebook or return URL was not valid.');
return NULL;
}
public function checkPermission($permission_to_check) {
try {
$permissions = $this->facebook
->get('/me/permissions')
->getGraphEdge()
->asArray();
foreach ($permissions as $permission) {
if ($permission['permission'] == $permission_to_check && $permission['status'] == 'granted') {
return TRUE;
}
}
} catch (FacebookResponseException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not check Facebook permissions: FacebookResponseException: @message', [
'@message' => json_encode($ex
->getMessage()),
]);
} catch (FacebookSDKException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not check Facebook permissions: FacebookSDKException: @message', [
'@message' => $ex
->getMessage(),
]);
}
return FALSE;
}
public function getFbProfile() {
try {
return $this->facebook
->get('/me?fields=id,name,email')
->getGraphNode();
} catch (FacebookResponseException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not load Facebook user profile: FacebookResponseException: @message', [
'@message' => json_encode($ex
->getMessage()),
]);
} catch (FacebookSDKException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not load Facebook user profile: FacebookSDKException: @message', [
'@message' => $ex
->getMessage(),
]);
}
return FALSE;
}
public function getFbProfilePic() {
$resolution = $this
->getPreferredResolution();
$query = '/me/picture?redirect=false';
if (is_array($resolution)) {
$query .= '&width=' . $resolution['width'] . '&height=' . $resolution['height'];
}
try {
$graph_node = $this->facebook
->get($query)
->getGraphNode();
return $graph_node;
} catch (FacebookResponseException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not load Facebook profile picture. FacebookResponseException: @message', [
'@message' => json_encode($ex
->getMessage()),
]);
} catch (FacebookSDKException $ex) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not load Facebook profile picture. FacebookSDKException: @message', [
'@message' => $ex
->getMessage(),
]);
}
return FALSE;
}
public function getFbProfilePicUrl() {
if ($graph_node = $this
->getFbProfilePic()) {
return $graph_node
->getField('url');
}
return FALSE;
}
public function getEmail(GraphNode $fb_profile) {
if ($email = $fb_profile
->getField('email')) {
return $email;
}
$this->loggerFactory
->get('simple_fb_connect')
->error('No email address in Facebook user profile');
return FALSE;
}
protected function getPreferredResolution() {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('user', 'user');
if (!isset($field_definitions['user_picture'])) {
return FALSE;
}
$max_resolution = $field_definitions['user_picture']
->getSetting('max_resolution');
$min_resolution = $field_definitions['user_picture']
->getSetting('min_resolution');
if ($max_resolution) {
$resolution = $max_resolution;
}
elseif ($min_resolution) {
$resolution = $min_resolution;
}
else {
return FALSE;
}
$dimensions = explode('x', $resolution);
return [
'width' => $dimensions[0],
'height' => $dimensions[1],
];
}
}