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

ProfilePlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 57
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 17
loc 57
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 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 ProfilePlugin decorates any Plugin to fill Profile to keep representation of plugin input/output. Created profile
12
 * is pushed in the current Stack.
13
 *
14
 * @author Fabien Bourigault <[email protected]>
15
 *
16
 * @internal
17
 */
18
class ProfilePlugin implements Plugin
19
{
20
    /**
21
     * @var Plugin
22
     */
23
    private $plugin;
24
25
    /**
26
     * @var Collector
27
     */
28
    private $collector;
29
30
    /**
31
     * @var Formatter
32
     */
33
    private $formatter;
34
35
    /**
36
     * @var string
37
     */
38
    private $pluginName;
39
40
    /**
41
     * @param Plugin    $plugin
42
     * @param Collector $collector
43
     * @param Formatter $formatter
44
     * @param string    $pluginName
45
     */
46 5
    public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, $pluginName)
47
    {
48 5
        $this->plugin = $plugin;
49 5
        $this->collector = $collector;
50 5
        $this->formatter = $formatter;
51 5
        $this->pluginName = $pluginName;
52 5
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4 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...
58
    {
59 4
        $profile = new Profile($this->pluginName, $this->formatter->formatRequest($request));
60
61 4
        $this->collector->getCurrentStack()->addProfile($profile);
62
63
        return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) {
64 1
            $profile->setResponse($this->formatter->formatResponse($response));
65
66 1
            return $response;
67 4
        }, function (Exception $exception) use ($profile) {
68 1
            $profile->setFailed(true);
69 1
            $profile->setResponse($this->formatter->formatException($exception));
70
71 1
            throw $exception;
72 4
        });
73
    }
74
}
75