Skip to content

Commit

Permalink
Merge pull request #177 from alexdebril/issue/173
Browse files Browse the repository at this point in the history
Issue/173
  • Loading branch information
alexdebril authored May 29, 2018
2 parents 7565c31 + c22ab80 commit 70ef2d4
Showing 4 changed files with 70 additions and 4 deletions.
36 changes: 36 additions & 0 deletions examples/change-user-agent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require __DIR__.DIRECTORY_SEPARATOR.'bootstrap.php';

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;

$logger = new Logger('Logger');
$stack = HandlerStack::create();
$stack->push(
Middleware::log(
$logger,
new MessageFormatter('{request}')
)
);

$client = new \FeedIo\Adapter\Guzzle\Client(
new GuzzleHttp\Client([
'handler' => $stack
]),
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
);

$feedIo = new \FeedIo\FeedIo($client, $logger);

$result = $feedIo->read('http://php.net/feed.atom');

echo "feed title : {$result->getFeed()->getTitle()} \n ";

$client->setUserAgent('Another User Agent');

$feedIo->read('http://php.net/feed.atom');

echo "feed title : {$result->getFeed()->getTitle()} \n ";
28 changes: 26 additions & 2 deletions src/FeedIo/Adapter/Guzzle/Client.php
Original file line number Diff line number Diff line change
@@ -24,17 +24,41 @@
*/
class Client implements ClientInterface
{

/**
* Default user agent provided with the package
*/
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1';

/**
* @var \GuzzleHttp\ClientInterface
*/
protected $guzzleClient;

/**
* @var string
*/
protected $userAgent;

/**
* @param \GuzzleHttp\ClientInterface $guzzleClient
* @param string $userAgent
*/
public function __construct(\GuzzleHttp\ClientInterface $guzzleClient)
public function __construct(\GuzzleHttp\ClientInterface $guzzleClient, string $userAgent = self::DEFAULT_USER_AGENT)
{
$this->guzzleClient = $guzzleClient;
$this->userAgent = $userAgent;
}

/**
* @param string $userAgent The new user-agent
* @return Client
*/
public function setUserAgent(string $userAgent) : Client
{
$this->userAgent = $userAgent;

return $this;
}

/**
@@ -113,7 +137,7 @@ protected function getOptions(\DateTime $modifiedSince) : array
{
return [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1',
'User-Agent' => $this->userAgent,
'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822)
]
];
7 changes: 7 additions & 0 deletions tests/FeedIo/Adapter/Guzzle/ClientTest.php
Original file line number Diff line number Diff line change
@@ -43,6 +43,13 @@ public function testGetResponse()
$this->assertEquals(1994, $response->getLastModified()->format('Y'));
}

public function testSetUserAgent()
{
$this->object->setUserAgent('the new user-agent');

$this->assertAttributeEquals('the new user-agent', 'userAgent', $this->object);
}

/**
* @expectedException \FeedIo\Adapter\NotFoundException
*/
3 changes: 1 addition & 2 deletions tests/FeedIo/FeedIoTest.php
Original file line number Diff line number Diff line change
@@ -139,8 +139,7 @@ public function testSetReader()
$logger = new \Psr\Log\NullLogger();
$reader = new Reader(
new Adapter\Guzzle\Client(
new \GuzzleHttp\Client(),
$logger
new \GuzzleHttp\Client()
),
$logger
);

0 comments on commit 70ef2d4

Please sign in to comment.