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 HttpClient (or HttpAsyncClient) to gather target url and response status code. |
12
|
|
|
* |
13
|
|
|
* @author Fabien Bourigault <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @internal |
16
|
|
|
*/ |
17
|
|
|
class ProfileClient implements HttpClient, HttpAsyncClient |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HttpClient|HttpAsyncClient |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Collector |
26
|
|
|
*/ |
27
|
|
|
private $collector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param HttpClient|HttpAsyncClient $client |
31
|
|
|
* @param Collector $collector |
32
|
|
|
*/ |
33
|
|
|
public function __construct($client, Collector $collector) |
34
|
|
|
{ |
35
|
|
|
if (!$client instanceof HttpClient || !$client instanceof HttpAsyncClient) { |
36
|
|
|
throw new \RuntimeException(sprintf( |
37
|
|
|
'%s first argument must implement %s and %s. Consider using %s.', |
38
|
|
|
__METHOD__, |
39
|
|
|
HttpClient::class, |
40
|
|
|
HttpAsyncClient::class, |
41
|
|
|
FlexibleHttpClient::class |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
$this->client = $client; |
45
|
|
|
$this->collector = $collector; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
52
|
|
|
{ |
53
|
|
|
$this->collectRequestInformations($request); |
54
|
|
|
|
55
|
|
|
return $this->client->sendAsyncRequest($request); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function sendRequest(RequestInterface $request) |
62
|
|
|
{ |
63
|
|
|
$this->collectRequestInformations($request); |
64
|
|
|
|
65
|
|
|
return $this->client->sendRequest($request); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param RequestInterface $request |
70
|
|
|
*/ |
71
|
|
|
private function collectRequestInformations(RequestInterface $request) |
72
|
|
|
{ |
73
|
|
|
if (!$stack = $this->collector->getCurrentStack()) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$stack = $this->collector->getCurrentStack(); |
78
|
|
|
$stack->setRequestTarget($request->getRequestTarget()); |
79
|
|
|
$stack->setRequestMethod($request->getMethod()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: