Completed
Pull Request — master (#128)
by Fabien
09:11
created

DebugPluginDecorator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleRequest() 0 4 1
A wrap() 0 14 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Closure;
6
use Http\Client\Common\Plugin;
7
use Http\Client\Exception;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class DebugPluginDecorator implements Plugin
12
{
13
    /**
14
     * @var Plugin
15
     */
16
    private $plugin;
17
18
    /**
19
     * @var Collector
20
     */
21
    private $collector;
22
23
    /**
24
     * @param Plugin    $plugin
25
     * @param Collector $collector
26
     */
27
    public function __construct(Plugin $plugin, Collector $collector)
28
    {
29
        $this->plugin = $plugin;
30
        $this->collector = $collector;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
37
    {
38
        $this->plugin->handleRequest($request, $this->wrap($next, false), $this->wrap($first, true));
39
    }
40
41
    /**
42
     * @param callable $callable
43
     * @param bool     $first
44
     *
45
     * @return Closure
46
     */
47
    private function wrap(callable $callable, $first)
48
    {
49
        return function (RequestInterface $request) use ($callable, $first) {
50
            $profile = $this->collector->getProfile($this->plugin, $request);
51
            $profile->setFirst($first);
52
            return $callable($request)->then(function (ResponseInterface $response) use ($profile) {
53
                $profile->setResponse($response);
54
                return $response;
55
            }, function (Exception $exception) use ($profile) {
56
                $profile->setException($exception);
57
                throw $exception;
58
            });
59
        };
60
    }
61
}