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

StackPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 34 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 17
loc 50
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handleRequest() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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