Completed
Push — master ( a131e3...7447f5 )
by Fabien
03:28
created

ProfilePlugin::handleRequest()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0729

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 14
cts 19
cp 0.7368
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 3
crap 2.0729
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 6
    public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, $pluginName)
47
    {
48 6
        $this->plugin = $plugin;
49 6
        $this->collector = $collector;
50 6
        $this->formatter = $formatter;
51 6
        $this->pluginName = $pluginName;
52 6
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 5
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
58
    {
59 5
        $profile = new Profile($this->pluginName);
60
61 5
        $stack = $this->collector->getCurrentStack();
62 5
        if (null !== $stack) {
63 5
            $stack->addProfile($profile);
64 5
        }
65
66
        // wrap the next callback to profile the plugin request changes
67
        $wrappedNext = function (RequestInterface $request) use ($next, $profile) {
68 5
            $profile->setRequest($this->formatter->formatRequest($request));
69
70 5
            return $next($request);
71 5
        };
72
73
        // wrap the first callback to profile the plugin request changes
74
        $wrappedFirst = function (RequestInterface $request) use ($first, $profile) {
75
            $profile->setRequest($this->formatter->formatRequest($request));
76
77
            return $first($request);
78 5
        };
79
80
        return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile) {
81 4
            $profile->setResponse($this->formatter->formatResponse($response));
82
83 4
            return $response;
84 4
        }, function (Exception $exception) use ($profile) {
85
            $profile->setFailed(true);
86
            $profile->setResponse($this->formatter->formatException($exception));
87
88
            throw $exception;
89 4
        });
90
    }
91
}
92