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
|
|
|
public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter, $pluginName) |
47
|
|
|
{ |
48
|
|
|
$this->plugin = $plugin; |
49
|
|
|
$this->collector = $collector; |
50
|
|
|
$this->formatter = $formatter; |
51
|
|
|
$this->pluginName = $pluginName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$profile = new Profile($this->pluginName, $this->formatter->formatRequest($request)); |
60
|
|
|
|
61
|
|
|
$this->collector->getCurrentStack()->addProfile($profile); |
62
|
|
|
|
63
|
|
|
return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) { |
64
|
|
|
$profile->setResponse($this->formatter->formatResponse($response)); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
}, function (Exception $exception) use ($profile) { |
68
|
|
|
$profile->setFailed(true); |
69
|
|
|
$profile->setResponse($this->formatter->formatException($exception)); |
70
|
|
|
|
71
|
|
|
throw $exception; |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.