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

StackPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 32.2 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 19
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handleRequest() 19 19 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
 * @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