You are here

class FacebookAuth in Social Auth Facebook 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Network/FacebookAuth.php \Drupal\social_auth_facebook\Plugin\Network\FacebookAuth
  2. 3.x src/Plugin/Network/FacebookAuth.php \Drupal\social_auth_facebook\Plugin\Network\FacebookAuth

Defines a Network Plugin for Social Auth Facebook.

@package Drupal\simple_fb_connect\Plugin\Network

Plugin annotation


@Network(
  id = "social_auth_facebook",
  social_network = "Facebook",
  type = "social_auth",
  handlers = {
    "settings": {
      "class": "\Drupal\social_auth_facebook\Settings\FacebookAuthSettings",
      "config_id": "social_auth_facebook.settings"
    }
  }
)

Hierarchy

Expanded class hierarchy of FacebookAuth

File

src/Plugin/Network/FacebookAuth.php, line 32

Namespace

Drupal\social_auth_facebook\Plugin\Network
View source
class FacebookAuth extends NetworkBase implements FacebookAuthInterface {

  /**
   * The Facebook Persistent Data Handler.
   *
   * @var \Drupal\social_auth_facebook\FacebookAuthPersistentDataHandler
   */
  protected $persistentDataHandler;

  /**
   * The logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   */
  protected $loggerFactory;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($container
      ->get('social_auth_facebook.persistent_data_handler'), $configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('config.factory'), $container
      ->get('logger.factory'));
  }

  /**
   * FacebookAuth constructor.
   *
   * @param \Drupal\social_auth_facebook\FacebookAuthPersistentDataHandler $persistent_data_handler
   *   The persistent data handler.
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory object.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   */
  public function __construct(FacebookAuthPersistentDataHandler $persistent_data_handler, array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $config_factory);
    $this->persistentDataHandler = $persistent_data_handler;
    $this->loggerFactory = $logger_factory;
  }

  /**
   * Sets the underlying SDK library.
   *
   * @return \Facebook\Facebook
   *   The initialized 3rd party library instance.
   *
   * @throws SocialApiException
   *   If the SDK library does not exist.
   */
  protected function initSdk() {
    $class_name = '\\Facebook\\Facebook';
    if (!class_exists($class_name)) {
      throw new SocialApiException(sprintf('The PHP SDK for Facebook could not be found. Class: %s.', $class_name));
    }

    /* @var \Drupal\social_auth_facebook\Settings\FacebookAuthSettings $settings */
    $settings = $this->settings;
    if ($this
      ->validateConfig($settings)) {

      // All these settings are mandatory.
      $facebook_settings = [
        'app_id' => $settings
          ->getAppId(),
        'app_secret' => $settings
          ->getAppSecret(),
        'default_graph_version' => 'v' . $settings
          ->getGraphVersion(),
        'persistent_data_handler' => $this->persistentDataHandler,
        'http_client_handler' => $this
          ->getHttpClient(),
      ];
      return new Facebook($facebook_settings);
    }
    return FALSE;
  }

  /**
   * Checks that module is configured.
   *
   * @param \Drupal\social_auth_facebook\Settings\FacebookAuthSettings $settings
   *   The Facebook auth settings.
   *
   * @return bool
   *   True if module is configured.
   *   False otherwise.
   */
  protected function validateConfig(FacebookAuthSettings $settings) {
    $app_id = $settings
      ->getAppId();
    $app_secret = $settings
      ->getAppSecret();
    $graph_version = $settings
      ->getGraphVersion();
    if (!$app_id || !$app_secret || !$graph_version) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Define App ID and App Secret on module settings.');
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Returns HTTP client to be used with Facebook SDK.
   *
   * Facebook SDK v5 uses the following autodetect logic for determining the
   * HTTP client:
   * 1. If cURL extension is loaded, use it.
   * 2. If cURL was not loaded but Guzzle is found, use it.
   * 3. Fallback to FacebookStreamHttpClient.
   *
   * Drupal 8 ships with Guzzle v6 but Facebook SDK v5 works only
   * with Guzzle v5. Therefore we need to change the autodetect logic
   * so that we're first using cURL and if that is not available, we
   * fallback directly to FacebookStreamHttpClient.
   *
   * @return string
   *   Client that should be used with Facebook SDK.
   */
  protected function getHttpClient() {
    if (extension_loaded('curl')) {
      return 'curl';
    }
    return 'stream';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookAuth::$loggerFactory protected property The logger factory.
FacebookAuth::$persistentDataHandler protected property The Facebook Persistent Data Handler.
FacebookAuth::create public static function
FacebookAuth::getHttpClient protected function Returns HTTP client to be used with Facebook SDK.
FacebookAuth::initSdk protected function Sets the underlying SDK library.
FacebookAuth::validateConfig protected function Checks that module is configured.
FacebookAuth::__construct public function FacebookAuth constructor.