You are here

protected function CronHandler::getMedia in Instagram Feeds 8

Gets 25 recent posts created by Instagram user.

Parameters

\Drupal\instagram_feeds\Entity\InstagramAccountInterface $account: Instagram account.

Return value

array The list of Instagram posts starting from oldest to newest.

Throws

\Exception

1 call to CronHandler::getMedia()
CronHandler::processAccount in src/CronHandler.php
Instagram posts import processor for the given account.

File

src/CronHandler.php, line 147

Class

CronHandler
Instagram Feeds Cron Handler Service.

Namespace

Drupal\instagram_feeds

Code

protected function getMedia(InstagramAccountInterface $account) {
  try {
    $request_url = $account::INSTAGRAM_GRAPH_ENDPOINT . '/me/media?' . http_build_query([
      'fields' => 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username',
      'access_token' => $account
        ->getToken(),
    ]);
    $body = $this
      ->getInstagramResponceContents($request_url, TRUE);
    $result = array_filter($body['data'] ?: [], [
      $this,
      'filterPostByPermalink',
    ]);
  } catch (\Exception $e) {
    $this->logger
      ->error($e
      ->getMessage());
  }

  // Dispatch the event.
  $event = new InstagramPostsObtainedEvent($this->config, $account, $result ?? []);
  $this->eventDispatcher
    ->dispatch(InstagramPostsObtainedEvent::getEventName(), $event);
  return $event->posts;
}