You are here

public function NewResultsCheck::checkAll in Search API Saved Searches 8

Checks all saved searches that are "due" for new results.

Parameters

string|null $type_id: (optional) The type of saved searches to check, or NULL to check searches for all enabled types that have at least one notification plugin set.

Return value

int The number of saved searches that were successfully checked for new results.

File

src/Service/NewResultsCheck.php, line 95

Class

NewResultsCheck
Provides a service for checking saved searches for new results.

Namespace

Drupal\search_api_saved_searches\Service

Code

public function checkAll($type_id = NULL) {
  $search_ids = $this
    ->getSearchesToCheck($type_id);
  if (!$search_ids) {
    return 0;
  }
  $count = 0;
  $now = $this->time
    ->getRequestTime();

  /** @var \Drupal\search_api_saved_searches\SavedSearchInterface $search */
  foreach ($this
    ->getSearchStorage()
    ->loadMultiple($search_ids) as $search) {
    try {
      $results = $this
        ->getNewResults($search);
      $search
        ->set('last_executed', $now);
      $search
        ->save();
      ++$count;
      if (!$results) {
        continue;
      }
      foreach ($search
        ->getType()
        ->getNotificationPlugins() as $plugin) {
        $plugin
          ->notify($search, $results);
      }
    } catch (\Exception $e) {
      $args['@search_id'] = $search
        ->id();
      watchdog_exception('search_api_saved_searches', $e, '%type while trying to find new results for saved search #@search_id: @message in %function (line %line of %file).', $args);
    }
  }
  return $count;
}