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 = \Drupal::url('entity.feeds_feed.subscribe', $args, ['absolute' => TRUE]); $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; } } }
/** * Receives a notification. * * @param \Drupal\feeds\SubscriptionInterface $feeds_subscription * The subscription entity. * @param string $feeds_push_token * The subscription token. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return Symfony\Component\HttpFoundation\Response * The response object. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * Thrown if anything seems amiss. */ public function receive(SubscriptionInterface $feeds_subscription, $feeds_push_token, Request $request) { if ($feeds_subscription->getToken() !== $feeds_push_token) { throw new NotFoundHttpException(); } // X-Hub-Signature is in the format sha1=signature. parse_str($request->headers->get('X-Hub-Signature'), $result); if (empty($result['sha1']) || !$feeds_subscription->checkSignature($result['sha1'], $request->getContent())) { throw new NotFoundHttpException(); } $feed = $this->entityManager()->getStorage('feeds_feed')->load($feeds_subscription->id()); try { $feed->pushImport($request->getContent()); } catch (\Exception $e) { return new Response('', 500); } return new Response('', 200); }