You are here

class TwitterPostCollector in Social Feed 8

Class TwitterPostCollector.

@package Drupal\socialfeed\Services

Hierarchy

Expanded class hierarchy of TwitterPostCollector

File

src/Services/TwitterPostCollector.php, line 12

Namespace

Drupal\socialfeed\Services
View source
class TwitterPostCollector {

  /**
   * The twitter application consumer key.
   *
   * @var string
   */
  protected $consumerKey;

  /**
   * The twitter application consumer secret.
   *
   * @var string
   */
  protected $consumerSecret;

  /**
   * The twitter application access token.
   *
   * @var string
   */
  protected $accessToken;

  /**
   * The twitter application access token secret.
   *
   * @var string
   */
  protected $accessTokenSecret;

  /**
   * Twitter OAuth client.
   *
   * @var \Abraham\TwitterOAuth\TwitterOAuth
   */
  protected $twitter;

  /**
   * TwitterPostCollector constructor.
   *
   * @param string $consumerKey
   *   $consumerKey.
   * @param string $consumerSecret
   *   $consumerSecret.
   * @param string $accessToken
   *   $accessToken.
   * @param string $accessTokenSecret
   *   $accessTokenSecret.
   * @param \Abraham\TwitterOAuth\TwitterOAuth|null $twitter
   *   Twitter OAuth Client.
   */
  public function __construct($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, TwitterOAuth $twitter = NULL) {
    $this->consumerKey = $consumerKey;
    $this->consumerSecret = $consumerSecret;
    $this->accessToken = $accessToken;
    $this->accessTokenSecret = $accessTokenSecret;
    $this->twitter = $twitter;
    $this
      ->setTwitterClient();
  }

  /**
   * Set the Twitter client.
   */
  public function setTwitterClient() {
    if (NULL === $this->twitter) {
      $this->twitter = new TwitterOAuth($this->consumerKey, $this->consumerSecret, $this->accessToken, $this->accessTokenSecret);
    }
  }

  /**
   * Retrieve Tweets from the given accounts home page.
   *
   * @param int $count
   *   The number of posts to return.
   *
   * @return array
   *   An array of posts.
   */
  public function getPosts($count) {
    return $this->twitter
      ->get('statuses/user_timeline', [
      'count' => $count,
      'tweet_mode' => 'extended',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwitterPostCollector::$accessToken protected property The twitter application access token.
TwitterPostCollector::$accessTokenSecret protected property The twitter application access token secret.
TwitterPostCollector::$consumerKey protected property The twitter application consumer key.
TwitterPostCollector::$consumerSecret protected property The twitter application consumer secret.
TwitterPostCollector::$twitter protected property Twitter OAuth client.
TwitterPostCollector::getPosts public function Retrieve Tweets from the given accounts home page.
TwitterPostCollector::setTwitterClient public function Set the Twitter client.
TwitterPostCollector::__construct public function TwitterPostCollector constructor.