diff --git a/spec/HistoryPluginSpec.php b/spec/HistoryPluginSpec.php new file mode 100644 index 0000000..e3e466b --- /dev/null +++ b/spec/HistoryPluginSpec.php @@ -0,0 +1,57 @@ +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 () {}); + } +} diff --git a/src/HistoryPlugin.php b/src/HistoryPlugin.php new file mode 100644 index 0000000..830e049 --- /dev/null +++ b/src/HistoryPlugin.php @@ -0,0 +1,45 @@ +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; + }); + } +} diff --git a/src/Journal/Journal.php b/src/Journal/Journal.php new file mode 100644 index 0000000..58dc484 --- /dev/null +++ b/src/Journal/Journal.php @@ -0,0 +1,31 @@ +