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 ProfileFactory |
34
|
|
|
*/ |
35
|
|
|
private $factory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $pluginName; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Plugin $plugin |
44
|
|
|
* @param Collector $collector |
45
|
|
|
* @param Formatter $formatter |
46
|
|
|
* @param ProfileFactory $factory |
47
|
|
|
* @param string $pluginName |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, ProfileFactory $factory, $pluginName) |
50
|
|
|
{ |
51
|
|
|
$this->plugin = $plugin; |
52
|
|
|
$this->collector = $collector; |
53
|
|
|
$this->formatter = $formatter; |
54
|
|
|
$this->factory = $factory; |
55
|
|
|
$this->pluginName = $pluginName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$profile = $this->factory->createProfile(); |
64
|
|
|
$profile->setPlugin($this->pluginName); |
65
|
|
|
$profile->setRequest($this->formatter->formatRequest($request)); |
66
|
|
|
|
67
|
|
|
$this->collector->getCurrentStack()->addProfile($profile); |
68
|
|
|
|
69
|
|
|
return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) { |
70
|
|
|
$profile->setResponse($this->formatter->formatResponse($response)); |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
}, function (Exception $exception) use ($profile) { |
74
|
|
|
$profile->setFailed(true); |
75
|
|
|
$profile->setResponse($this->formatter->formatException($exception)); |
76
|
|
|
|
77
|
|
|
throw $exception; |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.