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

ProfilePlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
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 ProfilePlugin implements Plugin
16
{
17
    /**
18
     * @var Plugin
19
     */
20
    private $plugin;
21
22
    /**
23
     * @var Collector
24
     */
25
    private $collector;
26
27
    /**
28
     * @var Formatter
29
     */
30
    private $formatter;
31
32
    /**
33
     * @var string
34
     */
35
    private $pluginName;
36
37
    /**
38
     * @param Plugin    $plugin
39
     * @param Collector $collector
40
     * @param Formatter $formatter
41
     * @param string    $pluginName
42
     */
43
    public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, $pluginName)
44
    {
45
        $this->plugin = $plugin;
46
        $this->collector = $collector;
47
        $this->formatter = $formatter;
48
        $this->pluginName = $pluginName;
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
        $profile = new Profile();
57
        $profile->setPlugin($this->pluginName);
58
        $profile->setRequest($this->formatter->formatRequest($request));
59
60
        $this->collector->getCurrentStack()->addProfile($profile);
61
62
        return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) {
63
            $profile->setResponse($this->formatter->formatResponse($response));
64
65
            return $response;
66
        }, function (Exception $exception) use ($profile) {
67
            $profile->setFailed(true);
68
            $profile->setResponse($this->formatter->formatException($exception));
69
70
            throw $exception;
71
        });
72
    }
73
}
74