Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Widop/HttpAdapter/Guzzle6HttpAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Widop\HttpAdapter;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

/**
* Guzzle Http adapter.
*
* @author Gunnar Lium <gunnarlium@gmail.com>
*/
class Guzzle6HttpAdapter extends AbstractHttpAdapter
{
/** @var ClientInterface */
private $client;

/**
* Creates a guzzle adapter.
*
* @param ClientInterface $client The guzzle client.
* @param integer $maxRedirects The maximum redirects.
*/
public function __construct(ClientInterface $client = null, $maxRedirects = 5)
{
parent::__construct($maxRedirects);

if ($client === null) {
$client = new Client(array('allow_redirects' => array('max' => $maxRedirects)));
}

$this->client = $client;
}

/**
* {@inheritdoc}
*/
public function getContent($url, array $headers = array())
{
try {
$response = $this->client->get($url, $headers);

return $response->getBody();
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}
}

/**
* {@inheritdoc}
*/
public function postContent($url, array $headers = array(), $content = '')
{
try {
$response = $this->client->post($url, array('headers' => $headers, 'form_params' => $content));

return $response->getBody();
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'guzzle6';
}
}