Skip to content

Commit c200e9f

Browse files
committed
Add history plugin
1 parent 91fc4d8 commit c200e9f

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

spec/HistoryPluginSpec.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use Http\Client\Exception\TransferException;
6+
use Http\Client\Plugin\Journal\Journal;
7+
use Http\Client\Tools\Promise\FulfilledPromise;
8+
use Http\Client\Tools\Promise\RejectedPromise;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use PhpSpec\ObjectBehavior;
12+
use Prophecy\Argument;
13+
14+
class HistoryPluginSpec extends ObjectBehavior
15+
{
16+
function let(Journal $journal)
17+
{
18+
$this->beConstructedWith($journal);
19+
}
20+
21+
function it_is_initializable()
22+
{
23+
$this->beAnInstanceOf('Http\Client\Plugin\JournalPlugin');
24+
}
25+
26+
function it_is_a_plugin()
27+
{
28+
$this->shouldImplement('Http\Client\Plugin\Plugin');
29+
}
30+
31+
function it_records_success(Journal $journal, RequestInterface $request, ResponseInterface $response)
32+
{
33+
$next = function (RequestInterface $receivedRequest) use($request, $response) {
34+
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
35+
return new FulfilledPromise($response->getWrappedObject());
36+
}
37+
};
38+
39+
$journal->addSuccess($request, $response)->shouldBeCalled();
40+
41+
$this->handleRequest($request, $next, function () {});
42+
}
43+
44+
function it_records_failure(Journal $journal, RequestInterface $request)
45+
{
46+
$exception = new TransferException();
47+
$next = function (RequestInterface $receivedRequest) use($request, $exception) {
48+
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
49+
return new RejectedPromise($exception);
50+
}
51+
};
52+
53+
$journal->addFailure($request, $exception)->shouldBeCalled();
54+
55+
$this->handleRequest($request, $next, function () {});
56+
}
57+
}

src/HistoryPlugin.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Http\Client\Exception;
6+
use Http\Client\Plugin\Journal\Journal;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
/**
11+
* Record http call.
12+
*/
13+
class HistoryPlugin implements Plugin
14+
{
15+
/**
16+
* @var Journal Journal use to store request / responses / exception.
17+
*/
18+
private $journal;
19+
20+
/**
21+
* @param Journal $journal Journal use to store request / responses / exception.
22+
*/
23+
public function __construct(Journal $journal)
24+
{
25+
$this->journal = $journal;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
32+
{
33+
$journal = $this->journal;
34+
35+
return $next($request)->then(function (ResponseInterface $response) use ($request, $journal) {
36+
$journal->addSuccess($request, $response);
37+
38+
return $response;
39+
}, function (Exception $exception) use ($request, $journal) {
40+
$journal->addFailure($request, $exception);
41+
42+
throw $exception;
43+
});
44+
}
45+
}

src/Journal/Journal.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin\Journal;
4+
5+
use Http\Client\Exception;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* Journal.
11+
*
12+
* Records history of http calls.
13+
*/
14+
interface Journal
15+
{
16+
/**
17+
* Record a successful call.
18+
*
19+
* @param RequestInterface $request Request use to make the call
20+
* @param ResponseInterface $response Response returned by the call
21+
*/
22+
public function addSuccess(RequestInterface $request, ResponseInterface $response);
23+
24+
/**
25+
* Record a failed call.
26+
*
27+
* @param RequestInterface $request Request use to make the call
28+
* @param Exception $exception Exception returned by the call
29+
*/
30+
public function addFailure(RequestInterface $request, Exception $exception);
31+
}

0 commit comments

Comments
 (0)