You are here

public function PubSubHubbub::onPostFetch in Feeds 8.3

Subscribes to a feed.

Parameters

\Drupal\feeds\Event\FetchEvent $event: The fetch event.

File

src/EventSubscriber/PubSubHubbub.php, line 70

Class

PubSubHubbub
Event listener for PubSubHubbub subscriptions.

Namespace

Drupal\feeds\EventSubscriber

Code

public function onPostFetch(FetchEvent $event) {
  $feed = $event
    ->getFeed();
  $fetcher = $feed
    ->getType()
    ->getFetcher();
  $subscription = $this->storage
    ->load($feed
    ->id());
  if (!$fetcher
    ->getConfiguration('use_pubsubhubbub')) {
    return $this
      ->unsubscribe($feed, $subscription);
  }
  if (!($hub = $this
    ->findRelation($event
    ->getFetcherResult(), 'hub'))) {
    $hub = $fetcher
      ->getConfiguration('fallback_hub');
  }

  // No hub found.
  if (!$hub) {
    return $this
      ->unsubscribe($feed, $subscription);
  }

  // Used to make other URLs absolute.
  $source_url = Url::fromString($feed
    ->getSource());
  $hub = (string) $source_url
    ->combine($hub);

  // If there is a rel="self" relation.
  if ($topic = $this
    ->findRelation($event
    ->getFetcherResult(), 'self')) {
    $topic = (string) $source_url
      ->combine($topic);
    $feed
      ->setSource($topic);
  }
  else {
    $topic = $feed
      ->getSource();
  }

  // Subscription does not exist yet.
  if (!$subscription) {
    $subscription = $this->storage
      ->create([
      'fid' => $feed
        ->id(),
      'topic' => $topic,
      'hub' => $hub,
    ]);
    return $this
      ->subscribe($feed, $subscription);
  }
  if ($topic !== $subscription
    ->getTopic() || $subscription
    ->getHub() !== $hub || $subscription
    ->getState() !== 'subscribed') {

    // Unsubscribe from the old feed.
    $this
      ->unsubscribe($feed, $subscription);
    $subscription = $this->storage
      ->create([
      'fid' => $feed
        ->id(),
      'topic' => $topic,
      'hub' => $hub,
    ]);
    return $this
      ->subscribe($feed, $subscription);
  }
}