PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / http / client-common / Plugin / HistoryPlugin.php

HistoryPlugin.php in DecaLog 4.4.0, at includes/libraries/http/client-common/Plugin/HistoryPlugin.php

50 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Http\Client\Common\Plugin;
6
7 use Http\Client\Common\Plugin;
8 use Http\Promise\Promise;
9 use Psr\Http\Client\ClientExceptionInterface;
10 use Psr\Http\Message\RequestInterface;
11 use Psr\Http\Message\ResponseInterface;
12
13 /**
14 * Record HTTP calls.
15 *
16 * @author Joel Wurtz <joel.wurtz@gmail.com>
17 */
18 final class HistoryPlugin implements Plugin
19 {
20 /**
21 * Journal use to store request / responses / exception.
22 *
23 * @var Journal
24 */
25 private $journal;
26
27 public function __construct(Journal $journal)
28 {
29 $this->journal = $journal;
30 }
31
32 /**
33 * {@inheritdoc}
34 */
35 public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
36 {
37 $journal = $this->journal;
38
39 return $next($request)->then(function (ResponseInterface $response) use ($request, $journal) {
40 $journal->addSuccess($request, $response);
41
42 return $response;
43 }, function (ClientExceptionInterface $exception) use ($request, $journal) {
44 $journal->addFailure($request, $exception);
45
46 throw $exception;
47 });
48 }
49 }
50