Skip to content

Add history plugin #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2015
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions spec/HistoryPluginSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace spec\Http\Client\Plugin;

use Http\Client\Exception\TransferException;
use Http\Client\Plugin\Journal\Journal;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\RejectedPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class HistoryPluginSpec extends ObjectBehavior
{
function let(Journal $journal)
{
$this->beConstructedWith($journal);
}

function it_is_initializable()
{
$this->beAnInstanceOf('Http\Client\Plugin\JournalPlugin');
}

function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_records_success(Journal $journal, RequestInterface $request, ResponseInterface $response)
{
$next = function (RequestInterface $receivedRequest) use($request, $response) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
return new FulfilledPromise($response->getWrappedObject());
}
};

$journal->addSuccess($request, $response)->shouldBeCalled();

$this->handleRequest($request, $next, function () {});
}

function it_records_failure(Journal $journal, RequestInterface $request)
{
$exception = new TransferException();
$next = function (RequestInterface $receivedRequest) use($request, $exception) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
return new RejectedPromise($exception);
}
};

$journal->addFailure($request, $exception)->shouldBeCalled();

$this->handleRequest($request, $next, function () {});
}
}
45 changes: 45 additions & 0 deletions src/HistoryPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Http\Client\Plugin;

use Http\Client\Exception;
use Http\Client\Plugin\Journal\Journal;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Record http call.
*/
class HistoryPlugin implements Plugin
{
/**
* @var Journal Journal use to store request / responses / exception.
*/
private $journal;

/**
* @param Journal $journal Journal use to store request / responses / exception.
*/
public function __construct(Journal $journal)
{
$this->journal = $journal;
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$journal = $this->journal;

return $next($request)->then(function (ResponseInterface $response) use ($request, $journal) {
$journal->addSuccess($request, $response);

return $response;
}, function (Exception $exception) use ($request, $journal) {
$journal->addFailure($request, $exception);

throw $exception;
});
}
}
31 changes: 31 additions & 0 deletions src/Journal/Journal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Http\Client\Plugin\Journal;

use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Journal.
*
* Records history of http calls.
*/
interface Journal
{
/**
* Record a successful call.
*
* @param RequestInterface $request Request use to make the call
* @param ResponseInterface $response Response returned by the call
*/
public function addSuccess(RequestInterface $request, ResponseInterface $response);

/**
* Record a failed call.
*
* @param RequestInterface $request Request use to make the call
* @param Exception $exception Exception returned by the call
*/
public function addFailure(RequestInterface $request, Exception $exception);
}