social_post_facebook-8.x-1.x-dev/src/FacebookPostAuthManager.php

src/FacebookPostAuthManager.php
<?php

namespace Drupal\social_post_facebook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\social_api\Plugin\NetworkManager;
use Drupal\social_api\SocialApiException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\social_post\PostManager\OAuth2Manager;

/**
 * Manages the authorization process and post on user behalf.
 */
class FacebookPostAuthManager extends OAuth2Manager {
  /**
   * The session manager.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * The Facebook client object.
   *
   * @var \League\OAuth2\Client\Provider\Facebook
   */
  protected mixed $client;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected ?Request $request;

  /**
   * The Social Api network manager.
   *
   * @var \Drupal\social_api\Plugin\NetworkManager
   */
  protected $networkManager;

  /**
   * The Facebook access token.
   *
   * @var \League\OAuth2\Client\Token\AccessToken
   */
  protected $token;

  /**
   * The Facebook page long lived access token.
   *
   * @var string
   */
  protected $longLivedToken;

  /**
   * The post data.
   *
   * @var array
   */
  protected $postData;

  /**
   * The target pageId for posting.
   *
   * @var array
   */
  protected $pageId;

  /**
   * FacebookPostManager constructor.
   *
   * @param \Symfony\Component\HttpFoundation\Session\Session $session
   *   The session manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Used to get the parameter code returned by Facebook.
   * @param \Drupal\social_api\Plugin\NetworkManager $network_manager
   *   The Social API network manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   */
  public function __construct(Session $session, RequestStack $request_stack, NetworkManager $network_manager, ConfigFactoryInterface $configFactory) {
    $this->session = $session;
    $this->request = $request_stack->getCurrentRequest();
    $this->networkManager = $network_manager;
    $this->settings = $configFactory->get('social_post_facebook.settings');
  }

  /**
   * Saves access token.
   */
  public function authenticate() {
    $this->token = $this->client->getAccessToken(
      'authorization_code',
      ['code' => $this->request->query->get('code')]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthorizationUrl(): mixed {
    $config_id = $this->settings->get('config_id');
    if ($config_id) {
      $options = ['config_id' => $config_id];
    }
    else {
      $options = ['scopes' => ['email']];
    }
    $login_url = $this->client->getAuthorizationUrl($options);
    // Generate and return the URL where we should redirect the user.
    return $login_url;
  }

  /**
   * Gets the data by using the access token returned.
   *
   * @return League\OAuth2\Client\Provider\FacebookUser
   *   User Info returned by the facebook.
   */
  public function getUserInfo(): mixed {
    $this->user = $this->client->getResourceOwner($this->token);
    return [
      'user' => $this->user,
      'pages' => $this->client->getAllLongLivedPageAccessTokens($this->token),
    ];
  }

  /**
   * Returns token generated after authorization.
   *
   * @return string
   *   Used for making API calls.
   */
  public function getAccessToken(): mixed {
    return $this->token;
  }

  /**
   * Returns the Google login URL where user will be redirected.
   *
   * @return string
   *   Absolute Google login URL where user will be redirected
   */
  public function getState(): string {
    $state = $this->client->getState();

    // Generate and return the URL where we should redirect the user.
    return $state;
  }

  /**
   * {@inheritdoc}
   */
  public function post() {
    if (!$this->client) {
      throw new SocialApiException('Call post() method from its wrapper doPost()');
    }

    $response = $this->client->postToPage($this->pageId, $this->longLivedToken, $this->postData['status']);

    if ($response->getStatusCode() != 200) {
      return FALSE;
    }

    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function doPost($post, $user_id) {
    $accounts = _social_post_facebook_get_accounts_by_uid(($user_id));
    $account = array_pop($accounts);
    $pages = social_post_facebook_get_pages($account);
    $page_id = array_key_first($pages);
    $access_token = $pages[$page_id]['long_access_token'];

    /** @var \Drupal\social_post_facebook\Plugin\Network\FacebookPost $network_plugin */
    $network_plugin = $this->networkManager->createInstance('social_post_facebook');
    $this->client = $network_plugin->getSdk();
    $this->postData['status'] = is_array($post) && !empty($post['status']) ? $post['status'] : $post;
    $this->longLivedToken = $access_token;
    $this->pageId = $page_id;
    return $this->post();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc