Completed
Pull Request — master (#33)
by Graham
08:30
created

HistoryPlugin::handleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Exception;
6
use Http\Client\Common\Plugin;
7
use Http\Client\Exception;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Http\Client\Exception as Exception because the name is already in use
Loading history...
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Record HTTP calls.
13
 *
14
 * @author Joel Wurtz <[email protected]>
15
 */
16
final class HistoryPlugin implements Plugin
17
{
18
    /**
19
     * Journal use to store request / responses / exception.
20
     *
21
     * @var Journal
22
     */
23
    private $journal;
24
25
    /**
26
     * @param Journal $journal
27 3
     */
28
    public function __construct(Journal $journal)
29 3
    {
30 3
        $this->journal = $journal;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35 2
     */
36
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
37 2
    {
38
        $journal = $this->journal;
39
40 1
        return $next($request)->then(function (ResponseInterface $response) use ($request, $journal) {
41
            $journal->addSuccess($request, $response);
42 1
43 2
            return $response;
44 1
        }, function (Exception $exception) use ($request, $journal) {
45
            $journal->addFailure($request, $exception);
46 1
47 2
            throw $exception;
48
        });
49
    }
50
}
51