1 | <?php |
||
22 | class ProfileClient implements HttpClient, HttpAsyncClient |
||
23 | { |
||
24 | /** |
||
25 | * @var HttpClient|HttpAsyncClient |
||
26 | */ |
||
27 | private $client; |
||
28 | |||
29 | /** |
||
30 | * @var Collector |
||
31 | */ |
||
32 | private $collector; |
||
33 | |||
34 | /** |
||
35 | * @var Formatter |
||
36 | */ |
||
37 | private $formatter; |
||
38 | |||
39 | /** |
||
40 | * @var Stopwatch |
||
41 | */ |
||
42 | private $stopwatch; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $eventNames = []; |
||
48 | |||
49 | /** |
||
50 | * @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or |
||
51 | * HttpAsyncClient interface. |
||
52 | * @param Collector $collector |
||
53 | * @param Formatter $formatter |
||
54 | * @param Stopwatch $stopwatch |
||
55 | */ |
||
56 | 19 | public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 3 | public function sendAsyncRequest(RequestInterface $request) |
|
72 | { |
||
73 | 3 | $activateStack = true; |
|
74 | 3 | $stack = $this->collector->getActiveStack(); |
|
75 | 3 | if (null === $stack) { |
|
76 | //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
||
77 | //we create our own stack and activate it! |
||
78 | $stack = new Stack('Default', $this->formatter->formatRequest($request)); |
||
79 | $this->collector->addStack($stack); |
||
80 | $this->collector->activateStack($stack); |
||
81 | $activateStack = false; |
||
82 | } |
||
83 | |||
84 | 3 | $this->collectRequestInformations($request, $stack); |
|
85 | 3 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
|
86 | |||
87 | 3 | $onFulfilled = function (ResponseInterface $response) use ($event, $stack) { |
|
88 | 2 | $this->collectResponseInformations($response, $event, $stack); |
|
89 | |||
90 | 2 | return $response; |
|
91 | 3 | }; |
|
92 | |||
93 | 3 | $onRejected = function (\Exception $exception) use ($event, $stack) { |
|
94 | 1 | $this->collectExceptionInformations($exception, $event, $stack); |
|
95 | |||
96 | 1 | throw $exception; |
|
97 | 3 | }; |
|
98 | |||
99 | 3 | $this->collector->deactivateStack($stack); |
|
100 | |||
101 | try { |
||
102 | 3 | return $this->client->sendAsyncRequest($request)->then($onFulfilled, $onRejected); |
|
|
|||
103 | } finally { |
||
104 | 3 | $event->stop(); |
|
105 | 3 | if ($activateStack) { |
|
106 | //We only activate the stack when created by the StackPlugin. |
||
107 | 3 | $this->collector->activateStack($stack); |
|
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 3 | public function sendRequest(RequestInterface $request) |
|
145 | |||
146 | /** |
||
147 | * @param RequestInterface $request |
||
148 | * @param Stack $stack |
||
149 | */ |
||
150 | 6 | private function collectRequestInformations(RequestInterface $request, Stack $stack) |
|
159 | |||
160 | /** |
||
161 | * @param ResponseInterface $response |
||
162 | * @param StopwatchEvent $event |
||
163 | * @param Stack $stack |
||
164 | */ |
||
165 | 5 | private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack) |
|
171 | |||
172 | /** |
||
173 | * @param \Exception $exception |
||
174 | * @param StopwatchEvent $event |
||
175 | * @param Stack $stack |
||
176 | */ |
||
177 | 1 | private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) |
|
186 | |||
187 | /** |
||
188 | * Generates the event name. |
||
189 | * |
||
190 | * @param RequestInterface $request |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | 6 | private function getStopwatchEventName(RequestInterface $request) |
|
206 | } |
||
207 |
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: