Exemple #1
0
 /**
  * Retries a POST request.
  *
  * @param \Drupal\feeds\SubscriptionInterface $subscription
  *   The subscription.
  * @param array $body
  *   The POST body.
  * @param int $retries
  *   (optional) The number of retries. Defaults to 3.
  *
  * @return \GuzzleHttp\Message\Response
  *   The Guzzle response.
  */
 protected static function retry(SubscriptionInterface $subscription, array $body, $retries = 3)
 {
     $tries = 0;
     do {
         $tries++;
         try {
             return \Drupal::httpClient()->post($subscription->getHub(), ['body' => $body]);
         } catch (RequestException $e) {
             \Drupal::logger('feeds')->warning('Subscription error: %error', ['%error' => $e->getMessage()]);
         }
     } while ($tries <= $retries);
 }
 /**
  * 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);
 }