Completed
Pull Request — master (#128)
by Fabien
08:06
created

StackPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

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