1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\FlexibleHttpClient; |
6
|
|
|
use Http\Client\HttpAsyncClient; |
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target |
12
|
|
|
* url and response status code. |
13
|
|
|
* |
14
|
|
|
* @author Fabien Bourigault <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
class ProfileClient implements HttpClient, HttpAsyncClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var HttpClient|HttpAsyncClient |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Collector |
27
|
|
|
*/ |
28
|
|
|
private $collector; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and |
32
|
|
|
* HttpAsyncClient interfaces. |
33
|
|
|
* @param Collector $collector |
34
|
|
|
*/ |
35
|
4 |
|
public function __construct($client, Collector $collector) |
36
|
|
|
{ |
37
|
4 |
|
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { |
38
|
|
|
throw new \RuntimeException(sprintf( |
39
|
|
|
'%s first argument must implement %s and %s. Consider using %s.', |
40
|
|
|
__METHOD__, |
41
|
|
|
HttpClient::class, |
42
|
|
|
HttpAsyncClient::class, |
43
|
|
|
FlexibleHttpClient::class |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
$this->client = $client; |
47
|
|
|
$this->collector = $collector; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
54
|
|
|
{ |
55
|
|
|
$this->collectRequestInformations($request); |
56
|
|
|
|
57
|
|
|
return $this->client->sendAsyncRequest($request); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function sendRequest(RequestInterface $request) |
64
|
|
|
{ |
65
|
|
|
$this->collectRequestInformations($request); |
66
|
|
|
|
67
|
|
|
return $this->client->sendRequest($request); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param RequestInterface $request |
72
|
|
|
*/ |
73
|
|
|
private function collectRequestInformations(RequestInterface $request) |
74
|
|
|
{ |
75
|
|
|
if (!$stack = $this->collector->getCurrentStack()) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$stack = $this->collector->getCurrentStack(); |
80
|
|
|
$stack->setRequestTarget($request->getRequestTarget()); |
81
|
|
|
$stack->setRequestMethod($request->getMethod()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: