Completed
Pull Request — master (#14)
by Márk
05:00
created

HistoryPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\Exception;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Record HTTP calls.
12
 *
13
 * @author Joel Wurtz <[email protected]>
14
 *
15
 * TODO: make class final in version 2.0, once plugins does not extend it anymore.
16
 */
17
/*final*/ class HistoryPlugin implements Plugin
18
{
19
    /**
20
     * Journal use to store request / responses / exception.
21
     *
22
     * @var Journal
23
     */
24
    private $journal;
25
26
    /**
27
     * @param Journal $journal
28
     */
29 3
    public function __construct(Journal $journal)
30
    {
31 3
        $this->journal = $journal;
32 3
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
38
    {
39 2
        $journal = $this->journal;
40
41
        return $next($request)->then(function (ResponseInterface $response) use ($request, $journal) {
42 1
            $journal->addSuccess($request, $response);
43
44 1
            return $response;
45 2
        }, function (Exception $exception) use ($request, $journal) {
46 1
            $journal->addFailure($request, $exception);
47
48 1
            throw $exception;
49 2
        });
50
    }
51
}
52