Completed
Pull Request — master (#84)
by Tobias
07:00
created

DebugPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleRequest() 0 19 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 passing through each 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
     *
34
     * @param DebugPluginCollector $collector
35
     * @param string $clientName
36
     */
37
    public function __construct(DebugPluginCollector $collector, $clientName)
38
    {
39
        $this->collector = $collector;
40
        $this->clientName = $clientName;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
47
    {
48
        $collector = $this->collector;
49
        $clientName = $this->clientName;
50
        $depth = &$this->depth;
51
52
        $collector->addRequest($request, $clientName, ++$depth);
53
54
        return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
55
            $collector->addResponse($response, $clientName, $depth--);
56
57
58
            return $response;
59
        }, function (Exception $exception) use ($collector, $clientName, &$depth) {
60
            $collector->addFailure($exception, $clientName, $depth--);
61
62
            throw $exception;
63
        });
64
    }
65
}
66