-
Notifications
You must be signed in to change notification settings - Fork 49
/
ProfilePlugin.php
76 lines (64 loc) · 1.9 KB
/
ProfilePlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Http\HttplugBundle\Collector;
use Exception;
use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* The ProfilePlugin decorates any Plugin to fill Profile to keep representation of plugin input/output. Created profile
* is pushed in the current Stack.
*
* @author Fabien Bourigault <[email protected]>
*
* @internal
*/
class ProfilePlugin implements Plugin
{
/**
* @var Plugin
*/
private $plugin;
/**
* @var Collector
*/
private $collector;
/**
* @var Formatter
*/
private $formatter;
/**
* @var string
*/
private $pluginName;
/**
* @param Plugin $plugin
* @param Collector $collector
* @param Formatter $formatter
* @param string $pluginName
*/
public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, $pluginName)
{
$this->plugin = $plugin;
$this->collector = $collector;
$this->formatter = $formatter;
$this->pluginName = $pluginName;
}
/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$profile = new Profile($this->pluginName, $this->formatter->formatRequest($request));
if ($stack = $this->collector->getCurrentStack()) {
$stack->addProfile($profile);
}
return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) {
$profile->setResponse($this->formatter->formatResponse($response));
return $response;
}, function (Exception $exception) use ($profile) {
$profile->setFailed(true);
$profile->setResponse($this->formatter->formatException($exception));
throw $exception;
});
}
}