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

StackPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 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
/**
11
 * The StackPlugin must be used as first Plugin in a client stack. It's used to detect when a new request start by
12
 * creating a new Stack and pushing it to the Collector.
13
 *
14
 * @author Fabien Bourigault <[email protected]>
15
 *
16
 * @internal
17
 */
18
class StackPlugin implements Plugin
19
{
20
    /**
21
     * @var Collector
22
     */
23
    private $collector;
24
25
    /**
26
     * @var string
27
     */
28
    private $client;
29
30
    /**
31
     * @var Formatter
32
     */
33
    private $formatter;
34
35
    /**
36
     * @param Collector $collector
37
     * @param Formatter $formatter
38
     * @param string    $client
39
     */
40 4
    public function __construct(Collector $collector, Formatter $formatter, $client)
41
    {
42 4
        $this->collector = $collector;
43 4
        $this->formatter = $formatter;
44 4
        $this->client = $client;
45 4
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3 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...
51
    {
52 3
        $stack = new Stack($this->client, $this->formatter->formatRequest($request));
53
54 3
        $this->collector->addStack($stack);
55
56
        return $next($request)->then(function (ResponseInterface $response) use ($stack) {
57 1
            $stack->setResponse($this->formatter->formatResponse($response));
58
59 1
            return $response;
60 3
        }, function (Exception $exception) use ($stack) {
61 1
            $stack->setResponse($this->formatter->formatException($exception));
62 1
            $stack->setFailed(true);
63
64 1
            throw $exception;
65 3
        });
66
    }
67
}
68