1c4b211bad653188a3c0facb2559ae97d0be8732
[yaffs-website] / web / modules / contrib / media_entity_twitter / src / TweetFetcher.php
1 <?php
2
3 namespace Drupal\media_entity_twitter;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\media_entity_twitter\Exception\TwitterApiException;
8
9 /**
10  * Fetches (and caches) tweet data from Twitter's API.
11  */
12 class TweetFetcher implements TweetFetcherInterface {
13
14   /**
15    * The optional cache backend.
16    *
17    * @var \Drupal\Core\Cache\CacheBackendInterface
18    */
19   protected $cache;
20
21   /**
22    * The current set of Twitter API credentials.
23    *
24    * @var array
25    */
26   protected $credentials = [];
27
28   /**
29    * The current API exchange object.
30    *
31    * @var \TwitterAPIExchange
32    */
33   protected $twitter;
34
35   /**
36    * TweetFetcher constructor.
37    *
38    * @param \Drupal\Core\Cache\CacheBackendInterface|NULL $cache
39    *   (optional) A cache bin for storing fetched tweets.
40    */
41   public function __construct(CacheBackendInterface $cache = NULL) {
42     $this->cache = $cache;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function fetchTweet($id) {
49     // Tweets don't change much, so pull it out of the cache (if we have one)
50     // if this one has already been fetched.
51     if ($this->cache && $cached_tweet = $this->cache->get($id)) {
52       return $cached_tweet->data;
53     }
54
55     // Ensure that we have an actual API exchange instance.
56     if (empty($this->twitter)) {
57       throw new \UnexpectedValueException('Twitter API exchange has not been initialized; credentials may not have been set yet.');
58     }
59
60     // Query Twitter's API.
61     $response = $this->twitter
62       ->setGetfield('?id=' . $id)
63       ->buildOAuth('https://api.twitter.com/1.1/statuses/show.json', 'GET')
64       ->performRequest();
65
66     if (empty($response)) {
67       throw new \Exception("Could not retrieve tweet $id.");
68     }
69     // Handle errors as per https://dev.twitter.com/overview/api/response-codes.
70     if (!empty($response['errors'])) {
71       throw new TwitterApiException($response['errors']);
72     }
73
74     $response = Json::decode($response);
75     // If we have a cache, store the response for future use.
76     if ($this->cache) {
77       // Tweets don't change often, so the response should expire from the cache
78       // on its own in 90 days.
79       $this->cache->set($id, $response, time() + (86400 * 90));
80     }
81
82     return $response;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getCredentials() {
89     return $this->credentials;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function setCredentials($consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret) {
96     $this->credentials = [
97       'consumer_key' => $consumer_key,
98       'consumer_secret' => $consumer_secret,
99       'oauth_access_token' => $oauth_access_token,
100       'oauth_access_token_secret' => $oauth_access_token_secret,
101     ];
102     $this->twitter = new \TwitterAPIExchange($this->credentials);
103   }
104
105 }