|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Common\EmulatedHttpAsyncClient; |
|
6
|
|
|
use Http\Client\Common\EmulatedHttpClient; |
|
7
|
|
|
use Http\Client\HttpAsyncClient; |
|
8
|
|
|
use Http\Client\HttpClient; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
class ProfileClient implements HttpClient, HttpAsyncClient |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var HttpClient|HttpAsyncClient $client |
|
15
|
|
|
*/ |
|
16
|
|
|
private $client; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Collector |
|
20
|
|
|
*/ |
|
21
|
|
|
private $collector; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param HttpClient|HttpAsyncClient $client |
|
25
|
|
|
* @param Collector $collector |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function __construct($client, Collector $collector) |
|
28
|
|
|
{ |
|
29
|
1 |
|
if ($client instanceof HttpAsyncClient && $client instanceof HttpClient) { |
|
30
|
1 |
|
$this->client = $client; |
|
31
|
1 |
|
} elseif ($client instanceof HttpAsyncClient) { |
|
32
|
|
|
$this->client = new EmulatedHttpClient($client); |
|
33
|
|
|
} elseif ($client instanceof HttpClient) { |
|
34
|
|
|
$this->client = new EmulatedHttpAsyncClient($client); |
|
35
|
|
|
} else { |
|
36
|
|
|
throw new \RuntimeException(sprintf('Client must be an instance of %s or %s', HttpClient::class, HttpAsyncClient::class)); |
|
37
|
|
|
} |
|
38
|
|
|
$this->collector = $collector; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->collectRequestInformations($request); |
|
47
|
|
|
return $this->client->sendAsyncRequest($request); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function sendRequest(RequestInterface $request) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->collectRequestInformations($request); |
|
56
|
|
|
return $this->client->sendRequest($request); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param RequestInterface $request |
|
61
|
|
|
*/ |
|
62
|
|
|
private function collectRequestInformations(RequestInterface $request) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$stack = $this->collector->getCurrentStack()) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$stack = $this->collector->getCurrentStack(); |
|
69
|
|
|
$stack->setRequestTarget($request->getRequestTarget()); |
|
70
|
|
|
$stack->setRequestMethod($request->getMethod()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
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: