You are here

protected function CronHandler::processAccount in Instagram Feeds 8

Instagram posts import processor for the given account.

Parameters

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

1 call to CronHandler::processAccount()
CronHandler::importInstagramPosts in src/CronHandler.php
Cron handler to import Instagram posts for all configured accounts.

File

src/CronHandler.php, line 184

Class

CronHandler
Instagram Feeds Cron Handler Service.

Namespace

Drupal\instagram_feeds

Code

protected function processAccount(InstagramAccountInterface $account) {
  $post_count = 0;
  $max_posts = $account
    ->getCronLimit();
  $last_imported_timestamp = $account
    ->getLastImportTimestamp();
  foreach ($this
    ->getMedia($account) as $post) {

    // Stop foreach if we have already reached the limit.
    if ($post_count >= $max_posts) {
      break;
    }

    // Only save the insta post if its timestamp is after the saved last
    // import date to prevent duplicates.
    if ($post['timestamp'] > $account
      ->getLastImportTimestamp()) {
      $this
        ->createMediaEntity($post, $account);
      $last_imported_timestamp = $post['timestamp'];
      $post_count++;
    }
  }

  // Update the last imported date only if there were new Instagram posts imported.
  if ($post_count > 0) {
    $account
      ->setLastImportTimestamp($last_imported_timestamp)
      ->save();
  }
  $logger_context = [
    '@account' => $account
      ->label(),
    '@count' => $post_count,
  ];
  $this->logger
    ->info("@count post(s) imported from @account account.<br />\n", $logger_context);
}