Completed
Pull Request — master (#128)
by Fabien
07:44
created

StackPlugin::handleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 3
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
/**
11
 * @author Fabien Bourigault <[email protected]>
12
 *
13
 * @internal
14
 */
15
class StackPlugin implements Plugin
16
{
17
    /**
18
     * @var Collector
19
     */
20
    private $collector;
21
22
    /**
23
     * @var string
24
     */
25
    private $client;
26
27
    /**
28
     * @var StackFactory
29
     */
30
    private $factory;
31
32
    /**
33
     * @var Formatter
34
     */
35
    private $formatter;
36
37
    /**
38
     * @param Collector    $collector
39
     * @param Formatter    $formatter
40
     * @param StackFactory $factory
41
     * @param string       $client
42
     */
43
    public function __construct(Collector $collector, Formatter $formatter, StackFactory $factory, $client)
44
    {
45
        $this->collector = $collector;
46
        $this->formatter = $formatter;
47
        $this->factory = $factory;
48
        $this->client = $client;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 View Code Duplication
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $stack = $this->factory->createStack();
57
        $stack->setClient($this->client);
58
        $stack->setRequest($this->formatter->formatRequest($request));
59
60
        $this->collector->addStack($stack);
61
62
        return $next($request)->then(function (ResponseInterface $response) use ($stack) {
63
            $stack->setResponse($this->formatter->formatResponse($response));
64
65
            return $response;
66
        }, function (Exception $exception) use ($stack) {
67
            $stack->setResponse($this->formatter->formatException($exception));
68
            $stack->setFailed(true);
69
70
            throw $exception;
71
        });
72
    }
73
}
74