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 both HttpClient and |
||
51 | * HttpAsyncClient interfaces. |
||
52 | * @param Collector $collector |
||
53 | * @param Formatter $formatter |
||
54 | * @param Stopwatch $stopwatch |
||
55 | */ |
||
56 | 13 | public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function sendAsyncRequest(RequestInterface $request) |
||
77 | { |
||
78 | $stack = $this->collector->getActiveStack(); |
||
79 | |||
80 | $this->collectRequestInformations($request, $stack); |
||
|
|||
81 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
||
82 | |||
83 | $onFulfilled = function (ResponseInterface $response) use ($event, $stack) { |
||
84 | $this->collectResponseInformations($response, $event, $stack); |
||
85 | |||
86 | return $response; |
||
87 | }; |
||
88 | |||
89 | $onRejected = function (\Exception $exception) use ($event, $stack) { |
||
90 | $this->collectExceptionInformations($exception, $event, $stack); |
||
91 | |||
92 | throw $exception; |
||
93 | }; |
||
94 | |||
95 | $this->collector->deactivateStack($stack); |
||
96 | |||
97 | try { |
||
98 | return $this->client->sendAsyncRequest($request)->then($onFulfilled, $onRejected); |
||
99 | } finally { |
||
100 | $event->stop(); |
||
101 | $this->collector->activateStack($stack); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function sendRequest(RequestInterface $request) |
||
109 | { |
||
110 | $stack = $this->collector->getActiveStack(); |
||
111 | |||
112 | $this->collectRequestInformations($request, $stack); |
||
113 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
||
114 | |||
115 | try { |
||
116 | $response = $this->client->sendRequest($request); |
||
117 | $this->collectResponseInformations($response, $event, $stack); |
||
118 | |||
119 | return $response; |
||
120 | } catch (\Exception $e) { |
||
121 | $this->collectExceptionInformations($e, $event, $stack); |
||
122 | |||
123 | throw $e; |
||
124 | } catch (\Throwable $e) { |
||
125 | $this->collectExceptionInformations($e, $event, $stack); |
||
126 | |||
127 | throw $e; |
||
128 | } finally { |
||
129 | $event->stop(); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param RequestInterface $request |
||
135 | * @param Stack $stack |
||
136 | */ |
||
137 | private function collectRequestInformations(RequestInterface $request, Stack $stack) |
||
138 | { |
||
139 | $stack->setRequestTarget($request->getRequestTarget()); |
||
140 | $stack->setRequestMethod($request->getMethod()); |
||
141 | $stack->setRequestScheme($request->getUri()->getScheme()); |
||
142 | $stack->setRequestHost($request->getUri()->getHost()); |
||
143 | $stack->setClientRequest($this->formatter->formatRequest($request)); |
||
144 | $stack->setCurlCommand($this->formatter->formatAsCurlCommand($request)); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param ResponseInterface $response |
||
149 | * @param StopwatchEvent $event |
||
150 | * @param Stack $stack |
||
151 | */ |
||
152 | private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack) |
||
153 | { |
||
154 | $stack->setDuration($event->getDuration()); |
||
155 | $stack->setResponseCode($response->getStatusCode()); |
||
156 | $stack->setClientResponse($this->formatter->formatResponse($response)); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param \Exception $exception |
||
161 | * @param StopwatchEvent $event |
||
162 | * @param Stack $stack |
||
163 | */ |
||
164 | private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) |
||
165 | { |
||
166 | if ($exception instanceof HttpException) { |
||
167 | $this->collectResponseInformations($exception->getResponse(), $event, $stack); |
||
168 | } |
||
169 | |||
170 | $stack->setDuration($event->getDuration()); |
||
171 | $stack->setClientException($this->formatter->formatException($exception)); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Generates the event name. |
||
176 | * |
||
177 | * @param RequestInterface $request |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | private function getStopwatchEventName(RequestInterface $request) |
||
193 | } |
||
194 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: