Newer
Older
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher.
*/
namespace Drupal\aggregator\Plugin\aggregator\fetcher;
use Drupal\aggregator\Plugin\FetcherInterface;
use Drupal\aggregator\FeedInterface;
Alex Pott
committed
use Drupal\Component\Datetime\DateTimePlus;
Alex Pott
committed
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
Alex Pott
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a default fetcher implementation.
*
catch
committed
* Uses the http_client service to download the feed.
Dries Buytaert
committed
*
Dries Buytaert
committed
* @AggregatorFetcher(
Dries Buytaert
committed
* id = "aggregator",
* title = @Translation("Default fetcher"),
* description = @Translation("Downloads data from a URL using Drupal's HTTP request handler.")
* )
*/
Alex Pott
committed
class DefaultFetcher implements FetcherInterface, ContainerFactoryPluginInterface {
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\ClientInterface
Alex Pott
committed
*/
protected $httpClient;
/**
* Constructs a DefaultFetcher object.
*
* @param \GuzzleHttp\ClientInterface $http_client
Alex Pott
committed
* A Guzzle client object.
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Alex Pott
committed
return new static(
catch
committed
$container->get('http_client')
Alex Pott
committed
);
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function fetch(FeedInterface $feed) {
$request = $this->httpClient->createRequest('GET', $feed->getUrl());
$feed->source_string = FALSE;
// Generate conditional GET headers.
Angie Byron
committed
if ($feed->getEtag()) {
$request->addHeader('If-None-Match', $feed->getEtag());
}
Angie Byron
committed
if ($feed->getLastModified()) {
Alex Pott
committed
$request->addHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified()));
}
try {
$response = $this->httpClient->send($request);
// In case of a 304 Not Modified, there is no new content, so return
// FALSE.
if ($response->getStatusCode() == 304) {
return FALSE;
}
$feed->source_string = $response->getBody(TRUE);
$feed->setEtag($response->getHeader('ETag'));
$feed->setLastModified(strtotime($response->getHeader('Last-Modified')));
$feed->http_headers = $response->getHeaders();
Angie Byron
committed
// Update the feed URL in case of a 301 redirect.
Angie Byron
committed
if ($response->getEffectiveUrl() != $feed->getUrl()) {
$feed->setUrl($response->getEffectiveUrl());
Angie Byron
committed
}
return TRUE;
}
Angie Byron
committed
catch (RequestException $e) {
watchdog('aggregator', 'The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()), WATCHDOG_WARNING);
drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())) , 'warning');
Angie Byron
committed
return FALSE;
}