Completed
Pull Request — master (#14)
by Márk
03:42
created

HistoryPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 35
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleRequest() 0 14 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