You are here

public static function PubSubHubbub::runSubscribeBatch in Feeds 8.3

Subscribes to or unsubscribes from a hub.

This method is used as callback for a batch.

Parameters

\Drupal\feeds\SubscriptionInterface $subscription: The subscription entity.

See also

::subscribe

::unsubscribe

File

src/EventSubscriber/PubSubHubbub.php, line 197

Class

PubSubHubbub
Event listener for PubSubHubbub subscriptions.

Namespace

Drupal\feeds\EventSubscriber

Code

public static function runSubscribeBatch(SubscriptionInterface $subscription) {
  switch ($subscription
    ->getState()) {
    case 'subscribing':
      $mode = 'subscribe';
      break;
    case 'unsubscribing':
      $mode = 'unsubscribe';

      // The subscription has been deleted, store it for a bit to handle the
      // response.
      $id = $subscription
        ->getToken() . ':' . $subscription
        ->id();
      \Drupal::keyValueExpirable('feeds_push_unsubscribe')
        ->setWithExpire($id, $subscription, 3600);
      break;
    default:
      throw new \LogicException('A subscription was found in an invalid state.');
  }
  $args = [
    'feeds_subscription_id' => $subscription
      ->id(),
    'feeds_push_token' => $subscription
      ->getToken(),
  ];
  $callback = CoreUrl::fromRoute('entity.feeds_feed.subscribe', $args, [
    'absolute' => TRUE,
  ])
    ->toString();
  $post_body = [
    'hub.callback' => $callback,
    'hub.mode' => $mode,
    'hub.topic' => $subscription
      ->getTopic(),
    'hub.secret' => $subscription
      ->getSecret(),
  ];
  $response = static::retry($subscription, $post_body);

  // Response failed.
  if (!$response || $response
    ->getStatusCode() != 202) {
    switch ($subscription
      ->getState()) {
      case 'subscribing':

        // Deleting the subscription will make it re-subscribe on the next
        // import.
        $subscription
          ->delete();
        break;
      case 'unsubscribing':

        // Unsubscribe failed. The hub should give up eventually.
        break;
    }
  }
}