1 | <?php |
||
23 | class ProfileClient implements HttpClient, HttpAsyncClient |
||
24 | { |
||
25 | use VersionBridgeClient; |
||
26 | |||
27 | /** |
||
28 | * @var HttpClient|HttpAsyncClient |
||
29 | */ |
||
30 | private $client; |
||
31 | |||
32 | /** |
||
33 | * @var Collector |
||
34 | */ |
||
35 | private $collector; |
||
36 | |||
37 | /** |
||
38 | * @var Formatter |
||
39 | */ |
||
40 | private $formatter; |
||
41 | |||
42 | /** |
||
43 | * @var Stopwatch |
||
44 | */ |
||
45 | private $stopwatch; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $eventNames = []; |
||
51 | |||
52 | /** |
||
53 | * @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or |
||
54 | * HttpAsyncClient interface. |
||
55 | * @param Collector $collector |
||
56 | 19 | * @param Formatter $formatter |
|
57 | * @param Stopwatch $stopwatch |
||
58 | 19 | */ |
|
59 | public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
||
60 | { |
||
61 | if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { |
||
62 | 19 | $client = new FlexibleHttpClient($client); |
|
63 | 19 | } |
|
64 | 19 | ||
65 | 19 | $this->client = $client; |
|
66 | 19 | $this->collector = $collector; |
|
67 | $this->formatter = $formatter; |
||
68 | $this->stopwatch = $stopwatch; |
||
69 | } |
||
70 | |||
71 | 3 | /** |
|
72 | * {@inheritdoc} |
||
73 | 3 | */ |
|
74 | 3 | public function sendAsyncRequest(RequestInterface $request) |
|
75 | 3 | { |
|
76 | $activateStack = true; |
||
77 | $stack = $this->collector->getActiveStack(); |
||
78 | if (null === $stack) { |
||
79 | //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
||
80 | //we create our own stack and activate it! |
||
81 | $stack = new Stack('Default', $this->formatter->formatRequest($request)); |
||
82 | $this->collector->addStack($stack); |
||
83 | $this->collector->activateStack($stack); |
||
84 | 3 | $activateStack = false; |
|
85 | 3 | } |
|
86 | |||
87 | 3 | $this->collectRequestInformations($request, $stack); |
|
88 | 2 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
|
89 | |||
90 | 2 | $onFulfilled = function (ResponseInterface $response) use ($event, $stack) { |
|
91 | 3 | $this->collectResponseInformations($response, $event, $stack); |
|
92 | $event->stop(); |
||
93 | 3 | ||
94 | 1 | return $response; |
|
95 | }; |
||
96 | 1 | ||
97 | 3 | $onRejected = function (\Exception $exception) use ($event, $stack) { |
|
98 | $this->collectExceptionInformations($exception, $event, $stack); |
||
99 | 3 | $event->stop(); |
|
100 | |||
101 | throw $exception; |
||
102 | 3 | }; |
|
103 | |||
104 | 3 | $this->collector->deactivateStack($stack); |
|
105 | 3 | ||
106 | try { |
||
107 | 3 | return $this->client->sendAsyncRequest($request)->then($onFulfilled, $onRejected); |
|
|
|||
108 | } catch (\Exception $e) { |
||
109 | $event->stop(); |
||
110 | |||
111 | throw $e; |
||
112 | } finally { |
||
113 | if ($activateStack) { |
||
114 | //We only activate the stack when created by the StackPlugin. |
||
115 | 3 | $this->collector->activateStack($stack); |
|
116 | } |
||
117 | 3 | } |
|
118 | 3 | } |
|
119 | |||
120 | protected function doSendRequest(RequestInterface $request) |
||
121 | { |
||
122 | $stack = $this->collector->getActiveStack(); |
||
123 | if (null === $stack) { |
||
124 | //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
||
125 | 3 | //we create our own stack but don't activate it. |
|
126 | 3 | $stack = new Stack('Default', $this->formatter->formatRequest($request)); |
|
127 | $this->collector->addStack($stack); |
||
128 | } |
||
129 | 3 | ||
130 | 3 | $this->collectRequestInformations($request, $stack); |
|
131 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
||
132 | 3 | ||
133 | try { |
||
134 | $response = $this->client->sendRequest($request); |
||
135 | $this->collectResponseInformations($response, $event, $stack); |
||
136 | |||
137 | return $response; |
||
138 | } catch (\Exception $e) { |
||
139 | $this->collectExceptionInformations($e, $event, $stack); |
||
140 | |||
141 | throw $e; |
||
142 | 3 | } catch (\Throwable $e) { |
|
143 | $this->collectExceptionInformations($e, $event, $stack); |
||
144 | |||
145 | throw $e; |
||
146 | } finally { |
||
147 | $event->stop(); |
||
148 | } |
||
149 | } |
||
150 | 6 | ||
151 | /** |
||
152 | 6 | * @param RequestInterface $request |
|
153 | 6 | * @param Stack $stack |
|
154 | 6 | */ |
|
155 | 6 | private function collectRequestInformations(RequestInterface $request, Stack $stack) |
|
164 | |||
165 | 5 | /** |
|
166 | * @param ResponseInterface $response |
||
167 | 5 | * @param StopwatchEvent $event |
|
168 | 5 | * @param Stack $stack |
|
169 | 5 | */ |
|
170 | 5 | private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack) |
|
176 | |||
177 | 1 | /** |
|
178 | * @param \Exception $exception |
||
179 | 1 | * @param StopwatchEvent $event |
|
180 | * @param Stack $stack |
||
181 | */ |
||
182 | private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) |
||
191 | |||
192 | /** |
||
193 | * Generates the event name. |
||
194 | 6 | * |
|
195 | * @param RequestInterface $request |
||
196 | 6 | * |
|
197 | * @return string |
||
198 | 6 | */ |
|
199 | private function getStopwatchEventName(RequestInterface $request) |
||
211 | } |
||
212 |
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: