Completed
Pull Request — master (#84)
by Tobias
08:23 queued 05:37
created

DebugPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 26.67%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 2
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 49
ccs 4
cts 15
cp 0.2667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleRequest() 0 18 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
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
 * A plugin used for log requests and responses. This plugin is executed between each normal plugin.
12
 *
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class DebugPlugin implements Plugin
16
{
17
    /**
18
     * @var DebugPluginCollector
19
     */
20
    private $collector;
21
22
    /**
23
     * @var string
24
     */
25
    private $clientName;
26
27
    /**
28
     * @var int
29
     */
30
    private $depth = -1;
31
32
    /**
33
     * @param DebugPluginCollector $collector
34
     * @param string               $clientName
35
     */
36 1
    public function __construct(DebugPluginCollector $collector, $clientName)
37
    {
38 1
        $this->collector = $collector;
39 1
        $this->clientName = $clientName;
40 1
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
46
    {
47
        $collector = $this->collector;
48
        $clientName = $this->clientName;
49
        $depth = &$this->depth;
50
51
        $collector->addRequest($request, $clientName, ++$depth);
52
53
        return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
54
            $collector->addResponse($response, $clientName, $depth--);
55
56
            return $response;
57
        }, function (Exception $exception) use ($collector, $clientName, &$depth) {
58
            $collector->addFailure($exception, $clientName, $depth--);
59
60
            throw $exception;
61
        });
62
    }
63
}
64